当前位置:网站首页>Leetcode skimming -- sum of two integers 371 medium
Leetcode skimming -- sum of two integers 371 medium
2022-07-02 15:28:00 【Fire breathing dragon and water arrow turtle】
Discussion and source code of the sum of two integers
The title of the sum of two integers is shown in the figure below , This problem belongs to mathematics and bit operation , It mainly examines the use of bit operation methods and the understanding of mathematical ideas of the topic . The title of this article, the author thought 2 Methods , They are bit operation method and mathematical processing method , Use of median operation method Java Compiling , Calling built-in function methods uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved with the idea of bit operation , First, judge the parameters b Is it 0, If not for 0 Then start the cycle , The parameter a And parameters b Operation and operation , Then move the operation result to the left 1 position , The parameter a And parameters b Assign values to parameters after and operation a, Assign the result after the previous left shift to the parameter b, In this cycle , Until it finally jumps out of the loop condition and returns the parameter a that will do . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int num = (a & b) << 1;
a = a ^ b;
b = num;
}
return a;
}
}

obviously , The effect of our bit operation method is not bad , At the same time, you can also use the method of calling built-in functions to solve , That is, directly set the parameter a And parameters b Call the built-in functions of the system sum() To sum , Get the result directly and return . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def getSum(self, a: int, b: int) -> int:
res = sum((a,b))
return res

As a result Java The efficiency of version bit operation method is good , and Python The speed of calling built-in function methods in version is also ok , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- The past and present lives of visual page building tools
- LeetCode刷题——两整数之和#371#Medium
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- TiDB数据迁移工具概览
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- N皇后问题的解决
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- Download blender on Alibaba cloud image station
- Solution of Queen n problem
- 语义分割学习笔记(一)
猜你喜欢

Learn the method code example of converting timestamp to uppercase date using PHP

17_ Redis_ Redis publish subscription

语义分割学习笔记(一)

19_ Redis_ Manually configure the host after downtime

Mavn builds nexus private server

19_Redis_宕机后手动配置主机

05_ queue

Oracle primary key auto increment

4. Jctree related knowledge learning

损失函数与正负样本分配:YOLO系列
随机推荐
学习使用php实现公历农历转换的方法代码
Tidb cross data center deployment topology
17_Redis_Redis发布订阅
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
List集合&UML图
4. Data splitting of Flink real-time project
12_ Redis_ Bitmap_ command
Real estate market trend outlook in 2022
How to solve the problem of database content output
17_ Redis_ Redis publish subscription
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
. Net core logging system
The past and present lives of visual page building tools
07_哈希
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
TiDB混合部署拓扑
03_線性錶_鏈錶
党史纪实主题公益数字文创产品正式上线
Principles, language, compilation, interpretation