当前位置:网站首页>338. Counting Bits
338. Counting Bits
2022-06-22 13:15:00 【Sterben_ Da】
338. Counting Bits
Easy
7064333Add to ListShare
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Example 1:
Input: n = 2 Output: [0,1,1] Explanation: 0 --> 0 1 --> 1 2 --> 10
Example 2:
Input: n = 5 Output: [0,1,1,2,1,2] Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101
Constraints:
0 <= n <= 105
Follow up:
- It is very easy to come up with a solution with a runtime of
O(n log n). Can you do it in linear timeO(n)and possibly in a single pass? - Can you do it without using any built-in function (i.e., like
__builtin_popcountin C++)?
class Solution:
def countBits(self, n: int) -> List[int]:
"""
assert Solution().countBits(5) == [0, 1, 1, 2, 1, 2]
assert Solution().countBits(2) == [0, 1, 1]
Reference problem solving ideas : Dynamic programming and bit operation ,dp[i] representative i There are several binary systems 1,
if i The end of the binary bit is 0, be dp[i]=dp[i>>1], Because the end is 0 了 ,1 The number of i The arithmetic shift right is the same
if i The end of the binary bit is 1, be dp[i]=dp[i-1]+1, Because the end is 1 了 , Is to make i-1 Binary bit of 1 Add one at the end of the present 1
Time complexity :O(n), Spatial complexity :O(n)
"""
dp = [0] * (n + 1)
for i in range(1, n + 1):
dp[i] = dp[i - 1] + 1 if i & 1 else dp[i >> 1]
return dp边栏推荐
- SAP 开发Keys 申请SSCR Keys申请
- 8 challenges of BSS application cloud native deployment
- gradle笔记
- postgis 通过 st_dwithin 检索一定距离范围内的 元素
- 769. Max Chunks To Make Sorted
- AcWing 241 楼兰图腾(树状数组详解)
- SAP-ABAP-BAPI_ GOODSMVT_ How to assign values when creating a material voucher Bapi
- SAP ABAP ole core code
- 2017年度总结
- leetcode 第 297 場周賽
猜你喜欢

MySQL_数据处理之查询

Tianyi cloud explores a new idea of cloud native and edge computing integration

MySQL_ Create and manage tables

PHP反序列化&魔术方法

SAP system license viewing application and import

leetcode LCP 10. 二叉树任务调度

130. Surrounded Regions

SAP 开发Keys 申请SSCR Keys申请

2022-6-21os review group linking method

Sequoiadb distributed database may 2022 issue
随机推荐
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
Alicloud disk performance analysis
Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.
Is the dynamic table of Flink created in this way? I use the flick CDC to read MySQL data, write the flick dynamic table, and send
redis修改密码,及启动、查看等操作
SAP ABAP ole core code
PHP deserialization & Magic method
338. Counting Bits
If Tiankeng majors learn IC design by themselves, will any company want it
934. Shortest Bridge
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
Automatically delete the log files in the specified directory and within the specified time
CVPR 2022 | visual language model pre training for scene text detection
leetcode 854. 相似度为 K 的字符串
Leetcode 297 match de la semaine
MySQL_数据处理之查询
240. Search a 2D Matrix II
leetcode 99.恢复二叉搜索树
MySQL_数据处理之增删改
leetcode 1579. 保证图可完全遍历