338. Counting Bits 和191. Number of 1 Bits

2023-11-21 03:58
文章标签 number counting 191 bits 338

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

第一题、338. Counting Bits
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:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:

It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
Space complexity should be O(n).
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
方法一、i&(i-1)可以消除最后一个1的特性,代码如下:

vector<int> countBits(int num) {vector<int> ret(num+1,0);for(int i = 1; i <= num; i++)ret[i] = ret[i&(i-1)] + 1; //由于i&(i-1)可以消除最后一个1,所以可以这样做return ret;}

方法二、普通的做法:

vector<int> countBits(int num) {vector<int> total(num+1);for(int i=0;i<=num;i++){total[i]=0;int j=i;while(j){j&=(j-1);total[i]++;}}return total;}

第二题、191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3
题意:统计一个整数的32位的二进制中1的个数,和上题的思路类似

int hammingWeight(uint32_t n) {int count = 0;while(n){n &= (n -1);count++;}return count;}

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



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

相关文章

lintcode Ugly Number II python

Description Ugly number is a number that only have factors 2, 3 and 5. Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12... 利用动态规划的思想 de

JavaScript各种基础对象:(3)包装对象之Number对象

目录 1 Number概述 2 Number对象的属性 3 Number对象实例的方法 3.1 Number.prototype.toString() 3.2 Number.prototype.toFixed() 3.3 Number.prototype.toExponential() 3.4 Number.prototype.toPrecision() 4 自定义方法 1

136. Single Number 找数组只出现一次的数字

Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra

338. Counting Bits 比特位计数

https://leetcode-cn.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 represent

el-input-number 限制输入正整数

vue 页面 限制输入最小值为0 :min="0" <el-input-number v-model="scope.row.num" @change="handleNumChange(scope)" @keydown.enter.prevent style="width: 200px; " :min="0" /> methods 里面限制输入的数字不为小数 使用 Number.isInt

菜鸟学习python之旅---基础入门(3)--- 数字(Number)

Python 支持三种不同的数值类型: 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Python2 的 Long 类型。浮点型(float) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)复数( (comp

数位统计DP——AcWing 338. 计数问题

数位统计DP 定义 数位DP(Digital DP)是一种用于解决与数字的数位相关问题的动态规划算法。它将数字的每一位看作一个状态,通过转移状态来计算满足特定条件的数字个数或其他相关统计信息。 运用情况 统计满足特定条件的数字个数,例如在给定范围内有多少个数字满足某些数位特征。计算数字的某个数位上的特定统计信息,如出现的数字频率等。解决与数字排列、组合相关的问题。 注意事项 数位DP的

PAT 甲级 1038 Recover the Smallest Number 两种思路

这道题的大致思路是每次把能够让拼接后的数字最小的串放在前面,怎么来比较哪个放在前面最小呢?考虑下面两种情况 情况1:321 32 324 情况2:131 13 134 显然应该把第一位最小的放在最前面,第一位相同比较第二位,如果所有位都相同呢?比如上面32和13的情况?可以循环进行比较。比如判断321和32谁放在前面的时候,比较3和3,2和2,3和1得到结果321更小,所以321放在前面。这个循环

解决python查询报%d format: a number is required, not str问题

【问题描述】 1、在一条查询语句中,查询条件既包含了整形又包含了字符串型,执行查询函数后,直接报%d format: a number is required, not str 2、例如 ,如下sql 语句  sql = 'select productid from product where productid = %d and productName = %s' 【实例代码】 #

计数排序(Counting Sort)

计数排序(Counting Sort) 计数排序是一个非基于比较的排序算法,该算法于1954年由 Harold H. Seward 提出。它的优势在于在对一定范围内的整数排序时,快于任何比较排序算法。排序思路: 1.找出待排序数组最大值2.定义一个索引最大值为待排序数组最大值的数组3.遍历待排序数组, 将待排序数组遍历到的值作新数组索引4.在新数组对应索引存储值原有基础上+1 简单代码实现