Skip to content

Commit ec89000

Browse files
committed
Updated 202-happy-number.md
1 parent 1da246e commit ec89000

File tree

2 files changed

+81
-75
lines changed

2 files changed

+81
-75
lines changed

en/1-1000/202-happy-number.md

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Write an algorithm to determine if a number `n` is happy.
66

77
A **happy number** is a number defined by the following process:
88

9-
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
10-
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
11-
* Those numbers for which this process **ends in 1** are happy.
9+
- Starting with any positive integer, replace the number by the sum of the squares of its digits.
10+
- Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
11+
- Those numbers for which this process **ends in 1** are happy.
1212

13-
Return `true` if `n` is _a happy number_, and `false` if not.
13+
Return `true` if `n` is *a happy number*, and `false` if not.
1414

1515
### [Example 1]
1616
**Input**: `n = 19`
@@ -19,10 +19,10 @@ Return `true` if `n` is _a happy number_, and `false` if not.
1919

2020
**Explanation**
2121
```
22-
1**2 + 9**2 = 82
23-
8**2 + 2**2 = 68
24-
6**2 + 8**2 = 100
25-
1**2 + 0**2 + 0**2 = 1
22+
1^2 + 9^2 = 82
23+
8^2 + 2^2 = 68
24+
6^2 + 8^2 = 100
25+
1^2 + 0^2 + 0^2 = 1
2626
```
2727

2828
### [Example 2]
@@ -31,49 +31,52 @@ Return `true` if `n` is _a happy number_, and `false` if not.
3131
**Output**: `false`
3232

3333
### [Constraints]
34-
- `1 <= n <= 2**31 - 1`
34+
- `1 <= n <= 2^31 - 1`
3535

3636
## Intuition
3737
1. It is more convenient to call `isHappy(n)` recursively. You only need to generate a new `n` as a parameter each time.
3838
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.
3939

4040
## Steps
41-
1. Generate a new `n` as the `isHappy(n)` parameter.
42-
```javascript
43-
let sum = 0
44-
45-
for (const digit of n.toString()) {
46-
sum += Math.pow(Number(digit), 2)
47-
}
4841

49-
// omitted code
42+
1. Generate a new `n` as the `isHappy(n)` parameter.
5043

51-
return isHappy(sum)
52-
```
44+
```javascript
45+
let sum = 0
46+
47+
for (const digit of n.toString()) {
48+
sum += Math.pow(Number(digit), 2)
49+
}
50+
51+
// omitted code
52+
53+
return isHappy(sum)
54+
```
5355

5456
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.
55-
```javascript
56-
var isHappy = function (n, appearedNums) { // 0
57-
appearedNums ||= new Set() // 1
58-
let sum = 0
59-
60-
for (const digit of n.toString()) {
61-
sum += Math.pow(Number(digit), 2)
62-
}
6357

64-
if (sum == 1) {
65-
return true
66-
}
67-
68-
if (appearedNums.has(sum)) { // 2
69-
return false
70-
}
71-
72-
appearedNums.add(sum) // 3
73-
74-
return isHappy(sum, appearedNums)
75-
};
76-
```
58+
```javascript
59+
var isHappy = function (n, appearedNums) { // 0
60+
appearedNums ||= new Set() // 1
61+
let sum = 0
62+
63+
for (const digit of n.toString()) {
64+
sum += Math.pow(Number(digit), 2)
65+
}
66+
67+
if (sum == 1) {
68+
return true
69+
}
70+
71+
if (appearedNums.has(sum)) { // 2
72+
return false
73+
}
74+
75+
appearedNums.add(sum) // 3
76+
77+
return isHappy(sum, appearedNums)
78+
};
79+
```
7780

7881
## Complexity
7982
* Time: `O(log N)`.

zh/1-1000/202-happy-number.md

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
**快乐数**」 定义为:
88

9-
* 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
10-
* 然后重复这个过程直到这个数变为 `1`,也可能是 **无限循环** 但始终变不到 `1`
11-
* 如果这个过程 **结果为** `1`,那么这个数就是快乐数。
12-
* 如果 `n`_快乐数_ 就返回 `true` ;不是,则返回 `false`
9+
- 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
10+
- 然后重复这个过程直到这个数变为 `1`,也可能是 **无限循环** 但始终变不到 `1`
11+
- 如果这个过程 **结果为** `1`,那么这个数就是快乐数。
12+
- 如果 `n`*快乐数* 就返回 `true` ;不是,则返回 `false`
1313

1414
### [示例 1]
1515
**输入**: `n = 19`
@@ -37,42 +37,45 @@
3737
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
3838

3939
## 步骤
40-
1. 生成新的`n`作为`isHappy()`的参数。
41-
```javascript
42-
let sum = 0
43-
44-
for (const digit of n.toString()) {
45-
sum += Math.pow(Number(digit), 2)
46-
}
4740

48-
// omitted code
41+
1. 生成新的`n`作为`isHappy()`的参数。
4942

50-
return isHappy(sum)
51-
```
43+
```javascript
44+
let sum = 0
45+
46+
for (const digit of n.toString()) {
47+
sum += Math.pow(Number(digit), 2)
48+
}
49+
50+
// omitted code
51+
52+
return isHappy(sum)
53+
```
5254

5355
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
54-
```javascript
55-
var isHappy = function (n, appearedNums) {
56-
appearedNums ||= new Set() // 1
57-
let sum = 0
58-
59-
for (const digit of n.toString()) {
60-
sum += Math.pow(Number(digit), 2)
61-
}
62-
63-
if (sum == 1) {
64-
return true
65-
}
66-
67-
if (appearedNums.has(sum)) { // 2
68-
return false
69-
}
70-
71-
appearedNums.add(sum) // 3
7256

73-
return isHappy(sum, appearedNums)
74-
};
75-
```
57+
```javascript
58+
var isHappy = function (n, appearedNums) {
59+
appearedNums ||= new Set() // 1
60+
let sum = 0
61+
62+
for (const digit of n.toString()) {
63+
sum += Math.pow(Number(digit), 2)
64+
}
65+
66+
if (sum == 1) {
67+
return true
68+
}
69+
70+
if (appearedNums.has(sum)) { // 2
71+
return false
72+
}
73+
74+
appearedNums.add(sum) // 3
75+
76+
return isHappy(sum, appearedNums)
77+
};
78+
```
7679

7780
## 复杂度
7881
* 时间:`O(log N)`

0 commit comments

Comments
 (0)