We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
输入: 2 输出: [0,1,1]
输入: 5 输出: [0,1,1,2,1,2]
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/counting-bits 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered:
func countBits(num int) []int { bits := make([]int, num+1) for i := 0; i <= num; i++ { bits[i] = countBit(i) } return bits } func countBit(num int) int { var ret int for i := num; i > 0; i /= 2 { if i % 2 == 1 { ret++ } } return ret }
func countBits(num int) []int { m := make([]int, num+1) bits := make([]int, num+1) for i := 0; i <= num; i++ { bits[i] = countBit(i, m) m[i] = bits[i] } return bits } func countBit(num int, m []int) int { var ret int for i := num; i > 0; i /= 2 { if m[i] != 0 { ret += m[i] break } else { if i % 2 == 1 { ret++ } } } return ret }
Sorry, something went wrong.
No branches or pull requests
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
示例 1:
示例 2:
进阶:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/counting-bits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered: