class Solution {
public:
int next(int n) {
int sum = 0;
while (n) {
sum += (n%10) * (n%10);
n /= 10;
}
return sum;
}
bool isHappy(int n) {
int fast = next(n), slow = n;
while (fast != slow) {
slow = next(slow);
fast = next(next(fast));
}
return fast == 1;
}
};
Space : O(1)
'알고리즘 > leetcode' 카테고리의 다른 글
234. Palindrome Linked List (0) | 2022.07.30 |
---|---|
14. Longest Common Prefix (0) | 2022.07.30 |
189. Rotate Array (0) | 2022.07.24 |
19. Remove Nth Node From End of List (0) | 2022.07.24 |
190. Reverse Bits (0) | 2022.07.24 |