Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 18, 2022 12:48
Show Gist options
  • Select an option

  • Save icameling/4359680b795ab83c702bfff3b9c6e095 to your computer and use it in GitHub Desktop.

Select an option

Save icameling/4359680b795ab83c702bfff3b9c6e095 to your computer and use it in GitHub Desktop.
#哈希表 #快乐数
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> nums;
int sum = 0;
while (sum != 1) {
sum = 0;
while (n > 0) {
int k = n % 10;
sum += (k*k);
n /= 10;
}
if (nums.find(n) != nums.end()) {
return false;
}
n = sum;
nums.insert(n);
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment