当前位置:网站首页>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
边栏推荐
- MySQL事务简介
- How to view computer graphics card information in win11
- 向线程池提交任务
- win10字体模糊怎么调节
- Is it safe for qiniu business school to send Huatai account? Really?
- Study notes of mattlotlib and Tkinter (I)
- The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
- 二叉树的基本操作与题型总结
- Zhanrui IOT chip 8910dm is certified by Deutsche Telekom
- 360 online enterprise security cloud is open to small, medium and micro enterprises for free
猜你喜欢

Nmap performs analysis of all network segment IP survivals in host detection

二叉树的基本操作与题型总结

【LeetCode】11. Container with the most water

Matlab: 文件夹锁定问题的解决
![[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

Tkinter学习笔记(二)

《物联网开发实战》18 场景联动:智能电灯如何感知光线?(上)(学习笔记)

Classes and objects (1)

高考结束,人生才刚刚开始,10年职场老鸟给的建议

图书管理系统
随机推荐
还在直接用 localStorage 么?该提升下逼格了
Leetcode stack topic summary
How to use the transaction code sat to find the name of the background storage database table corresponding to a sapgui screen field
Use the securecrtportable script function to read data from network devices
go encoding包
Why is the printer unable to print the test page
【Uniapp 原生插件】商米钱箱插件
Explain asynchronous tasks in detail: the task of function calculation triggers de duplication
Why microservices are needed
Tkinter study notes (III)
win10字体模糊怎么调节
Basic operation and question type summary of binary tree
Xshell不小心按到ctrl+s造成页面锁定的解决办法
swiper——单页面多轮播插件冲突解决方案
Tkinter学习笔记(四)
详解异步任务:函数计算的任务触发去重
超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
360 online enterprise security cloud is open to small, medium and micro enterprises for free
Players must read starfish NFT advanced introduction
All features of polymorphism