mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #867 from Tiffany-yuan/master
update: 0202.快乐数 js版本 使用Set(),求和用reduce
This commit is contained in:
@ -215,6 +215,23 @@ var isHappy = function(n) {
|
|||||||
}
|
}
|
||||||
return n === 1;
|
return n === 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 方法四:使用Set(),求和用reduce
|
||||||
|
var isHappy = function(n) {
|
||||||
|
let set = new Set();
|
||||||
|
let totalCount;
|
||||||
|
while(totalCount !== 1) {
|
||||||
|
let arr = (''+(totalCount || n)).split('');
|
||||||
|
totalCount = arr.reduce((total, num) => {
|
||||||
|
return total + num * num
|
||||||
|
}, 0)
|
||||||
|
if (set.has(totalCount)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
set.add(totalCount);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
Reference in New Issue
Block a user