[LeetCode]Happy Number(Java)

来源:互联网 发布:投影互动软件破解 编辑:程序博客网 时间:2024/04/30 14:28

先贴我自己的代码递归的思想,把用过的数都记下来

public class Solution {    public boolean isHappy(int n) {        if(n == 1){            return true;        }else{            List<Integer> stack = new ArrayList<Integer>();            stack.add(n);            return judge(n,stack);        }    }    public boolean judge(int n,List<Integer> stack){        if(n == 1){            return true;        }        List<Integer> split = new ArrayList<Integer>();        int newNum = 0;        while(n!=0){                        int test = n%10;            //System.out.println(test);            split.add(test);            //System.out.println(split);            n = n/10;        }        for (Integer key : split){            newNum += Math.pow(key,2);        }        if(stack.contains(newNum)){            return false;        }else{            stack.add(newNum);            //return isHappy(newNum);            return judge(newNum,stack);        }    }}

2016/8/27


0 0