Java Solution; 6ms.


  • 0
    S
    public class Solution {
      public boolean isHappy(int n) {
        final Set<Integer> seen = new HashSet<>();
        while (n != 1) {
          int s = 0;
          int m = n;
          while (m != 0) {
            final int d = (m % 10);
            s += (d * d);
            m /= 10;
          }
          if (seen.contains(s)) return false;
          seen.add(s);
          n = s;
        }
        return true;
      }
    }
    

Log in to reply
 

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