Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- // 202. Happy Number
- public boolean isHappy(int n) {
- Set<Integer> set = new HashSet<>();
- while (!set.contains(n)) {
- set.add(n);
- n = getSum(n);
- if (n == 1)
- return true;
- }
- return false;
- }
- private int getSum(int n) {
- int sum = 0;
- while (n > 0) {
- sum += (n % 10) * (n % 10);
- n = n / 10;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement