Counting Bits(Difficulty: Medium)
生活随笔
收集整理的這篇文章主要介紹了
Counting Bits(Difficulty: Medium)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Given a non negative integer number?num. For every numbers?i?in the range?0 ≤ i ≤ num?calculate the number of 1's in their binary representation and return them as an array.
Example:
For?num = 5?you should return?[0,1,1,2,1,2].
實現:
1 class Solution { 2 public: 3 vector<int> countBits(int num) { 4 vector<int> result; 5 for (int i = 0; i <= num; i++) 6 { 7 int count = 0; 8 int j = i; 9 while (j) 10 { 11 ++count; 12 j = (j-1) & j; 13 } 14 result.push_back(count); 15 } 16 return result; 17 } 18 };?
轉載于:https://www.cnblogs.com/lrh-xl/p/5462833.html
總結
以上是生活随笔為你收集整理的Counting Bits(Difficulty: Medium)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 远离元宇宙后 扎克伯格财富暴涨3000亿
- 下一篇: 埃塞俄比亚货币 埃塞俄比亚的货币是什么