题目:
Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

翻译:
写一个算法去检测一个数是否是“快乐”的。
“快乐数”按照如下方法定义:首先它是一个正数,使用它的每位数字的平方的和替换它,直到这个数等于1,或者它会一直循环而不到1。这些能够到1终止的数就是“快乐数”。

分析:
本方案采用递归方式去遍历每个数字,直到它小于10,并且预先算出每个10以内的数字定义是否是“快乐数”。

代码:

public class Solution {public boolean isHappy(int n) {if(n>9){int sum=0;int nk=n;while(nk>9){sum+=(nk%10)*(nk%10);nk/=10;}sum+=(nk%10)*(nk%10);return isHappy(sum);}if(n==1||n==7){return true;}else{return false;}}
}

Leet Code OJ 202. Happy Number [Difficulty: Easy]相关推荐

  1. Leet Code OJ 263. Ugly Number [Difficulty: Easy]

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  2. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  3. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. Leet Code OJ 283. Move Zeroes [Difficulty: Easy]

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  5. Leet Code OJ 292. Nim Game [Difficulty: Easy]

    题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  6. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  7. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  8. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  9. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

最新文章

  1. CSS Float(浮动)
  2. HighCharts使用心得
  3. 计算机网络:第三章 数据链路层
  4. 如何成为一名合格的推荐系统工程师?
  5. Linux——安装FTP服务器
  6. 全国计算机等级考试题库二级C操作题100套(第67套)
  7. 2020邮箱账号密码大全_通知 | 复旦大学2020年春季学期研究生选课FAQ
  8. 信用卡还款直减500
  9. SequoiaDB 系列源码分析调整
  10. awk命令过滤tomcat的访日日志中IP地址
  11. sas中一些小的选项的含义
  12. C# LINQ 对象克隆
  13. 第四届中国云计算大会——123
  14. 利用Eclipse中的Maven构建Web项目(一)
  15. mysql的数据库文件在哪里_MySQL数据库文件其具体的存放位置简述
  16. selenium 模拟浏览器刷新
  17. 百度senta使用方法
  18. linux 设置mail
  19. 打光篇-Radiometric Photometric概念
  20. 用户行为分析,就该这么做!

热门文章

  1. tomcat apache mysql_Android实现与Apache Tomcat服务器数据交互(MySql数据库)
  2. mysql innodb索引覆盖_Mysql InnoDB 覆盖索引与回表
  3. HDU4546(优先队列)
  4. mysql_ping与mysql长连接
  5. C++虚继承(八) --- 虚继承与继承的差异
  6. 区分Debug版还是Relase版
  7. SqlServer安装出错解决办法
  8. python 下载拉钩教育AES加密视频
  9. 掌握这 20 个容器实战技巧!
  10. 聊一聊Kafka分区的隐藏属性——二次归类