-
202. Happy Number릿코드(LEETCODE) 2020. 4. 9. 22:01반응형
https://leetcode.com/problems/happy-number/
class Solution { public: int calc(int n) { int sol = 0; while (n) { int cand = n % 10; sol = sol + pow(cand, 2); n = n / 10; } return sol; } bool isHappy(int n) { while (n) { int cand = calc(n); if (1 <= cand && cand <= 9) { if (cand == 1 || cand == 7) return true; else return false; } n = cand; } return false; } };
반응형'릿코드(LEETCODE)' 카테고리의 다른 글
283. Move Zeroes (0) 2020.04.09 53. Maximum Subarray (0) 2020.04.09 804. Unique Morse Code Words (0) 2020.03.05 1252. Cells with Odd Values in a Matrix (0) 2020.03.03 1207. Unique Number of Occurrences (0) 2020.03.03