leetcode--快乐数

题目:一个数字
19
1+81 = 82
64 + 4 = 68
36 + 64 = 100
1 + 0 = 1
就是快乐数

思路:
碰到逐位的就取余。相信自己没问题
代码:

private int getNext(int n) {
	int total = 0;
	while (n > 0) {
		int yushu = n % 10;
		n /= 10;
		total += yushu * yushu;
	}
	return total;
}
private boolean isHappy(int n) {
	Set<Integer> set = new HashSet();
	while (n != 1 && !set.contains(n)) {
		set.add(n);
		n = getNext(n);
	}
	return n == 1;
}

你可能感兴趣的:(leetcode,算法,职场和发展)