当前位置:网站首页>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 .
边栏推荐
- 14_Redis_乐观锁
- I made an istio workshop. This is the first introduction
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
- Solve the problem that El radio group cannot be edited after echo
- Kibana basic operation
- Semantic segmentation learning notes (1)
- [C language] explain the initial and advanced levels of the pointer and points for attention (1)
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- Download blender on Alibaba cloud image station
- 07_哈希
猜你喜欢
Solution of Queen n problem
List set & UML diagram
损失函数与正负样本分配:YOLO系列
LeetCode刷题——验证二叉树的前序序列化#331#Medium
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
数据分析思维分析方法和业务知识——业务指标
Practical debugging skills
5. Practice: jctree implements the annotation processor at compile time
学习使用php将时间戳转换为大写日期的方法代码示例
List集合&UML图
随机推荐
XML Configuration File
Tidb hybrid deployment topology
如何用 Sysbench 测试 TiDB
N皇后问题的解决
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
记一次面试
MySQL -- Index Optimization -- order by
工程师评测 | RK3568开发板上手测试
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Force deduction solution summary 2029 stone game IX
Force deduction solution summarizes the lucky numbers in 1380 matrix
How to conduct TPC-C test on tidb
10_Redis_geospatial_命令
. Solution to the problem of Chinese garbled code when net core reads files
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
使用 TiUP 部署 TiDB 集群
LeetCode刷题——去除重复字母#316#Medium
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
04.进入云原生后的企业级应用构建的一些思考
02_线性表_顺序表