mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0202.快乐数.md
This commit is contained in:
@ -108,7 +108,29 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isHappy(self, n: int) -> bool:
|
||||||
|
set_ = set()
|
||||||
|
while 1:
|
||||||
|
sum_ = self.getSum(n)
|
||||||
|
if sum_ == 1:
|
||||||
|
return True
|
||||||
|
#如果这个sum曾经出现过,说明已经陷入了无限循环了,立刻return false
|
||||||
|
if sum_ in set_:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
set_.add(sum_)
|
||||||
|
n = sum_
|
||||||
|
|
||||||
|
#取数值各个位上的单数之和
|
||||||
|
def getSum(self, n):
|
||||||
|
sum_ = 0
|
||||||
|
while n > 0:
|
||||||
|
sum_ += (n%10) * (n%10)
|
||||||
|
n //= 10
|
||||||
|
return sum_
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user