首页 > 代码库 > 338. Counting Bits
338. Counting Bits
// My own solution
class Solution {public: vector<int> countBits(int num) { vector<int> ret = {0}; if(num <= 0) return ret; ret.push_back(1); int i = 2; while(i <= num){ int start = (i >> 1) - 1; int mid = i; int end = mid << 1; while(++start < mid && i <= num){ ret.push_back(ret[start]); i++; } mid -= 1; while(i < end && i <= num){ ret.push_back(ret[++mid] + 1); i++; } } return ret; }};
// Others‘ solution
class Solution {public: vector<int> countBits(int num) { vector<int> ret(num + 1, 0); for(int i = 1; i <= num; i++){ ret[i]=ret[i / 2]+(i % 2); } return ret; }};
338. Counting Bits
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。