Skip to content

Commit b102005

Browse files
committed
feat: add solutions to lc problem: No.0202
No.0202.Happy Number
1 parent 67b4229 commit b102005

File tree

6 files changed

+249
-129
lines changed

6 files changed

+249
-129
lines changed

solution/0200-0299/0202.Happy Number/README.md

Lines changed: 111 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
简单模拟,有可能进入死循环导致无法停止,有几种方式解决:
54+
**方法一:哈希表 + 模拟**
5555

56-
- 哈希表:转换过程不会重复出现同一个数字。
57-
- 限制转换次数:在一定次数转换后还未成功变为 1,那么就断言此数不是快乐数。
58-
- 快慢指针:与判断链表是否存在环原理一致。
56+
将每次转换后的数字存入哈希表,如果出现重复数字,说明进入了循环,不是快乐数。否则,如果转换后的数字为 $1$,说明是快乐数。
57+
58+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。
59+
60+
**方法二:快慢指针**
61+
62+
与判断链表是否存在环原理一致。如果 $n$ 是快乐数,那么快指针最终会与慢指针相遇,且相遇时的数字为 $1$;否则,快指针最终会与慢指针相遇,且相遇时的数字不为 $1$。
63+
64+
因此,最后判断快慢指针相遇时的数字是否为 $1$ 即可。
65+
66+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
5967

6068
<!-- tabs:start -->
6169

@@ -66,43 +74,72 @@
6674
```python
6775
class Solution:
6876
def isHappy(self, n: int) -> bool:
69-
def get_next(n):
70-
s = 0
71-
while n > 0:
72-
n, digit = divmod(n, 10)
73-
s += digit**2
74-
return s
75-
76-
visited = set()
77-
while n != 1 and n not in visited:
78-
visited.add(n)
79-
n = get_next(n)
77+
vis = set()
78+
while n != 1 and n not in vis:
79+
vis.add(n)
80+
x = 0
81+
while n:
82+
n, v = divmod(n, 10)
83+
x += v * v
84+
n = x
8085
return n == 1
8186
```
8287

88+
```python
89+
class Solution:
90+
def isHappy(self, n: int) -> bool:
91+
def next(x):
92+
y = 0
93+
while x:
94+
x, v = divmod(x, 10)
95+
y += v * v
96+
return y
97+
98+
slow, fast = n, next(n)
99+
while slow != fast:
100+
slow, fast = next(slow), next(next(fast))
101+
return slow == 1
102+
```
103+
83104
### **Java**
84105

85106
<!-- 这里可写当前语言的特殊实现逻辑 -->
86107

87108
```java
88109
class Solution {
89110
public boolean isHappy(int n) {
90-
Set<Integer> visited = new HashSet<>();
91-
while (n != 1 && !visited.contains(n)) {
92-
visited.add(n);
93-
n = getNext(n);
111+
Set<Integer> vis = new HashSet<>();
112+
while (n != 1 && !vis.contains(n)) {
113+
vis.add(n);
114+
int x = 0;
115+
while (n != 0) {
116+
x += (n % 10) * (n % 10);
117+
n /= 10;
118+
}
119+
n = x;
94120
}
95121
return n == 1;
96122
}
123+
}
124+
```
97125

