knakul853

Untitled

Jul 19th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> countBits(int num) {
  4.        vector<int>ans;
  5.       int x=1;
  6.       int pow = 1;                       //2*m+x
  7.       ans.push_back(0);
  8.       for(int i=1;i<=num;i++)
  9.       {
  10.         if(i==pow)
  11.         {
  12.           ans.push_back(1);
  13.           x = 1;
  14.           pow=pow<<1;
  15.         }
  16.         else
  17.         {
  18.           ans.push_back(ans[x]+1);
  19.           x++;
  20.         }
  21.       }
  22.       return ans;
  23.     }
  24.  
  25. };
Add Comment
Please, Sign In to add comment