第一次參加鐵人賽好興奮ㄚㄚㄚㄚㄚ(希望能撐到最後......)
今天的每日一題:338. Counting Bits, 恩...跟除餘(廚餘(?))打好關係就能迎刃而解囉~~
Time Complexity:O(nlog(n))
class Solution {
public:
vector<int> countBits(int n) {
vector<int> ans;
for(int i = 0; i <= n; i++){
ans.push_back(solve(i));
}
return ans;
}
int solve(int n){
int num = 0;
while(n / 2){
num += n % 2;
n /= 2;
}
if(n){
num += 1;
}
return num;
}
};