码迷,mamicode.com
首页 > 其他好文 > 详细

338. Counting Bits

时间:2016-09-25 13:09:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

// 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

标签:

原文地址:http://www.cnblogs.com/blankquiz/p/5905626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!