We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
编写一个算法来判断一个数是不是“快乐数”。
一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。
**示例: **
输入: 19 输出: true 解释: 1*1 + 9*9 = 82; 8*8 + 2*2 = 68; 6*6 + 8*8 = 100; 1*1 + 0*0 + 0*0 = 1;
// 实现一 记录已计算过的值,出现重复则进入了循环,返回false function isHappy(num) { let sum = 0, hash = {}; while (!hash[num]) { hash[num] = true; while (num > 0) { let t = num % 10; sum += t * t; num = Math.floor(num / 10); } if (sum === 1) { return true; } num = sum; sum = 0; } return false; } // 实现二 循环链表,快指针和慢指针 function isHappy(num) { let slow = num, fast = calculate(num); while (slow !== fast) { slow = calculate(slow); if (slow === 1) { return true; } fast = calculate(fast); fast = calculate(fast); if (fast === 1) { return true; } } return slow === 1; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
编写一个算法来判断一个数是不是“快乐数”。
一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。
**示例: **
解答
The text was updated successfully, but these errors were encountered: