본문 바로가기
문제풀이

Leetcode Happy Number JAVA

by AndoneKwon 2021. 1. 4.
class Solution {
    public boolean isHappy(int n) {
        char[] arr = Integer.toString(n).toCharArray();
        HashSet<String> set = new HashSet<>();
        String temp;
        int answer=0;
        do{
            int tempInt=0;
            for(char item : arr){
                tempInt+=Math.pow(Integer.parseInt(Character.toString(item)),2);
            }
            temp = Integer.toString(tempInt);
            arr = temp.toCharArray();
            if(set.contains(temp)){
                return false;
            }
            set.add(temp);
            if(temp.compareTo("1")==0)
                return true;
        }while(true);
    }
}

'문제풀이' 카테고리의 다른 글

Leetcode MinStack JAVA  (0) 2021.01.05
Leetcode Climbing Stairs JAVA  (0) 2021.01.04
Leetcode Best Time to Buy and Sell Stock JAVA  (0) 2021.01.04
Leetcode Number of 1 Bits JAVA  (0) 2021.01.04
Leetcode Intersection of Two Arrays II JAVA  (0) 2020.12.28