gha890826

LeetCode - 202. Happy Number

Mar 21st, 2019 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool isHappy(int ans) {
  4.         int count=0;
  5.         while(ans>1&&count<10)
  6.         {
  7.             int sum=0;
  8.             while(ans>0)
  9.             {
  10.                 sum+=(ans%10)*(ans%10);
  11.                 //cout<<"***temp%10 "<<temp%10<<endl;
  12.                 ans/=10;
  13.             }
  14.             ans=sum;
  15.             count++;
  16.             //cout<<"***"<<ans<<endl;
  17.         }
  18.  
  19.         if(ans==1)
  20.             return true;
  21.         else
  22.             return false;
  23.     }
  24. };
Add Comment
Please, Sign In to add comment