Created
July 18, 2022 12:48
-
-
Save icameling/4359680b795ab83c702bfff3b9c6e095 to your computer and use it in GitHub Desktop.
#哈希表 #快乐数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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