Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
AhkunTa
Arithmetic
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Arithmetic
/
LeetCode
/
338_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
51 lines (42 loc) · 1.76 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
338_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
51 lines (42 loc) · 1.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
// 示例 1:
// 输入: 2
// 输出: [0,1,1]
// 示例 2:
// 输入: 5
// 输出: [0,1,1,2,1,2]
// 进阶:
// 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗?
// 要求算法的空间复杂度为O(n)。
// 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。
// 神仙算法
// 只能想出暴力法
// 利用位与的性质 &
// 1 & 2 = 01 & 10 = 00
// 2 & 3 = 10 & 11 = 10
// 3 & 4 = 11 & 10 = 10
// i & (i-1) 前一位与后一位相与 最后一位永远为0
// i & (i - 1)可以去掉i最右边的一个1(如果有),因此 i & (i - 1)是比 i 小的,而且i & (i - 1)的1的个数已经在前面算过了,所以i的1的个数就是 i & (i - 1)的1的个数加上1
var countBits = function (num) {
let res = [0];
for (let i = 1; i <= num; i++) {
res[i] = res[i & (i - 1)] + 1;
}
return res;
};
// 方法二:i >> 1会把最低位去掉,因此i >> 1 也是比i小的,同样也是在前面的数组里算过。当 i 的最低位是0,则 i 中1的个数和i >> 1中1的个数相同;当i的最低位是1,i 中1的个数是 i >> 1中1的个数再加1
var countBits = function (num) {
for (let i = 0; i <= num; i++) {
res[i] = res[i >> 1] + (i & 1); //注意i&1需要加括号
}
return res;
};
// 动态规划
var countBits = function (num) {
let dp = [0];
for (let i = 0; i <= num / 2; i++) {
dp[i * 2] = dp[i];
if (i * 2 + 1 <= num) dp[i * 2 + 1] = dp[i] + 1;
}
return dp;
};
You can’t perform that action at this time.