Skip to content

Commit 2f6a592

Browse files
committed
feat: add solutions to lc problem: No.0202
No.0202.Happy Number
1 parent de784eb commit 2f6a592

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ func getSum(n int) (sum int) {
154154

155155
### **TypeScript**
156156

157+
```ts
158+
function isHappy(n: number): boolean {
159+
const getNext = (n: number) => {
160+
let res = 0;
161+
while (n !== 0) {
162+
res += (n % 10) ** 2;
163+
n = Math.floor(n / 10);
164+
}
165+
return res;
166+
};
167+
const set = new Set();
168+
while (n !== 1) {
169+
const next = getNext(n);
170+
if (set.has(next)) {
171+
return false;
172+
}
173+
set.add(next);
174+
n = next;
175+
}
176+
return true;
177+
}
178+
```
179+
157180
```ts
158181
function isHappy(n: number): boolean {
159182
const getNext = (n: number) => {
@@ -177,6 +200,33 @@ function isHappy(n: number): boolean {
177200

178201
### **Rust**
179202

203+
```rust
204+
use std::collections::HashSet;
205+
impl Solution {
206+
fn get_next(mut n: i32) -> i32 {
207+
let mut res = 0;
208+
while n != 0 {
209+
res += (n % 10).pow(2);
210+
n /= 10;
211+
}
212+
res
213+
}
214+
215+
pub fn is_happy(mut n: i32) -> bool {
216+
let mut set = HashSet::new();
217+
while n != 1 {
218+
let next = Self::get_next(n);
219+
if set.contains(&next) {
220+
return false;
221+
}
222+
set.insert(next);
223+
n = next;
224+
}
225+
true
226+
}
227+
}
228+
```
229+
180230
```rust
181231
impl Solution {
182232
pub fn is_happy(n: i32) -> bool {
@@ -199,6 +249,29 @@ impl Solution {
199249
}
200250
```
201251

252+
### **C**
253+
254+
```c
255+
int getNext(int n) {
256+
int res = 0;
257+
while (n) {
258+
res += (n % 10) * (n % 10);
259+
n /= 10;
260+
}
261+
return res;
262+
}
263+
264+
bool isHappy(int n) {
265+
int slow = n;
266+
int fast = getNext(n);
267+
while (slow != fast) {
268+
slow = getNext(slow);
269+
fast = getNext(getNext(fast));
270+
}
271+
return fast == 1;
272+
}
273+
```
274+
202275
### **...**
203276
204277
```

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ func getSum(n int) (sum int) {
138138

139139
### **TypeScript**
140140

141+
```ts
142+
function isHappy(n: number): boolean {
143+
const getNext = (n: number) => {
144+
let res = 0;
145+
while (n !== 0) {
146+
res += (n % 10) ** 2;
147+
n = Math.floor(n / 10);
148+
}
149+
return res;
150+
};
151+
const set = new Set();
152+
while (n !== 1) {
153+
const next = getNext(n);
154+
if (set.has(next)) {
155+
return false;
156+
}
157+
set.add(next);
158+
n = next;
159+
}
160+
return true;
161+
}
162+
```
163+
141164
```ts
142165
function isHappy(n: number): boolean {
143166
const getNext = (n: number) => {
@@ -161,6 +184,33 @@ function isHappy(n: number): boolean {
161184

162185
### **Rust**
163186

187+
```rust
188+
use std::collections::HashSet;
189+
impl Solution {
190+
fn get_next(mut n: i32) -> i32 {
191+
let mut res = 0;
192+
while n != 0 {
193+
res += (n % 10).pow(2);
194+
n /= 10;
195+
}
196+
res
197+
}
198+
199+
pub fn is_happy(mut n: i32) -> bool {
200+
let mut set = HashSet::new();
201+
while n != 1 {
202+
let next = Self::get_next(n);
203+
if set.contains(&next) {
204+
return false;
205+
}
206+
set.insert(next);
207+
n = next;
208+
}
209+
true
210+
}
211+
}
212+
```
213+
164214
```rust
165215
impl Solution {
166216
pub fn is_happy(n: i32) -> bool {
@@ -183,6 +233,29 @@ impl Solution {
183233
}
184234
```
185235

236+
### **C**
237+
238+
```c
239+
int getNext(int n) {
240+
int res = 0;
241+
while (n) {
242+
res += (n % 10) * (n % 10);
243+
n /= 10;
244+
}
245+
return res;
246+
}
247+
248+
bool isHappy(int n) {
249+
int slow = n;
250+
int fast = getNext(n);
251+
while (slow != fast) {
252+
slow = getNext(slow);
253+
fast = getNext(getNext(fast));
254+
}
255+
return fast == 1;
256+
}
257+
```
258+
186259
### **...**
187260
188261
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int getNext(int n) {
2+
int res = 0;
3+
while (n) {
4+
res += (n % 10) * (n % 10);
5+
n /= 10;
6+
}
7+
return res;
8+
}
9+
10+
bool isHappy(int n) {
11+
int slow = n;
12+
int fast = getNext(n);
13+
while (slow != fast) {
14+
slow = getNext(slow);
15+
fast = getNext(getNext(fast));
16+
}
17+
return fast == 1;
18+
}

0 commit comments

Comments
 (0)