LTE error on my java code


  • 0
    H
    public class Solution {
        public boolean isHappy(int n) {
            ArrayList<Integer> visited = new ArrayList<Integer>();
            while(n!= 1) {
                if(visited.contains(n)) {
                    return false;
                }
                visited.add(n);
                int tmp = 0;
                while(n!=0) {
                    int dig = n%10;
                    tmp += dig*dig;
                }
                n = tmp;
            }
            return true;
        }
    }

  • 0
    B

    Replace your ArrayList with HashSet.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.