YEZAELP

LeetCode: Counting Bits

Nov 3rd, 2021 (edited)
586
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     vector<int> countBits(int n) {
  4.         vector <int> ans;
  5.         ans.push_back(0);
  6.         if(n == 0) return ans;
  7.         ans.push_back(1);
  8.         if(n == 1) return ans;
  9.        
  10.         for(int i=2;i<=n;i++)
  11.             ans.push_back(ans[i >> 1] + (i & 1  == 1));
  12.        
  13.         return ans;
  14.     }
  15. };
RAW Paste Data Copied