leetcode 202. Happy Number
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
很自然的解法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution( object ): def isHappy( self , n): """ :type n: int :rtype: bool """ def sum2(num): ans = 0 while num ! = 0 : ans + = (num % 10 ) * * 2 num = num / 10 return ans m = set () while n not in m: if n = = 1 : return True m.add(n) n = sum2(n) return False |
还有可以使用环路链表的判断方法,一个快慢指针一起赛跑,直到追上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution( object ): def isHappy( self , n): """ :type n: int :rtype: bool """ def sum2(num): ans = 0 while num ! = 0 : ans + = (num % 10 ) * * 2 num = num / 10 return ans slow = n fast = sum2(sum2(n)) while slow ! = fast: slow = sum2(slow) fast = sum2(sum2(fast)) return slow = = 1 |
java代码其实非常优雅:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int digitSquareSum( int n) { int sum = 0 , tmp; while (n) { tmp = n % 10 ; sum += tmp * tmp; n /= 10 ; } return sum; } bool isHappy( int n) { int slow, fast; slow = fast = n; do { slow = digitSquareSum(slow); fast = digitSquareSum(fast); fast = digitSquareSum(fast); } while (slow != fast); if (slow == 1 ) return 1 ; else return 0 ; } |
数学解法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution { public : int loop[ 8 ] = { 4 , 16 , 37 , 58 , 89 , 145 , 42 , 20 }; bool inLoop( int n){ for (auto x: loop){ if (x == n) return true ; } return false ; } bool isHappy( int n) { if (n == 1 ) return true ; if (inLoop(n)) return false ; int next = 0 ; while (n){ next += (n% 10 )*(n% 10 ); n /= 10 ; } return isHappy(next); } }; |
1 2 3 4 5 6 7 | proof: 1 .loop number is less than 162 . Assume f(x) is the sum of the squares of x’s digits. let’s say 0 < x <= 9 , 999 , 999 , 999 which is larger than the range of an int . f(x) <= 9 ^ 2 * 10 = 810 . So no mater how big x is, after one step of f(x), it will be less than 810 .The most large f(x) value (x < 810 ) is f( 799 ) = 211 . And do this several times: f( 211 ) < f( 199 ) = 163 . f( 163 ) < f( 99 ) = 162 . So no mater which x you choose after several times of f(x),it finally fall in the range of [ 1 , 162 ] and can never get out. 2 .I check every unhappy number in range of [ 1 , 162 ] , they all fall in loop { 4 , 16 , 37 , 58 , 89 , 145 , 42 , 20 } ,which means every unhappy number fall in this loop. |
其实通过枚举就应该知道上述<=810范围内的数字都会落在特定范围,通过不断缩小范围来找规律。=》
Using fact all numbers in [2, 6] are not happy (and all not happy numbers end on a cycle that hits this interval):
bool isHappy(int n) {
while(n>6){
int next = 0;
while(n){next+=(n%10)*(n%10); n/=10;}
n = next;
}
return n==1;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution( object ): def isHappy( self , n): """ :type n: int :rtype: bool """ def sum2(num): ans = 0 while num ! = 0 : ans + = (num % 10 ) * * 2 num = num / 10 return ans while n > 6 : n = sum2(n) return n = = 1 |
标签:
leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」