Happy Number

我觉得这个算法比较陌生,对我不是简单题目

public class Solution {
    public boolean isHappy(int n) {
        // 这个叫“模拟”的算法是什么http://meetqun.com/thread-8853-1-1.html
        HashSet <Integer> hs = new HashSet<Integer>();
        while(!hs.contains(n)){
            hs.add(n);
            int k=0;
            while(n>0){
                int last = n%10;
                n /= 10;
                k += last*last;
            }
            if(k==1) return true;
            n =k;
        }
        return false;
    }
}

 

posted @ 2015-04-24 12:54  世界到处都是小星星  阅读(149)  评论(0编辑  收藏  举报