非常巧,前后做了两次,写的代码思路、甚至是空行都是一模一样的...
代码:
class Solution { public: bool isHappy(int n) { if (int_set.find(n) != int_set.end()) { return false; } int_set.insert(n); int new_num = 0; for ( ; n != 0; n /= 10) { new_num += ((n % 10) * (n % 10)); } return new_num==1 || isHappy(new_num); } private: unordered_set<int> int_set; };
原文地址:http://blog.csdn.net/stephen_wong/article/details/46591315