LeetCode338. Counting Bits

2023-12-19 13:36
文章标签 counting bits leetcode338

本文主要是介绍LeetCode338. Counting Bits,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

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++)?

二、题解

class Solution {
public:int hammingWeight(int n) {n = (n & 0x55555555) + ((n >> 1) & 0x55555555);n = (n & 0x33333333) + ((n >> 2) & 0x33333333);n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff);n = (n & 0x0000ffff) + ((n >> 16) & 0x0000ffff);return n;}vector<int> countBits(int n) {vector<int> res(n+1,0);for(int i = 0;i <= n;i++){res[i] = hammingWeight(i);}return res;}
};

这篇关于LeetCode338. Counting Bits的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/512382

相关文章

POJ 2386 Lake Counting(DFS)

题目: http://poj.org/problem?id=2386 题解: 遍历一次,遇到w,一次DFS后,与此w连接的所有w都被替换成‘ . ’,直到遍历完图中不再存在w为止,总共进行DFS的次数就是答案了。 代码: #include<stdio.h>int N,M;char map[110][110];void dfs(int x,int y){map[x][y]='

[LeetCode] 338. Counting Bits

题:https://leetcode.com/problems/counting-bits/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 repres

[LeetCode] 190. Reverse Bits

题:https://leetcode.com/problems/reverse-bits/ 题目大意 将32位的数,二进制翻转。 解题思路 解法和 将int a =123,翻转的思路 相同。 int b= 0;while(a>0){b = b*10 + a %10;a /=10;} 将新数整体左移一位,然后 取每个数的第i位置,将该位放到 新数的最低位。循环32次遍可以得到翻转。

《Zero-Shot Object Counting》CVPR2023

摘要 论文提出了一种新的计数设置,称为零样本对象计数(Zero-Shot Object Counting, ZSC),旨在测试时对任意类别的对象实例进行计数,而只需在测试时提供类别名称。现有的类无关计数方法需要人类标注的示例作为输入,这在许多实际应用中是不切实际的。ZSC方法不依赖于人类标注者,可以自动操作。研究者们提出了一种方法,可以从类别名称开始,准确识别出最佳的图像块(patches),用

Swift 3.0 学习 -- 计算字符数量 (Counting Characters)

在swift2.0的时候,可以通过调用全局countElements函数,并将字符串作为参数进行传递,可以获取该字符串的字符数量。如下: let unusualMenagerie = "Koala, Snail, Penguin, Dromedary"print("unusualMenagerie has \(countElements(unusualMenagerie)) charact

leetcode338 Counting Bits Java

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. Example: Fo

UVA 11401 Triangle Counting

中文详解请访问我的博客:http://xiaoshig.sinaapp.com/?p=128 You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two

sdut2610--Boring Counting(二分+划分树)

Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the

win7 x64 PL/SQL连接Make sure you have the 32 bits Oracle Cient italled.问题解决

一、当前环境 oracle11g win7 x64 PLSQL Developer 9.0.0.1601 使用plsql登录出现提示 Initialization error Could not initialize "F:\app\Administrator\product\11.2.0\dbhome_ 1\bin\oci... Make sure you have the 32

论文笔记 A Large Contextual Dataset for Classification,Detection and Counting of Cars with Deep Learning

ECCV 2016的文章,首先建立了一个从上到下照的车辆影像数据集(即鸟瞰视角),并提出ResCeption神经网络进行训练,进一步建立residual learning with Inception-style layers,进行车辆数目的计算。该方法为车辆数目的计算的一种新方式:通过定位和密度估计方法。对于新的场景或新的目标计数也同样适用。 文章主要关注3个任务点:(1)两类的分类问题(2)