[LeetCode]338. Counting Bits
来源:互联网 发布:mac 树状笔记 编辑:程序博客网 时间:2024/04/20 11:54
Problem Description
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.
[https://leetcode.com/problems/counting-bits/]
思路
DP。
对于一个数,如果他是偶数,那它二进制后含有‘1’的个数与左移一位之后含有‘1’的个数相同,如果是奇数,则加1。
例如:
偶数1100110中含有的1与110011相同
奇数1100111中含有的1比110011多1.
Code
package q338;public class Solution { public int[] countBits(int num) { int[] ans=new int[num+1]; ans[0]=0; for(int i=1;i<=num;i++){ ans[i]=ans[i-1]>>1+i%2; } return ans; }}
0 0
- [leetcode] 338. Counting Bits
- leetcode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode#338. Counting Bits
- [LeetCode] 338. Counting Bits
- LeetCode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode *** 338. Counting Bits
- (leetcode) 338. Counting Bits
- #leetcode#338. Counting Bits
- LeetCode 338. Counting Bits
- leetcode 338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode-338. Counting Bits
- leetcode-338. Counting Bits
- Leetcode 338. Counting Bits
- LeetCode 338. Counting Bits
- 几个有关iOS的几个常见问题-----RunTime
- linux (centos6.3) 安装mysql
- xftp 连接Linux中文乱码的解决方法
- 链表设置
- RED HAT5安装VMWARE TOOLS报错解决办法
- [LeetCode]338. Counting Bits
- mysql5.7缺少的两个库文件->资源(binary_log_funcs.h)(binary_log_types.h)
- CSU 1716(字符串)
- 利用PyMysql访问windows下的MySql数据库
- 数学表达式配对栈
- Layout
- 关于无限级理解,及其树形结构
- 队列
- Java如何实现WebSocket通讯问题(转载)