9 lines C++ solution


  • 4
    S
    class Solution {
    public:
        bool isHappy(int n) {
            vector<int> vec;
            while (n != 1){
              int temp = 0;
              while(n){
                temp += pow(n % 10, 2);
                n /= 10;
              }
              if (find(vec.begin(), vec.end(), temp) != vec.end()) return false;
              else vec.push_back(n = temp);
            }
            return true;
        }
    };

  • 1
    A

    Considering duplicate element in a vector is also a good way.Good!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.