98-
private int getNext(int n) {
99-
int s = 0;
100-
while (n > 0) {
101-
int digit = n % 10;
102-
s += digit * digit;
103-
n /= 10;
126+
```java
127+
class Solution {
128+
public boolean isHappy(int n) {
129+
int slow = n, fast = next(n);
130+
while (slow != fast) {
131+
slow = next(slow);
132+
fast = next(next(fast));
104133
}
105-
return s;
134+
return slow == 1;
135+
}
136+
137+
private int next(int x) {
138+
int y = 0;
139+
for (; x > 0; x /= 10) {
140+
y += (x % 10) * (x % 10);
141+
}
142+
return y;
106143
}
107144
}
108145
```
@@ -113,19 +150,35 @@ class Solution {
113150
class Solution {
114151
public:
115152
bool isHappy(int n) {
116-
auto getNext = [](int n) {
117-
int res = 0;
118-
while (n) {
119-
res += pow(n % 10, 2);
120-
n /= 10;
153+
unordered_set<int> vis;
154+
while (n != 1 && !vis.count(n)) {
155+
vis.insert(n);
156+
int x = 0;
157+
for (; n; n /= 10) {
158+
x += (n % 10) * (n % 10);
121159
}
122-
return res;
160+
n = x;
161+
}
162+
return n == 1;
163+
}
164+
};
165+
```
166+
167+
```cpp
168+
class Solution {
169+
public:
170+
bool isHappy(int n) {
171+
auto next = [](int x) {
172+
int y = 0;
173+
for (; x; x /= 10) {
174+
y += pow(x % 10, 2);
175+
}
176+
return y;
123177
};
124-
int slow = n;
125-
int fast = getNext(n);
178+
int slow = n, fast = next(n);
126179
while (slow != fast) {
127-
slow = getNext(slow);
128-
fast = getNext(getNext(fast));
180+
slow = next(slow);
181+
fast = next(next(fast));
129182
}
130183
return slow == 1;
131184
}
@@ -136,19 +189,33 @@ public:
136189

137190
```go
138191
func isHappy(n int) bool {
139-
m := make(map[int]bool)
140-
for n != 1 && !m[n] {
141-
n, m[n] = getSum(n), true
192+
vis := map[int]bool{}
193+
for n != 1 && !vis[n] {
194+
vis[n] = true
195+
x := 0
196+
for ; n > 0; n /= 10 {
197+
x += (n % 10) * (n % 10)
198+
}
199+
n = x
142200
}
143201
return n == 1
144202
}
203+
```
145204

146-
func getSum(n int) (sum int) {
147-
for n > 0 {
148-
sum += (n % 10) * (n % 10)
149-
n = n / 10
205+
```go
206+
func isHappy(n int) bool {
207+
next := func(x int) (y int) {
208+
for ; x > 0; x /= 10 {
209+
y += (x % 10) * (x % 10)
210+
}
211+
return
212+
}
213+
slow, fast := n, next(n)
214+
for slow != fast {
215+
slow = next(slow)
216+
fast = next(next(fast))
150217
}
151-
return
218+
return slow == 1
152219
}
153220
```
154221

solution/0200-0299/0202.Happy Number/README_EN.md

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,70 @@
5252
```python
5353
class Solution:
5454
def isHappy(self, n: int) -> bool:
55-
def get_next(n):
56-
s = 0
57-
while n > 0:
58-
n, digit = divmod(n, 10)
59-
s += digit**2
60-
return s
61-
62-
visited = set()
63-
while n != 1 and n not in visited:
64-
visited.add(n)
65-
n = get_next(n)
55+
vis = set()
56+
while n != 1 and n not in vis:
57+
vis.add(n)
58+
x = 0
59+
while n:
60+
n, v = divmod(n, 10)
61+
x += v * v
62+
n = x
6663
return n == 1
6764
```
6865

66+
```python
67+
class Solution:
68+
def isHappy(self, n: int) -> bool:
69+
def next(x):
70+
y = 0
71+
while x:
72+
x, v = divmod(x, 10)
73+
y += v * v
74+
return y
75+
76+
slow, fast = n, next(n)
77+
while slow != fast:
78+
slow, fast = next(slow), next(next(fast))
79+
return slow == 1
80+
```
81+
6982
### **Java**
7083

7184
```java
7285
class Solution {
7386
public boolean isHappy(int n) {
74-
Set<Integer> visited = new HashSet<>();
75-
while (n != 1 && !visited.contains(n)) {
76-
visited.add(n);
77-
n = getNext(n);
87+
Set<Integer> vis = new HashSet<>();
88+
while (n != 1 && !vis.contains(n)) {
89+
vis.add(n);
90+
int x = 0;
91+
while (n != 0) {
92+
x += (n % 10) * (n % 10);
93+
n /= 10;
94+
}
95+
n = x;
7896
}
7997
return n == 1;
8098
}
99+
}
100+
```
81101

82-
private int getNext(int n) {
83-
int s = 0;
84-
while (n > 0) {
85-
int digit = n % 10;
86-
s += digit * digit;
87-
n /= 10;
102+
```java
103+
class Solution {
104+
public boolean isHappy(int n) {
105+
int slow = n, fast = next(n);
106+
while (slow != fast) {
107+
slow = next(slow);
108+
fast = next(next(fast));
109+
}
110+
return slow == 1;
111+
}
112+
113+
private int next(int x) {
114+
int y = 0;
115+
for (; x > 0; x /= 10) {
116+
y += (x % 10) * (x % 10);
88117
}
89-
return s;
118+
return y;
90119
}
91120
}
92121
```
@@ -97,19 +126,35 @@ class Solution {
97126
class Solution {
98127
public:
99128
bool isHappy(int n) {
100-
auto getNext = [](int n) {
101-
int res = 0;
102-
while (n) {
103-
res += pow(n % 10, 2);
104-
n /= 10;
129+
unordered_set<int> vis;
130+
while (n != 1 && !vis.count(n)) {
131+
vis.insert(n);
132+
int x = 0;
133+
for (; n; n /= 10) {
134+
x += (n % 10) * (n % 10);
105135
}
106-
return res;
136+
n = x;
137+
}
138+
return n == 1;
139+
}
140+
};
141+
```
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
bool isHappy(int n) {
147+
auto next = [](int x) {
148+
int y = 0;
149+
for (; x; x /= 10) {
150+
y += pow(x % 10, 2);
151+
}
152+
return y;
107153
};
108-
int slow = n;
109-
int fast = getNext(n);
154+
int slow = n, fast = next(n);
110155
while (slow != fast) {
111-
slow = getNext(slow);
112-
fast = getNext(getNext(fast));
156+
slow = next(slow);
157+
fast = next(next(fast));
113158
}
114159
return slow == 1;
115160
}
@@ -120,19 +165,33 @@ public:
120165

121166
```go
122167
func isHappy(n int) bool {
123-
m := make(map[int]bool)
124-
for n != 1 && !m[n] {
125-
n, m[n] = getSum(n), true
168+
vis := map[int]bool{}
169+
for n != 1 && !vis[n] {
170+
vis[n] = true
171+
x := 0
172+
for ; n > 0; n /= 10 {
173+
x += (n % 10) * (n % 10)
174+
}
175+
n = x
126176
}
127177
return n == 1
128178
}
179+
```
129180

130-
func getSum(n int) (sum int) {
131-
for n > 0 {
132-
sum += (n % 10) * (n % 10)
133-
n = n / 10
181+
```go
182+
func isHappy(n int) bool {
183+
next := func(x int) (y int) {
184+
for ; x > 0; x /= 10 {
185+
y += (x % 10) * (x % 10)
186+
}
187+
return
188+
}
189+
slow, fast := n, next(n)
190+
for slow != fast {
191+
slow = next(slow)
192+
fast = next(next(fast))
134193
}
135-
return
194+
return slow == 1
136195
}
137196
```
138197

0 commit comments

Comments
 (0)