IT常识
技术 Python PHP JavaScript IOS Android Java 数据库 资源 公众号 代码片段 github
  • IT常识
  • web服务器

LeetCode之371. Sum of Two Integers

Posted 2020-08-15

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode之371. Sum of Two Integers相关的知识,希望对你有一定的参考价值。

----------------------------------

 

使用位运算实现加法:

a^b 加不同部分
(a&b)<<1 加相同部分
递归相加

 

AC代码:

public class Solution {
    public int getSum(int a, int b) {
        if(b==0) return a;
        int t1=a^b;
        int t2=(a&b)<<1;
        return getSum(t1,t2);
    }
}

 

题目来源: https://leetcode.com/problems/sum-of-two-integers/

以上是关于LeetCode之371. Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(371) Sum of Two Integers

LeetCode 第 371 题 (Sum of Two Integers)

Leetcode - 371. Sum of Two Integers

leetcode-371-Sum of Two Integers

LeetCode 371. Sum of Two Integers

Leetcode 371. Sum of Two Integers JAVA语言

(c)2006-2024 SYSTEM All Rights Reserved IT常识