本文主要是介绍338. Counting Bits(Leetcode每日一题-2021.03.03)--抄答案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem
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.
Example1
Input: 2
Output: [0,1,1]
Example2
Input: 5
Output: [0,1,1,2,1,2]
Solution
class Solution {
public:vector<int> countBits(int num) {vector<int> f(num + 1);for (int i = 1; i <= num; i ++ )f[i] = f[i >> 1] + (i & 1);return f;}
};
这篇关于338. Counting Bits(Leetcode每日一题-2021.03.03)--抄答案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!