当前位置:网站首页>leetcode 中的位运算
leetcode 中的位运算
2022-06-11 22:06:00 【生产队的驴儿】
位运算分类
逻辑位运算符
位于 &
只有两个都为1
才为1
位 或 |
只要有一个为1
就为1

异或 ^
只有不相同 才为 1,
否则 为 0。
按位取反

位移运算符
左移 <<
左移一位可以看做 该数 乘2了
右移 >>
左移一位可以看做 该数 除2了
但是注意 如果 该数 是正数, 左边补0
如果 该数 是负数,左边补1.

实战leetcode
https://leetcode.cn/problems/add-two-integers/
2235 两数求和
代码
class Solution{
public int sum (int num1, int num2){
if (num2 == 0){
return num1;
}
int add_xor = num1 ^ num2; // sum result without carry
int add_carry = (num1 & num2) << 1; // summ reult only about carry
return sum(add_xor,add_carry);
}
}
Reference
https://www.bilibili.com/video/BV1YT4y117AH/?spm_id_from=333.788.recommend_more_video.0&vd_source=8d8fef6cad2875d6b6b4c08c3a9ac66d
边栏推荐
- go os模块
- Use the securecrtportable script function to read data from network devices
- Simple example of logistic regression for machine learning
- Inner join execution plan changed
- 超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- [Chongqing Guangdong education] college physics of Xiangtan University: mechanical and thermal reference materials
- 【LeetCode】11. Container with the most water
- One question per day -- verifying palindrome string
- MySQL事务简介
猜你喜欢

The shortcomings of the "big model" and the strengths of the "knowledge map"

Simple example of logistic regression for machine learning

Zhanrui IOT chip 8910dm is certified by Deutsche Telekom

仅需三步学会使用低代码ThingJS与森数据DIX数据对接

Classes and objects (4)

【LeetCode】11. Container with the most water
![[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?](/img/cd/e7ffecbee13596f2298ee8c0a5b873.jpg)
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?

超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录

Popular science | what are the types of NFT (Part 1)
![[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials](/img/0a/58df3fd771d58c66245397d131fa53.png)
[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials
随机推荐
Go OS module
STM32开发笔记112:ADS1258驱动设计——读寄存器
How to view the installation date of the win system
Example of using zypper command
Why microservices are needed
机器学习之线性回归简单实例
Huawei equipment configuration hovpn
6.项目上线
go io模块
go os模块
3.2 测试类的命名规则
How to adjust the font blur of win10
How to view computer graphics card information in win11
Uncover the secret of the popular app. Why is it so black
MySQL事务简介
向线程池提交任务
【学术相关】申请审核制下,到双一流大学读博的难度有多大?
win11怎么看电脑显卡信息
剑指offer数组题型总结篇
启牛商学院送华泰账户安不安全?真的吗