@@ -6,11 +6,11 @@ Write an algorithm to determine if a number `n` is happy.
6
6
7
7
A** happy number** is a number defined by the following process:
8
8
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.
12
12
13
- Return` true ` if` n ` is_ a happynumber _ , and` false ` if not.
13
+ Return` true ` if` n ` is* a happynumber * , and` false ` if not.
14
14
15
15
###[ Example 1]
16
16
** Input** :` n = 19 `
@@ -19,10 +19,10 @@ Return `true` if `n` is _a happy number_, and `false` if not.
19
19
20
20
** Explanation**
21
21
```
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
26
26
```
27
27
28
28
###[ Example 2]
@@ -31,49 +31,52 @@ Return `true` if `n` is _a happy number_, and `false` if not.
31
31
** Output** :` false `
32
32
33
33
###[ Constraints]
34
- - ` 1 <= n <= 2** 31 - 1 `
34
+ - ` 1 <= n <= 2^ 31 - 1 `
35
35
36
36
##Intuition
37
37
1 . It is more convenient to call` isHappy(n) ` recursively. You only need to generate a new` n ` as a parameter each time.
38
38
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.
39
39
40
40
##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
- }
48
41
49
- // omitted code
42
+ 1 . Generate a new ` n ` as the ` isHappy(n) ` parameter.
50
43
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
+ ```
53
55
54
56
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
- }
63
57
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
+ ```
77
80
78
81
##Complexity
79
82
* Time:` O(log N) ` .