Skip to content

Instantly share code, notes, and snippets.

@xynophon
Created August 20, 2015 15:51
Show Gist options
  • Select an option

  • Save xynophon/e94d5202bdd8763a0820 to your computer and use it in GitHub Desktop.

Select an option

Save xynophon/e94d5202bdd8763a0820 to your computer and use it in GitHub Desktop.
LeetCode Happy Number
import java.util.*;
public class HappyNumber {
public boolean isHappy(int n) {
HashSet<Integer> sum_set = new HashSet<>();
int sum = 0;
while(n != 1) {
while (n > 0) {
sum += Math.pow(n % 10, 2);
n /= 10;
}
if (sum_set.add(sum) == false) {
return false;
}
n = sum;
sum = 0;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment