Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ab7a26

Browse files
committed
docs: update solution 202, add a new contributor @Wushiyii
1 parent 68f8b47 commit 4ab7a26

File tree

4 files changed

+30
-51
lines changed

4 files changed

+30
-51
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
3434
|---|---|---|---|---|---|---|---|
3535
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44309823?v=4" width="80px;"/>](https://github.com/KongJHong) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/18181519?v=4" width="80px;"/>](https://github.com/limbowandering) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37685012?v=4" width="80px;"/>](https://github.com/jxdeng3989) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44314231?v=4" width="80px;"/>](https://github.com/igayhub) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/30177307?v=4" width="80px;"/>](https://github.com/MCN1998) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/5793058?v=4" width="80px;"/>](https://github.com/Fairyhead) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/24841082?v=4" width="80px;"/>](https://github.com/zhng1456) </center> |
3636
| <center> [<img src="https://avatars3.githubusercontent.com/u/32598987?v=4" width="80px;"/>](https://github.com/xiapengchng) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37660444?v=4" width="80px;"/>](https://github.com/Mcnwork2018) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/22535595?v=4" width="80px;"/>](https://github.com/bluesword12350) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/39827514?v=4" width="80px;"/>](https://github.com/ashwek) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25475525?v=4" width="80px;"/>](https://github.com/Mrtj2016) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/19249839?v=4" width="80px;"/>](https://github.com/biubiubiubiubiubiubiu) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31436272?v=4" width="80px;"/>](https://github.com/Neil94n) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/9976942?v=4" width="80px;"/>](https://github.com/LZHD) </center> |
37+
| <center> [<img src="https://avatars3.githubusercontent.com/u/31801502?v=4" width="80px;"/>](https://github.com/Wushiyii) </center> | | | | | | | |
3738

3839
<!-- ALL-CONTRIBUTORS-LIST:END -->

‎SOLUTION_TREE.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@
546546
├── 0200.Number of Islands
547547
│ ├── README.md
548548
│ └── Solution.py
549+
├── 0202.Happy Number
550+
│ ├── README.md
551+
│ └── Solution.java
549552
├── 0203.Remove Linked List Elements
550553
│ ├── README.md
551554
│ ├── Solution.java
@@ -639,6 +642,8 @@
639642
│ ├── Solution.cpp
640643
│ ├── Solution.js
641644
│ └── Solution.py
645+
├── 0423.Reconstruct Original Digits from English
646+
│ └── Solution.cpp
642647
├── 0427.Construct Quad Tree
643648
│ └── Solution.cpp
644649
├── 0434.Number of Segments in a String

‎solution/0202.Happy Number/README.md‎

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。
99

10-
示例:
10+
**示例**:
1111
```
1212
输入: 19
1313
输出: true
@@ -19,46 +19,25 @@
1919
```
2020

2121
### 解法
22-
在进行验算的过程中,发现一个规律,只要过程中得到任意一个结果和为4,那么就一定会按 `4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4`
23-
进行循环,这样的数就不为快乐数;此外,结果和与若是与输入n或者上一轮结果和n相同,那也不为快乐数.
22+
在进行验算的过程中,发现一个规律,只要过程中得到任意一个结果和为 4,那么就一定会按 `4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4`
23+
进行循环,这样的数就不为快乐数;此外,结果和与若是与输入 n 或者上一轮结果和 n 相同,那也不为快乐数
2424

2525
```java
2626
class Solution {
2727
public boolean isHappy(int n) {
28-
if (n <= 0) return false;
29-
int sum = 0;
30-
while (sum != n) {
31-
while (n > 0) {
32-
sum += Math.pow(n % 10 ,2);
33-
n /= 10;
34-
}
35-
if (sum == 1) {
36-
return true;
37-
} else if (sum == 4) {
38-
return false;
39-
} else {
40-
n = sum;
41-
sum = 0;
42-
}
43-
}
44-
return false;
45-
}
46-
}
47-
48-
// 递归
49-
public boolean isHappy(int n) {
5028
if (n <= 0) return false;
5129
int sum = 0;
30+
while (sum != n) {
5231
while (n > 0) {
53-
sum += Math.pow(n % 10 ,2);
32+
sum += Math.pow(n % 10, 2);
5433
n /= 10;
5534
}
56-
if (sum == 1) {
57-
return true;
58-
} else if (sum == 4) {
59-
return false;
60-
} else {
61-
return isHappy(sum);
62-
}
35+
if (sum == 1) return true;
36+
if (sum == 4) return false;
37+
n = sum;
38+
sum = 0;
39+
}
40+
return false;
6341
}
42+
}
6443
```
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class Solution {
32
public boolean isHappy(int n) {
43
if (n <= 0) return false;
@@ -8,32 +7,27 @@ public boolean isHappy(int n) {
87
sum += Math.pow(n % 10, 2);
98
n /= 10;
109
}
11-
if (sum == 1) {
12-
return true;
13-
} else if (sum == 4) {
14-
return false;
15-
} else {
16-
n = sum;
17-
sum = 0;
18-
}
10+
if (sum == 1) return true;
11+
if (sum == 4) return false;
12+
n = sum;
13+
sum = 0;
1914
}
2015
return false;
2116
}
17+
}
18+
2219

23-
// 递归
24-
public boolean isHappy2(int n) {
20+
// 递归
21+
class Solution {
22+
public boolean isHappy(int n) {
2523
if (n <= 0) return false;
2624
int sum = 0;
2725
while (n > 0) {
2826
sum += Math.pow(n % 10, 2);
2927
n /= 10;
3028
}
31-
if (sum == 1) {
32-
return true;
33-
} else if (sum == 4) {
34-
return false;
35-
} else {
36-
return isHappy2(sum);
37-
}
29+
if (sum == 1) return true;
30+
if (sum == 4) return false;
31+
return isHappy(sum);
3832
}
3933
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /