当前位置:网站首页>Leetcode sword finger offer brush questions - day 21
Leetcode sword finger offer brush questions - day 21
2022-07-05 23:05:00 【DEGv587】
Leetcode The finger of the sword Offer How to brush questions :
The finger of the sword Offer 15. Binary 1 The number of
solution : An operation
public class Solution {
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
ret += n & 1;
n >>>= 1;
}
return ret;
}
}
public class Solution {
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
ret++;
n &= (n - 1);
}
return ret;
}
}
The finger of the sword Offer 65. Do not add, subtract, multiply or divide
solution : An operation
Non carry and :a ^ b
Carry sum :(a & b) << 1
and : Carry sum + Non carry and = (a ^ b) + ((a & b) << 1)
loop , When carry is 0 Exit from time
class Solution {
public int add(int a, int b) {
while (b != 0) {
int c = (a & b) << 1;// Carry sum
a ^= b;// Non carry and
b = c;// take c Assign a value to b, here b Is carry and
}
return a;
}
}
边栏推荐
- LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
- 判断二叉树是否为完全二叉树
- Debian 10 installation configuration
- Element positioning of Web Automation
- My experience and summary of the new Zhongtai model
- Tensor attribute statistics
- Activate function and its gradient
- 【Note17】PECI(Platform Environment Control Interface)
- 二叉树递归套路总结
- 傅里叶分析概述
猜你喜欢
The difference between MVVM and MVC
30 optimization skills about mysql, super practical
February 13, 2022-4-symmetric binary tree
Error when LabVIEW opens Ni instance finder
I closed the open source project alinesno cloud service
基于STM32的ADC采样序列频谱分析
Arduino 测量交流电流
视频标准二三事
Three. JS VR house viewing
Detailed explanation of pointer and array written test of C language
随机推荐
Simple and beautiful method of PPT color matching
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
利用LNMP实现wordpress站点搭建
媒体查询:引入资源
Matlab smooth curve connection scatter diagram
Go语言实现原理——锁实现原理
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Three. JS VR house viewing
My experience and summary of the new Zhongtai model
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Methods modified by static
VIM tail head intercept file import
CJ mccullem autograph: to dear Portland
First, redis summarizes the installation types
TOPSIS code part of good and bad solution distance method
Openresty ngx Lua regular expression
Sum of two numbers, sum of three numbers (sort + double pointer)
Douban scoring applet Part-2
透彻理解JVM类加载子系统