@@ -6,11 +6,11 @@ Write an algorithm to determine if a number `n` is happy.
66
77A ** 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
37371 . It is more convenient to call ` isHappy(n) ` recursively. You only need to generate a new ` n ` as a parameter each time.
38382 . 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
54562 . 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) ` .
0 commit comments