Skip to content

Commit 9116411

Browse files
committed
二刷202
1 parent aa38364 commit 9116411

File tree

5 files changed

+95
-16
lines changed

5 files changed

+95
-16
lines changed

docs/0202-happy-number.adoc

+53-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
11
[#0202-happy-number]
2-
= 202. Happy Number
2+
= 202. 快乐数
33

4-
{leetcode}/problems/happy-number/[LeetCode - Happy Number^]
4+
https://leetcode.cn/problems/happy-number/[LeetCode - 202. 快乐数 ^]
55

6-
竟然可以使用"双指针",类似判断链表中是否有环的思路!妙!
6+
编写一个算法来判断一个数 `n` 是不是快乐数。
77

8+
*「快乐数」* 定义为:
89

9-
Write an algorithm to determine if a number is "happy".
10+
* 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
11+
* 然后重复这个过程直到这个数变为 1,也可能是 *无限循环* 但始终变不到 1。
12+
* 如果这个过程 *结果为* 1,那么这个数就是快乐数。
1013
11-
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
14+
如果 `n`_快乐数_ 就返回 `true` ;不是,则返回 `false`
1215

13-
*Example: *
16+
*示例 1:*
1417

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
*Input:* 19
18-
*Output:* true
19-
*Explanation:
20-
*1^2^ + 9^2^ = 82
21-
8^2^ + 2^2^ = 68
22-
6^2^ + 8^2^ = 100
23-
1^2^ + 0^2^ + 0^2^ = 1
24-
----
18+
....
19+
输入:n = 19
20+
输出:true
21+
解释:
22+
12 + 92 = 82
23+
82 + 22 = 68
24+
62 + 82 = 100
25+
12 + 02 + 02 = 1
26+
....
27+
28+
*示例 2:*
29+
30+
....
31+
输入:n = 2
32+
输出:false
33+
....
34+
35+
36+
*提示:*
37+
38+
* `+1 <= n <= 2+`^`+31+`^`+-1+`
2539
40+
== 思路分析
41+
42+
竟然可以使用"双指针",类似判断链表中是否有环的思路!妙!
43+
44+
双指针的思路,来个图就一目了然了:
45+
46+
image::images/0202-01.png[{image_attr}]
2647

2748
[[src-0202]]
49+
[tabs]
50+
====
51+
一刷::
52+
+
53+
--
2854
[{java_src_attr}]
2955
----
3056
include::{sourcedir}/_0202_HappyNumber.java[tag=answer]
3157
----
58+
--
59+
60+
二刷::
61+
+
62+
--
63+
[{java_src_attr}]
64+
----
65+
include::{sourcedir}/_0202_HappyNumber_2.java[tag=answer]
66+
----
67+
--
68+
====
3269

docs/images/0202-01.png

73.6 KB
Loading

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ endif::[]
5050
|{doc_base_url}/0141-linked-list-cycle.adoc[题解]
5151
|✅ 快慢指针
5252

53+
|{counter:codes2503}
54+
|{leetcode_base_url}/happy-number/[202. 快乐数^]
55+
|{doc_base_url}/0202-happy-number.adoc[题解]
56+
|✅ 快慢指针
57+
5358
|===
5459

5560
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0202_HappyNumber.java

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class _0202_HappyNumber {
3232
* Memory Usage: 33.4 MB, less than 10.60% of Java online submissions for Happy Number.
3333
*
3434
* Copy from: https://leetcode.com/problems/happy-number/discuss/56917/My-solution-in-C(-O(1)-space-and-no-magic-math-property-involved-)[My solution in C( O(1) space and no magic math property involved ) - LeetCode Discuss]
35+
*
36+
* @author D瓜哥 · https://www.diguage.com
37+
* @since 2020-01-10 21:40
3538
*/
3639
public boolean isHappy(int n) {
3740
int slow = n;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0202_HappyNumber_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2020-01-10 21:40
9+
*/
10+
public boolean isHappy(int n) {
11+
int slow = n, fast = n;
12+
do {
13+
slow = squareSum(slow);
14+
fast = squareSum(fast);
15+
fast = squareSum(fast);
16+
if (fast == 1) {
17+
return true;
18+
}
19+
} while (slow != fast);
20+
21+
return false;
22+
}
23+
24+
private int squareSum(int num) {
25+
int sum = 0;
26+
while (num > 0) {
27+
int n = num % 10;
28+
sum += n * n;
29+
num /= 10;
30+
}
31+
return sum;
32+
}
33+
// end::answer[]
34+
}

0 commit comments

Comments
 (0)