LeetCode | Happy Number
来源:互联网 发布:python re 编辑:程序博客网 时间:2024/06/08 02:33
题目
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
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
分析与思路
得到每位数字的平方和比较简单,使用一个while语句就能实现,难点主要是对Happy Number性质的了解上。如果不是Happy Number,那么将会无限循环下去,此时该怎么办?
我的思路是这样的,首先非常容易就能判断出 在一位数字中 只有1和7是Happy Number,那么对一个数进行每位数字平方和的计算时,只要在循环中出现2 3 4 5 6 8 9中的一个,该数就不是Happy Number。按照这个思路生成的代码顺利通过了所有的测试,但是这里其实需要证明在每个不是Happy Number的循环中,一定会在某次出现1~9中的一个数。
代码为:
class Solution {public: int sumOfTheSquaresOfItsDigits(int n) { int sum=0; while(n) { sum+=(n%10)*(n%10); n/=10; } return sum; } bool isHappy(int n) { if(n<=0)return false; while(n>10) n=sumOfTheSquaresOfItsDigits(n); if(n==1 || n==7 || n==10) return true; else return false; }};
其实,上面的思路还有种变形,也即不需要到10,只要到7即可,2, 3 4 5 6都不是Happy Number,这样的思路写出的代码也顺利通过了所有测试样例。
代码:
bool isHappy(int n) { while(n>6) { int next = 0; while(n){next+=(n%10)*(n%10); n/=10;} n = next; } return n==1;}
在Discuss区看到有人提出 所有不是Happy Number的数在cycle内都会出现4,所以把4作为了一个magic_number,他提供的代码为:
bool isHappy(int n) { if (n <= 0) return false; int magic = 4; while (1) { if (n == 1) return true; if (n == magic) return false; int t = 0; while (n) { t += (n % 10) * (n % 10); n /= 10; } n = t; }}
但是!!!!以上的做法都面临一个问题,那就是需要给出严格的数学证明证明出:在非Happy Number的每次计算自己数字平方和的过程中,一定会出现1~9、1~6、4。如果没有证明,其实还是不能严格说算法是正确的。在做题的时候我就没想到这点,但是所有测试用例都能通过。由于我数学是体育老师教的,我没办法给出严格的证明过程。所以我试着找一种其他方法解决这个问题。
int型整数最大值是2147483647,也即所有数各个位上的数字平方和最大也不会超过9*9*10=810(10位数字全是9时),实际上第9位(从0位起算)最高位连3都不可能。所以可以开一个810个元素的数组(已经远远足够),对于出现过的数字, 就将相应的数组下标那个元素标志为true,问题就转换成一个简单的hash应用问题。
代码:
class Solution {public: int sumOfTheSquaresOfItsDigits(int n) { int sum=0; while(n) { sum+=(n%10)*(n%10); n/=10; } return sum; } bool isHappy(int n) { bool hash[810]; memset(hash,false,sizeof(hash)); int sum=n; while(true) { sum = sumOfTheSquaresOfItsDigits(sum); if(sum == 1) return true; else { if(hash[sum] == true) return false; else hash[sum] = true; } } }};
0 0
- [leetcode] Happy Number
- leetcode: Happy Number
- [LeetCode] Happy Number
- [LeetCode]Happy Number
- LeetCode 202 Happy Number
- LeetCode OJ Happy Number
- [LeetCode]202.Happy Number
- LeetCode 202-Happy Number
- leetcode-202 Happy Number
- [LeetCode]Happy Number
- leetcode 202: Happy Number
- leetcode Happy Number
- [LeetCode 202]Happy Number
- LeetCode (24) Happy Number
- LeetCode Happy Number 题解
- [LeetCode] Happy Number
- leetcode 202 Happy Number
- LeetCode Happy Number
- Android-给你一个不一样的EditText
- iOS 开发者经典网站
- 天融信防火墙NGFW4000配置手册
- saltstack入门教程精华案例
- xlsx转csv格式,修改自apache poi提供的转换类
- LeetCode | Happy Number
- curl命令
- Hibernate中HQL函数汇总及其说明
- MFC学习之 在Button控件上画线
- QT——QWidget、QMainWindow、 QDialog用法区别解析
- Openwrt /etc/rc.button/reset 保存配置安全脚本
- AUTOCAD shift+鼠标中键 旋转视图后的还原方法
- 将table边框改为细线
- MySQL DATE_FORMAT() 函数