当前位置:网站首页>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 .
边栏推荐
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- Niuke Practice 101
- . Solution to the problem of Chinese garbled code when net core reads files
- Markdown tutorial
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
- Common English abbreviations for data analysis (I)
- PHP method to get the index value of the array item with the largest key value in the array
- 原则、语言、编译、解释
- 14_ Redis_ Optimistic lock
猜你喜欢
Yolo format data set processing (XML to txt)
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
yolo格式数据集处理(xml转txt)
20_ Redis_ Sentinel mode
How does the computer set up speakers to play microphone sound
Practical debugging skills
Markdown tutorial
Download blender on Alibaba cloud image station
随机推荐
Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
08_ 串
20_ Redis_ Sentinel mode
Recommended configuration of tidb software and hardware environment
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
Solve the problem of frequent interruption of mobaxterm remote connection
学习使用php将时间戳转换为大写日期的方法代码示例
数据分析常见的英文缩写(一)
How to test tidb with sysbench
15_Redis_Redis.conf详解
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
How to avoid 7 common problems in mobile and network availability testing
Application and practice of Jenkins pipeline
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Tidb data migration scenario overview
HUSTPC2022
N皇后问题的解决
搭建自己的语义分割平台deeplabV3+
Set set you don't know
Summary of the first three passes of sqli Labs