spider68

set bit in range

Apr 9th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> countBits(int num) {
  4.         if( num == 0)
  5.             return {0};
  6.        
  7.         vector<int> dp(num+1);
  8.         dp[0] = 0;
  9.         dp[1] = 1;
  10.        
  11.         for(int i = 2 ; i<=num ; i++){
  12.             if(!(i&1))
  13.             {
  14.                dp[i] = dp[i>>1];
  15.             } else {
  16.                 dp[i] = dp[i-1] + 1;  
  17.             }            
  18.         }
  19.         return dp;
  20.     }
  21. };
Add Comment
Please, Sign In to add comment