Sum of Two Integers LeetCode Solution

Problem – Sum of Two Integers

Given two integers a and b, return the sum of the two integers without using the operators + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = 2, b = 3
Output: 5

Constraints:

  • -1000 <= a, b <= 1000

Sum of Two Integers LeetCode Solution in Python

class Solution(object):
def getSum(self, a, b):
    """
    :type a: int
    :type b: int
    :rtype: int
    """
    list=[a,b]
    return sum(list)

Sum of Two Integers LeetCode Solution in C++

class Solution {
public:
    int getSum(int a, int b) {
        int sum = a;
        
        while (b != 0)
        {
            sum = a ^ b;//calculate sum of a and b without thinking the carry 
            b = (a & b) << 1;//calculate the carry
            a = sum;//add sum(without carry) and carry
        }
        
        return sum;
    }
};

Sum of Two Integers LeetCode Solution in Java

public int getSum(int a, int b) {
    return b == 0 ? a : getSum(a^b, (a&b)<<1);
}
Sum of Two Integers LeetCode Solution Review:

In our experience, we suggest you solve this Sum of Two Integers LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Sum of Two Integers LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Sum of Two Integers LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *