Skip to content

Commit

Permalink
Update 0202.快乐数.md
Browse files Browse the repository at this point in the history
  • Loading branch information
z80160280 authored Jun 1, 2021
1 parent 7a0d134 commit 4fc751a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion problems/0202.快乐数.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,29 @@ class Solution {
```

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:

Expand Down

0 comments on commit 4fc751a

Please sign in to comment.