- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
61 lines (58 loc) · 1.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* # 202. Happy Number
*
* Write an algorithm to determine if a number is "happy".
*
* A happy number is a number defined by the following process: Starting with any positive integer,
* replace the number by the sum of the squares of its digits, and repeat the process until
* the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
* Those numbers for which this process ends in 1 are happy numbers.
*
* ## Example
*
* ```bash
* Input: 19
* Output: true
* Explanation:
* 1^2 + 9^2 = 82
* 8^2 + 2^2 = 68
* 6^2 + 8^2 = 100
* 1^2 + 0^2 + 0^2 = 1
* ```
*
* ### Thinkings
*
* - 要报所有的推导结果环境,一旦发现已经存在,即除了死循环,立即退出;
* 一开始以为这题很简单,也没仔细考虑;使用了 Array.reduce() 和 While 控制语句完事;跑测试用例时才发现,第二条用例就没有通过。问题出在时间复杂度上,这里必须对死循环进行处理。
*/
exporttypeSolution=(n: number)=>boolean;
/**
* @date 2020.06.28 15:18
* @time
* @space
* @runtime 92 ms, faster than 100.00%
* @memory 36.9 MB, less than 100.00%
* @runtime_cn 88 ms, faster than 25.00%
* @memory_cn 35.9 MB, less than 100.00%
*/
exportconstisHappy=(n: number): boolean=>{
letresult: number=n;
constcache=newSet<number>();
consthappify=(n: number): number=>{
letsum=0;
constlen=String(n).length;
for(leti=0;i<len;i++){
sum+=(n%10)**2;
n=(n-n%10)/10;
}
returnsum;
};
while(result!==1){
result=happify(result);
if(cache.has(result)){
returnfalse;
}
cache.add(result);
}
returntrue;
};