1
- # 202. Happy Number - Best Practices of LeetCode Solutions
2
- LeetCode link: [ 202. Happy Number] ( https://leetcode.com/problems/happy-number ) ,
3
- [ 202. 快乐数] ( https://leetcode.cn/problems/happy-number )
1
+ # 202. 快乐数 - 力扣题解最佳实践
2
+ 力扣链接:[ 202. 快乐数] ( https://leetcode.cn/problems/happy-number ) , 难度: ** 简单** 。
4
3
5
- [ 中文题解] ( #中文题解 )
6
-
7
- ## LeetCode problem description
8
- Write an algorithm to determine if a number ` n ` is happy.
9
-
10
- A ** happy number** is a number defined by the following process:
11
-
12
- * Starting with any positive integer, replace the number by the sum of the squares of its digits.
13
- * Repeat the process until the number equals 1 (where it will stay), or it ** loops endlessly in a cycle** which does not include 1.
14
- * Those numbers for which this process ** ends in 1** are happy.
4
+ ## 力扣“202. 快乐数”问题描述
5
+ 编写一个算法来判断一个数 ` n ` 是不是快乐数。
15
6
16
- Return ` true ` if ` n ` is _ a happy number _ , and ` false ` if not.
7
+ 「 ** 快乐数 ** 」 定义为:
17
8
18
- Difficulty: ** Easy**
9
+ * 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
10
+ * 然后重复这个过程直到这个数变为 ` 1 ` ,也可能是 ** 无限循环** 但始终变不到 ` 1 ` 。
11
+ * 如果这个过程 ** 结果为** ` 1 ` ,那么这个数就是快乐数。
12
+ * 如果 ` n ` 是 _ 快乐数_ 就返回 ` true ` ;不是,则返回 ` false ` 。
19
13
20
- ### [ Example 1]
21
- ** Input ** : ` n = 19 `
14
+ ### [ 示例 1]
15
+ ** 输入 ** : ` n = 19 `
22
16
23
- ** Output ** : ` true `
17
+ ** 输出 ** : ` true `
24
18
25
- ** Explanation **
19
+ ** 解释 ** :
26
20
```
27
21
1**2 + 9**2 = 82
28
22
8**2 + 2**2 = 68
29
23
6**2 + 8**2 = 100
30
24
1**2 + 0**2 + 0**2 = 1
31
25
```
32
26
33
- ### [ Example 2]
34
- ** Input ** : ` n = 2`
27
+ ### [ 示例 2]
28
+ ** 输入 ** : ` k = 2, prices = [3,2,6,5,0,3] `
35
29
36
- ** Output ** : ` false `
30
+ ** 输出 ** : ` 7 `
37
31
38
- ### [ Constraints]
39
- - ` 1 <= n <= 2**31 - 1 `
32
+ ** 解释** :
33
+ ```
34
+ 在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
35
+ 随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
36
+ ```
40
37
41
- ## Intuition
42
- [ 中文题解] ( #中文题解 )
38
+ ### [ 约束]
39
+ - ` 1 <= k <= 100 `
40
+ - ` 1 <= prices.length <= 1000 `
41
+ - ` 0 <= prices[i] <= 1000 `
43
42
44
- 1 . It is more convenient to call ` isHappy(n) ` recursively. You only need to generate a new ` n ` as a parameter each time.
45
- 2 . If ` n ` has already appeared, it means that the loop has been entered, and ` return false ` . You can use ` Set ` to save the ` n ` that has appeared.
43
+ ## 思路
44
+ 1 . 递归调用` isHappy(n) ` 比较方便,每次只需要生成新的` n ` 作为参数。
45
+ 2 . 如果` n ` 已经出现过了,说明进入了循环,` return false ` 。可以用` Set ` 保存已经出现过的` n ` 。
46
46
47
- ## Steps
48
- 1 . Generate a new ` n ` as the ` isHappy(n) ` parameter.
47
+ ## 步骤
48
+ 1 . 生成新的 ` n ` 作为 ` isHappy() ` 的参数。
49
49
``` javascript
50
50
let sum = 0
51
51
@@ -58,9 +58,9 @@ for (const digit of n.toString()) {
58
58
return isHappy (sum)
59
59
```
60
60
61
- 2 . If ` n ` has already appeared, it means that the loop has been entered, and ` return false ` . You can use ` Set ` to save the ` n ` that has appeared.
61
+ 2 . 如果 ` n ` 已经出现过了,说明进入了循环, ` return false ` 。可以用 ` Set ` 保存已经出现过的 ` n ` 。
62
62
``` javascript
63
- var isHappy = function (n , appearedNums ) { // 0
63
+ var isHappy = function (n , appearedNums ) {
64
64
appearedNums || = new Set () // 1
65
65
let sum = 0
66
66
@@ -82,9 +82,9 @@ var isHappy = function (n, appearedNums) { // 0
82
82
};
83
83
```
84
84
85
- ## Complexity
86
- * Time: ` O(log N) ` .
87
- * Space: ` O(log N) ` .
85
+ ## 复杂度
86
+ * 时间: ` O(log N) ` 。
87
+ * 空间: ` O(log N) ` 。
88
88
89
89
## Java
90
90
``` java
@@ -222,71 +222,3 @@ public class Solution
222
222
```
223
223
// Welcome to create a PR to complete the code of this language, thanks!
224
224
```
225
-
226
- ## 问题描述
227
- 编写一个算法来判断一个数 ` n ` 是不是快乐数。
228
-
229
- 「** 快乐数** 」 定义为:
230
-
231
- * 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
232
- * 然后重复这个过程直到这个数变为 ` 1 ` ,也可能是 ** 无限循环** 但始终变不到 ` 1 ` 。
233
- * 如果这个过程 ** 结果为** ` 1 ` ,那么这个数就是快乐数。
234
- * 如果 ` n ` 是 _ 快乐数_ 就返回 ` true ` ;不是,则返回 ` false ` 。
235
-
236
- 难度: ** 容易**
237
-
238
- ### [ Example 1]
239
- ** 输入** : ` n = 19 `
240
-
241
- ** 输出** : ` true `
242
-
243
- ** 解释**
244
- ```
245
- 1**2 + 9**2 = 82
246
- 8**2 + 2**2 = 68
247
- 6**2 + 8**2 = 100
248
- 1**2 + 0**2 + 0**2 = 1
249
- ```
250
-
251
- # 中文题解
252
- ## 思路
253
- 1 . 递归调用` isHappy(n) ` 比较方便,每次只需要生成新的` n ` 作为参数。
254
- 2 . 如果` n ` 已经出现过了,说明进入了循环,` return false ` 。可以用` Set ` 保存已经出现过的` n ` 。
255
-
256
- ## 步骤
257
- 1 . 生成新的` n ` 作为` isHappy() ` 的参数。
258
- ``` javascript
259
- let sum = 0
260
-
261
- for (const digit of n .toString ()) {
262
- sum += Math .pow (Number (digit), 2 )
263
- }
264
-
265
- // omitted code
266
-
267
- return isHappy (sum)
268
- ```
269
-
270
- 2 . 如果` n ` 已经出现过了,说明进入了循环,` return false ` 。可以用` Set ` 保存已经出现过的` n ` 。
271
- ``` javascript
272
- var isHappy = function (n , appearedNums ) {
273
- appearedNums || = new Set () // 1
274
- let sum = 0
275
-
276
- for (const digit of n .toString ()) {
277
- sum += Math .pow (Number (digit), 2 )
278
- }
279
-
280
- if (sum == 1 ) {
281
- return true
282
- }
283
-
284
- if (appearedNums .has (sum)) { // 2
285
- return false
286
- }
287
-
288
- appearedNums .add (sum) // 3
289
-
290
- return isHappy (sum, appearedNums)
291
- };
292
- ```
0 commit comments