当前位置:网站首页>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 .
边栏推荐
- Tidb cross data center deployment topology
- 學習使用php實現公曆農曆轉換的方法代碼
- How to test tidb with sysbench
- HUSTPC2022
- 17_ Redis_ Redis publish subscription
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- Tidb hybrid deployment topology
- How to avoid 7 common problems in mobile and network availability testing
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
猜你喜欢

Mavn builds nexus private server

Pytorch 保存tensor到.mat文件

How to find a sense of career direction

02_ Linear table_ Sequence table

02_线性表_顺序表

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

I made an istio workshop. This is the first introduction

Table responsive layout tips

SQL stored procedure

Kibana basic operation
随机推荐
The past and present lives of visual page building tools
4. Data splitting of Flink real-time project
Set set you don't know
Tidb data migration tool overview
TiDB数据迁移工具概览
Yolov5 code reproduction and server operation
10_ Redis_ geospatial_ command
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
Real estate market trend outlook in 2022
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Base64 coding can be understood this way
Solve the problem that El radio group cannot be edited after echo
How to avoid 7 common problems in mobile and network availability testing
Case introduction and problem analysis of microservice
TiDB跨数据中心部署拓扑
LeetCode刷题——去除重复字母#316#Medium
Tidb environment and system configuration check
20_ Redis_ Sentinel mode
02.面向容器化后,必须面对golang
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar