Skip to content

Latest commit

 

History

History
159 lines (116 loc) · 3.79 KB

File metadata and controls

159 lines (116 loc) · 3.79 KB

338. Counting Bits - 比特位计数

Tags - 题目标签

Description - 题目描述

EN:

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 time O(n) and possibly in a single pass?
  • Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?

ZH-CN:

给你一个整数 n ,对于 0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans 作为答案。

 

示例 1:

输入:n = 2
输出:[0,1,1]
解释:
0 --> 0
1 --> 1
2 --> 10

示例 2:

输入:n = 5
输出:[0,1,1,2,1,2]
解释:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101

 

提示:

  • 0 <= n <= 105

 

进阶:

  • 很容易就能实现时间复杂度为 O(n log n) 的解决方案,你可以在线性时间复杂度 O(n) 内用一趟扫描解决此问题吗?
  • 你能不使用任何内置函数解决此问题吗?(如,C++ 中的 __builtin_popcount

Link - 题目链接

LeetCode - LeetCode-CN

Latest Accepted Submissions - 最近一次 AC 的提交

Language Runtime Memory Submission Time
typescript 80 ms 48.2 MB 2023/04/09 15:26
function countBits(n: number): number[] {
  const ans = new Array(n + 1).fill(0);

  for (let i = 0; i <= n; i++) {
    let temp = i;
    let counter = 0;
    while (temp !== 0) {
      counter += temp & 1;
      temp = temp >> 1;
    }
    ans[i] = counter;
  }

  return ans;
};

My Notes - 我的笔记

计算一个数用二进制表示的1 的个数,只需要对每一位与1相与即可。

function countBits(n: number): number[] {
  const ans = new Array(n + 1).fill(0);

  for (let i = 0; i <= n; i++) {
    let temp = i;
    let counter = 0;
    while (temp !== 0) {
      counter += temp & 1;
      temp = temp >> 1;
    }
    ans[i] = counter;
  }

  return ans;
};