We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7d61f3c commit 55282daCopy full SHA for 55282da
56. 快乐数.md
@@ -0,0 +1,28 @@
1
+***编写一个算法来判断一个数 n 是不是快乐数。***
2
+
3
+```
4
+输入:n = 19
5
+输出:true
6
+解释:
7
+12 + 92 = 82
8
+82 + 22 = 68
9
+62 + 82 = 100
10
+12 + 02 + 02 = 1
11
12
13
14
+def isHappy(self, n: int) -> bool:
15
+ def get_next(number):
16
+ total_sum = 0
17
+ while number > 0:
18
+ number, digit = divmod(number, 10)
19
+ total_sum += digit ** 2
20
+ return total_sum
21
22
+ slow_runner = n
23
+ fast_runner = get_next(n)
24
+ while fast_runner != 1 and slow_runner != fast_runner:
25
+ slow_runner = get_next(slow_runner)
26
+ fast_runner = get_next(get_next(fast_runner))
27
+ return fast_runner == 1
28
0 commit comments