1classSolution {2public:3boolisHappy(intn) {4set<int>dict;5while(!dict.count(n)){6dict.insert(n);7intsum =0;8while(n >=1){9sum += (n %10) * (n %10);10n /=10;11}12n =sum;13if(sum ==1)14returntrue;15}16returnfalse;17}18};...
n= sum([int(i)**2foriinstr(n)])returnn==1 Runtime:60 ms, faster than8.31% of Python3 online submissions for Happy Number. Memory Usage:14 MB, less than20.99% of Python3 online submissions for Happy Number. classSolution:defisHappy(self, n: int) ->bool:whilen> 6: next=0whilen...
程序如下: 1classSolution {2public:3boolisHappy(intn) {4vector<int>vec;5vec.push_back(n);6while(n!=1)7{8intsum =0;9inttmpN =n;10while(tmpN !=0)11{12sum += (tmpN %10) * (tmpN %10);13tmpN /=10;14}15n =sum;1617if(find(vec.begin(), vec.end(), n) !=vec.end()...
https://leetcode.com/problems/happy-number/ https://leetcode.com/problems/happy-number/discuss/56913/Beat-90-Fast-Easy-Understand-Java-Solution-with-Brief-Explanation https://leetcode.com/problems/happy-number/discuss/56917/My-solution-in-C(-O(1)-space-and-no-magic-math-property-involved-) ...
Example:19 is a happy number 12+ 92= 82 82+ 22= 68 62+ 82= 100 12+ 02+ 02= 1 1classSolution {2public:3boolisHappy(intn) {4inthashtable[800];5//vector<int> iter;6memset(hashtable, -1,sizeof(hashtable));//initilize all elements as -1;7intsum=0;8//int it=0;910while(...
Example: 19 is a happy number 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 Credits: Special thanks to@mithmattand@tsfor adding this problem and creating all test cases. 新出的easy的题目。 1classSolution {2public:3boolisHappy(intn) {4inttmp, dig;5while(n ...
leetcode 202. Happy Number classSolution {public:boolisHappy(intn) {if(n <=0)returnfalse;set<int>res;while(res.count(n) ==0){ res.insert(n);intnum =0;while(n !=0){ num+= (n%10)*(n%10); n= n/10; }if(num ==1)returntrue;...
1 class Solution { 2 public: 3 int calculate(int n) 4 { 5 int res = 0; 6 while(n > 0) 7 { 8 int tmp = n%10; 9 res = res + tmp*tmp; 10 n /= 10; 11 } 12 return res; 13 } 14 bool isHappy(int n) { 15 if(n == 1) return true; 16 unordered_set<int> To...
classSolution{public:boolisHappy(intn){ unordered_set<int> visited;while(n !=1&& visited.find(n) == visited.end()){ visited.insert(n);intval =0;while(n >0){intdigit = n %10; val += digit*digit; n /=10; } n = val; ...
Example: 19 is a happy number 12+ 92= 82 82+ 22= 68 62+ 82= 100 12+ 02+ 02= 1 Solution: Dull. Accepted code: 1@@ -0,0+1,36@@2//1CE, 1AC, dull3#include <unordered_set>4usingnamespacestd;56classSolution {7public:8boolisHappy(intn) {9unordered_set<int>us;10unordered_se...