LeetCode之Sum of Two Integers
生活随笔
收集整理的這篇文章主要介紹了
LeetCode之Sum of Two Integers
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、題目
?
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.
Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
Subscribe to see which companies asked this question.
?
?
?
?
2、代碼實現
?
public class Solution {public int getSum(int a, int b) {while(b != 0) { int tmp =(a & b) << 1; //a是每次相加,相當于和a = a ^ b;//b相當于是進位b = tmp;} return a; } }?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的LeetCode之Sum of Two Integers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode之Detect Capi
- 下一篇: LeetCode之Maximum Dep