当前位置:网站首页>Leetcode 191. number of 1 bits bit operation /easy
Leetcode 191. number of 1 bits bit operation /easy
2022-07-27 15:26:00 【Abcheche】
List of articles
1.Description
Write a function , Input is an unsigned integer ( In the form of a binary string ), Returns the number of digits in its binary expression as ‘1’ The number of ( Also known as Han Ming weight ).
Tips :
Please note that , In some languages ( Such as Java) in , There is no unsigned integer type . under these circumstances , Both input and output will be specified as signed integer types , And should not affect your implementation , Because whether integers are signed or unsigned , Its internal binary representation is the same .
stay Java in , The compiler uses binary complement notation to represent signed integers . therefore , Above Example 3 in , The input represents a signed integer -3.
2.Example
Input :00000000000000000000000000001011
Output :3
explain : Binary string of input 00000000000000000000000000001011 in , There are three of them '1'.
3.Solution
1. Right shift to position
You can move right 32 Times or move right to n be equal to 0 until .
Be careful :Java Medium “>>” yes Count right , Abandon the lowest position , The high bits are filled with sign bits ;
“>>>” Logical shift right : Abandon the lowest position , High level 0 fill .
Therefore, when using the second method, you need to use “>>>”.
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0) {
if((n&1)==1) {
count++;
}
n=n>>>1;
}
return count;
}
}
2. Bit operation optimization
Use n = n&(n-1) You can get n The lowest position of 1 become 0 The number after , Therefore, as long as the operation is carried out circularly, it is enough to count the total number of operations .
public class Solution {
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
n &= n - 1;
ret++;
}
return ret;
}
}
边栏推荐
- 《剑指Offer》 链表反转
- lua学习笔记
- LeetCode 783. 二叉搜索树节点最小距离 树/easy
- Two stage submission and three stage submission
- Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
- 网络设备硬核技术内幕 路由器篇 14 从鹿由器到路由器 (中)
- STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
- Leetcode 244周赛-赛后补题题解【西兰花选手】
- STM32之CAN ---CAN ID过滤器分析
- USB interface electromagnetic compatibility (EMC) solution
猜你喜欢

What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key

基于FIFO IDT7202-12的数字存储示波器

JUC(JMM、Volatile)

Kotlin的基础用法

EMC design scheme of RS485 interface

Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

LeetCode 783. 二叉搜索树节点最小距离 树/easy

Design scheme of digital oscilloscope based on stm32

MySQL 面试40连问,面试官你再问下去我可要翻脸了
随机推荐
STM32 can -- can ID filter analysis
LeetCode 240. 搜索二维矩阵 II medium
魔塔项目中的问题解决
网络设备硬核技术内幕 路由器篇 18 DPDK及其前传(三)
网络设备硬核技术内幕 路由器篇 16 DPDK及其前传(一)
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
LeetCode 456. 132模式 单调栈/medium
一些二进制位操作
Unity3D学习笔记10——纹理数组
LeetCode 190. 颠倒二进制位 位运算/easy
STM32 CAN 通信 滤波设置问题
Digital storage oscilloscope based on FIFO idt7202-12
南山区民政局关于开展2022年度南山区社会组织等级评估工作的通知
Wechat applet realizes music search page
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
Unity 鼠标控制第一人称摄像机视角
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
泛型
基于stm32的数字示波器设计方案
Deveco studio2.1 operation item error