跳至主要內容

202. 快乐数

T4mako算法哈希表数学双指针小于 1 分钟

202. 快乐数

简单

题目描述open in new window

class Solution {
    public boolean isHappy(int n) {
		HashSet<Integer> set = new HashSet<>();
		while (true) {
			if (!set.contains(n)) {
				set.add(n);
				int count = 0;
				while(n != 0) {
					count += Math.pow(n % 10, 2);
					n /= 10;
				}
				if (count == 1)
					return true;
				else
					n = count;
			} else {
				return false;
			}

		}
	}
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5