当前位置:网站首页>Sword finger offer | number of 1 in binary
Sword finger offer | number of 1 in binary
2022-07-25 15:53:00 【Little Yapi】
Enter an integer n , Output the number 32 In bit binary representation 1 The number of . Negative numbers are represented by complements .
Method 1:
Method 1: take 32 Every one is associated with 1 Meet each other . Statistics 1 The number of .
public int NumberOf1(int n) {
int cnt = 0;
// Method 1: take 32 Every one is associated with 1 Meet each other . Statistics 1 The number of .
// 1 Of 32 Base number : 0000...1 1<<i: By gradually moving left , bring 1 Can and n Every phase of is related to
for(int i=0;i<32;i++){
if((n & (1<<i)) != 0){
cnt++;
}
}
return cnt;
}
Method 2:
Method 2: Gradually n On the right side of the 1 Turn into 0.
public int twoNumberOf1(int n) {
int cnt = 0;
// Method 2: Gradually n On the right side of the 1 Turn into 0.
// n = n & (n-1); take n The most the right side 1 Turn into 0
while (n != 0){
cnt ++;
n = n & (n-1);
}
return cnt;
}
Method 3:
Method 3: The digital n Turn into 32 Bit string form .
public int threeNumberOf1(int n) {
int cnt = 0;
// Method 3: The digital n Turn into 32 Bit string form .
String numStr = Integer.toBinaryString(n);
for(int i=0;i<numStr.length();i++){
if(numStr.charAt(i)=='1'){
cnt++;
}
}
return cnt;
}
边栏推荐
- LeetCode - 362 敲击计数器(设计)
- 2600 pages in total! Another divine interview manual is available~
- Leetcode - 707 design linked list (Design)
- MySQL优化总结二
- Games101 review: 3D transformation
- Understand "average load"
- Data system partition design - partition and secondary index
- BSC智能链合约模式系统开发详情
- P4552 differential
- Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
猜你喜欢

Leetcode - 225 implements stack with queue

HDD Hangzhou station · harmonyos technical experts share the features of Huawei deveco studio

Leetcode - 362 knock counter (Design)

Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel

LeetCode - 379 电话目录管理系统(设计)

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

P4552 differential

【服务器数据恢复】HP EVA服务器存储意外断电导致RAID信息丢失的数据恢复案例

Leetcode - 622 design cycle queue (Design)

CVPR 2022 | in depth study of batch normalized estimation offset in network
随机推荐
Games101 review: Transformation
Reasons for data format conversion when matlab reads the displayed image
2019 Zhejiang race c-wrong arrangement, greedy
30 lines write the concurrency tool class yourself (semaphore, cyclicbarrier, countdownlatch)
Understanding of this object
Deadlock gossip
如何实现页面包含
LeetCode - 362 敲击计数器(设计)
LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
Where is there a demo to set up the flex CDC to draw the number of MySQL?
mouseover和mouseenter的区别
Icpc2021 Kunming m-violence + chairman tree
Pytorch学习笔记--Pytorch常用函数总结1
# JWT 图解
ZOJ - 4114 flipping game DP, reasonable state representation
Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
MySQL—常用SQL语句整理总结
兆骑科创海内外高层次创新创业人才服务平台,双创成果转化平台
如何解决跨域问题
PAT甲级1151 LCA in a Binary Tree (30 分)