更新 0202.快乐数

This commit is contained in:
SambacFeng
2021-09-17 23:55:11 +08:00
parent c911b6b65d
commit 3e069c6616

View File

@ -159,26 +159,28 @@ func getSum(n int) int {
javaScript:
```js
function getN(n) {
if (n == 1 || n == 0) return n;
let res = 0;
var isHappy = function (n) {
let m = new Map()
const getSum = (num) => {
let sum = 0
while (n) {
res += (n % 10) * (n % 10);
n = parseInt(n / 10);
sum += (n % 10) ** 2
n = Math.floor(n / 10)
}
return sum
}
while (true) {
// n出现过证明已陷入无限循环
if (m.has(n)) return false
if (n === 1) return true
m.set(n, 1)
n = getSum(n)
}
return res;
}
var isHappy = function(n) {
const sumSet = new Set();
while (n != 1 && !sumSet.has(n)) {
sumSet.add(n);
n = getN(n);
}
return n == 1;
};
// 使用环形链表的思想 说明出现闭环 退出循环
// 方法二:使用环形链表的思想 说明出现闭环 退出循环
var isHappy = function(n) {
if (getN(n) == 1) return true;
let a = getN(n), b = getN(getN(n));