202. Happy Number

public class Solution {
    public boolean isHappy(int n) {
        boolean res=false;
        Map<Integer,Integer> mp=new HashMap<Integer,Integer>();
        int temp=n;
        while(true)
        {
            int sum=0;
            while(temp!=0)
            {
                sum+=(temp%10)*(temp%10);
                temp/=10;
            }
            
            if(sum==1)
            {
                return true;
            }
            if(mp.containsKey(sum))
            {
                return false;
            }
            else
            {
                mp.put(sum,1);
                temp=sum;
            }

        }
    }
}

 

posted @ 2016-04-03 10:28  阿怪123  阅读(79)  评论(0)    收藏  举报