File tree 6 files changed +120
-2
lines changed
6 files changed +120
-2
lines changed Original file line number Diff line number Diff line change 45
45
| [ 7] ( https://leetcode-cn.com/problems/reverse-integer ) | [ 整数反转] ( /solution/1-99/0007.reverse-integer/ ) | ` 数学 ` | <font color =green >简单</font > | ✅ |
46
46
| [ 9] ( https://leetcode-cn.com/problems/palindrome-number ) | [ 回文数] ( /solution/1-99/0009.palindrome-number/ ) | ` 数学 ` | <font color =green >简单</font > | ✅ |
47
47
| [ 13] ( https://leetcode-cn.com/problems/roman-to-integer ) | [ 罗马数字转整数] ( /solution/1-99/0013.roman-to-integer/ ) | ` 数学 ` ,` 字符串 ` | <font color =green >简单</font > | ✅ |
48
+ | [ 202] ( https://leetcode-cn.com/problems/happy-number ) | [ 快乐数] ( /solution/200-299/0202.happy-number/ ) | ` 哈希表 ` ,` 数学 ` | <font color =green >简单</font > | ✅ |
48
49
| [ 268] ( https://leetcode-cn.com/problems/missing-number ) | [ 缺失数字] ( /solution/200-299/0268.missing-number/ ) | ` 位运算 ` ,` 数组 ` ,` 数学 ` | <font color =green >简单</font > | ✅ |
49
50
50
51
#### ** 哈希表**
54
55
| [ 1] ( https://leetcode-cn.com/problems/two-sum ) | [ 两数之和] ( /solution/1-99/0001.two-sum/ ) | ` 数组 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
55
56
| [ 36] ( https://leetcode-cn.com/problems/valid-sudoku ) | [ 有效的数独] ( /solution/1-99/0036.valid-sudoku/ ) | ` 哈希表 ` | <font color =blue >中等</font > | ✅ |
56
57
| [ 136] ( https://leetcode-cn.com/problems/single-number ) | [ 只出现一次的数字] ( /solution/100-199/0136.single-number/ ) | ` 位运算 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
58
+ | [ 202] ( https://leetcode-cn.com/problems/happy-number ) | [ 快乐数] ( /solution/200-299/0202.happy-number/ ) | ` 哈希表 ` ,` 数学 ` | <font color =green >简单</font > | ✅ |
57
59
| [ 217] ( https://leetcode-cn.com/problems/contains-duplicate ) | [ 存在重复元素] ( /solution/200-299/0217.contains-duplicate/ ) | ` 数组 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
58
60
| [ 219] ( https://leetcode-cn.com/problems/contains-duplicate-ii ) | [ 存在重复元素 ii] ( /solution/200-299/0219.contains-duplicate-ii/ ) | ` 数组 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
59
61
Original file line number Diff line number Diff line change 64
64
| [ 196] ( https://leetcode-cn.com/problems/delete-duplicate-emails ) | [ 删除重复的电子邮箱] ( /solution/100-199/0196.delete-duplicate-emails/ ) | | <font color =green >简单</font > |
65
65
| [ 197] ( https://leetcode-cn.com/problems/rising-temperature ) | [ 上升的温度] ( /solution/100-199/0197.rising-temperature/ ) | | <font color =green >简单</font > |
66
66
| [ 198] ( https://leetcode-cn.com/problems/house-robber ) | [ 打家劫舍] ( /solution/100-199/0198.house-robber/ ) | ` 动态规划 ` | <font color =green >简单</font > |
67
- | [ 202] ( https://leetcode-cn.com/problems/happy-number ) | [ 快乐数] ( /solution/200-299/0202.happy-number/ ) | ` 哈希表 ` ,` 数学 ` | <font color =green >简单</font > |
67
+ | [ 202] ( https://leetcode-cn.com/problems/happy-number ) | [ 快乐数] ( /solution/200-299/0202.happy-number/ ) | ` 哈希表 ` ,` 数学 ` | <font color =green >简单</font > | ✅ |
68
68
| [ 203] ( https://leetcode-cn.com/problems/remove-linked-list-elements ) | [ 移除链表元素] ( /solution/200-299/0203.remove-linked-list-elements/ ) | ` 链表 ` | <font color =green >简单</font > |
69
69
| [ 204] ( https://leetcode-cn.com/problems/count-primes ) | [ 计数质数] ( /solution/200-299/0204.count-primes/ ) | ` 哈希表 ` ,` 数学 ` | <font color =green >简单</font > |
70
70
| [ 205] ( https://leetcode-cn.com/problems/isomorphic-strings ) | [ 同构字符串] ( /solution/200-299/0205.isomorphic-strings/ ) | ` 哈希表 ` | <font color =green >简单</font > |
Original file line number Diff line number Diff line change
1
+ # [ 202. 快乐数] ( https://leetcode-cn.com/problems/happy-number/description/ )
2
+
3
+ ### 题目描述
4
+
5
+ <p >编写一个算法来判断一个数 <code >n</code > 是不是快乐数。</p >
6
+
7
+ <p >「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 <strong >无限循环</strong > 但始终变不到 1。如果 <strong >可以变为</strong >  ; 1,那么这个数就是快乐数。</p >
8
+
9
+ <p >如果 <code >n</code > 是快乐数就返回 <code >True</code > ;不是,则返回 <code >False</code > 。</p >
10
+
11
+ <p >  ; </p >
12
+
13
+ <p ><strong >示例:</strong ></p >
14
+
15
+ <pre ><strong >输入:</strong >19
16
+ <strong >输出:</strong >true
17
+ <strong >解释:
18
+ </strong >1<sup >2</sup > + 9<sup >2</sup > = 82
19
+ 8<sup >2</sup > + 2<sup >2</sup > = 68
20
+ 6<sup >2</sup > + 8<sup >2</sup > = 100
21
+ 1<sup >2</sup > + 0<sup >2</sup > + 0<sup >2</sup > = 1
22
+ </pre >
23
+
24
+ ### 解题思路
25
+
26
+ 1 . hash table
27
+
28
+ ### 具体解法
29
+
30
+
31
+ #### ** Golang**
32
+ ``` go
33
+ func isHappy (n int ) bool {
34
+ numMap := make (map [int ]bool )
35
+ for n != 1 {
36
+ num := 0
37
+ for i := 0 ; i < len (strconv.Itoa (n)); i++ {
38
+ v := n / powerf (10 , i) % 10
39
+ num += v * v
40
+ }
41
+ n = num
42
+ if _ , ok := numMap[n]; ok {
43
+ return false
44
+ }
45
+ numMap[n] = true
46
+ }
47
+ return true
48
+ }
49
+
50
+ func powerf (x int , n int ) int {
51
+ if n == 0 {
52
+ return 1
53
+ }
54
+ return x * powerf (x, n-1 )
55
+ }
56
+ ```
57
+
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ import (
4
+ "strconv"
5
+ )
6
+
7
+ /*
8
+ * @lc app=leetcode.cn id=202 lang=golang
9
+ *
10
+ * [202] 快乐数
11
+ */
12
+
13
+ // @lc code=start
14
+ func isHappy (n int ) bool {
15
+ numMap := make (map [int ]bool )
16
+ for n != 1 {
17
+ num := 0
18
+ for i := 0 ; i < len (strconv .Itoa (n )); i ++ {
19
+ v := n / powerf (10 , i ) % 10
20
+ num += v * v
21
+ }
22
+ n = num
23
+ if _ , ok := numMap [n ]; ok {
24
+ return false
25
+ }
26
+ numMap [n ] = true
27
+ }
28
+ return true
29
+ }
30
+
31
+ func powerf (x int , n int ) int {
32
+ if n == 0 {
33
+ return 1
34
+ }
35
+ return x * powerf (x , n - 1 )
36
+ }
37
+
38
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ func TestIsHappy (t * testing.T ) {
8
+ var ret bool
9
+ var num int
10
+ num = 19
11
+ ret = true
12
+ if ret != isHappy (num ) {
13
+ t .Fatalf ("case fails %v\n " , ret )
14
+ }
15
+
16
+ num = 2
17
+ ret = false
18
+ if ret != isHappy (num ) {
19
+ t .Fatalf ("case fails %v\n " , ret )
20
+ }
21
+ }
Original file line number Diff line number Diff line change 8
8
- [ 200 - 299题] ( index-tags.md )
9
9
- [ 200. 岛屿数量] ( solution/200-299/0200.number-of-islands/ )
10
10
- [ 201. 数字范围按位与] ( solution/200-299/0201.bitwise-and-of-numbers-range/ )
11
- - [ 202. 快乐数] ( solution/200-299/0202.happy-number/ )
11
+ - [ 202. 快乐数 ✅ ] ( solution/200-299/0202.happy-number/ )
12
12
- [ 203. 移除链表元素] ( solution/200-299/0203.remove-linked-list-elements/ )
13
13
- [ 204. 计数质数] ( solution/200-299/0204.count-primes/ )
14
14
- [ 205. 同构字符串] ( solution/200-299/0205.isomorphic-strings/ )
You can’t perform that action at this time.
0 commit comments