面试150-46(Leetcode202快乐数)

代码:

class Solution {
    public boolean isHappy(int n) {
        Map map = new HashMap<>();
        while(!map.containsKey(n)){
            int num = getHappy(n);
            if(num==1)return true;
            map.put(n,num);
            n = num;
        }
        return false;
    }
    public int getHappy(int n){
        int num = 0;
        while(n>0){
            int x = n%10;
            num += x*x;
            n/=10;
        }
        return num;
    }
}

你可能感兴趣的:(java,leetcode,算法)