-
202. Happy Number javaLeetCode_Study_Plan/Programming Skills 2022. 9. 20. 11:50
https://leetcode.com/problems/happy-number/?envType=study-plan&id=programming-skills-i
class Solution { public boolean isHappy(int n) { int sum = 0; ArrayList<Integer> arraylist = new ArrayList(); while (true) { System.out.println("start n:" + n); while (n != 0) { int tmp = n % 10; n = n / 10 ; sum += Math.pow(tmp, 2); } System.out.println("calculated sum:" + sum); if (sum == 1) { return true; } else { if (arraylist.contains(sum)){ System.out.println("반복되는 숫자입니다."); break; } else{ arraylist.add(sum); n = sum; sum = 0; System.out.println("다시 반복합니다."); } } } return false; }
'LeetCode_Study_Plan > Programming Skills' 카테고리의 다른 글
589. N-ary Tree Preorder Traversal java (0) 2022.09.20 1790. Check if One String Swap Can Make Strings Equal java (0) 2022.09.20 1502. Can Make Arithmetic Progression From Sequence java (0) 2022.09.19 1822. Sign of the Product of an Array java (0) 2022.09.19 1779. Find Nearest Point That Has the Same X or Y Coordinate java (0) 2022.09.19