您的位置:首页 > 移动开发

Leetcode 202. Happy Number

2017-01-09 13:17 423 查看
public class Solution {
public boolean isHappy(int n) {
HashSet<Integer> hs = new HashSet<Integer>();
// if repeat, HashSet.add() will return false
while (hs.add(n)) {
// sum the squares of n's digits
int sum = 0;
while (n > 0) {
sum += (n % 10)*(n % 10);
n /= 10;
}
if (sum == 1) return true;
n = sum;
}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: