Code returns wrong answer for input 10? It is working fine for me on the home eclipse.


  • 0
    C
    
    public class Solution {
        public static HashSet<Integer> set = new HashSet<Integer>();
        public static boolean isHappy(int dividend) {
            
                if(set.contains(dividend)){
                    return false;
                }
                set.add(dividend);
                int sum=0;
                int leastSig = 0;
                while(dividend != 0){
                 leastSig = dividend%10;
                 dividend = dividend/10;
                 sum = sum + leastSig*leastSig;
            }
            if(sum == 1){
                return true;
            }
            else{
                return isHappy(sum);
            }
            
        }
    }```

Log in to reply
 

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