371. Sum of Two Integers

package LeetCode_371

/**
 * 371. Sum of Two Integers
 * https://leetcode.com/problems/sum-of-two-integers/description/
 * https://www.polarxiong.com/archives/LeetCode-371-sum-of-two-integers.html
 * */
class Solution {
    fun getSum(a: Int, b: Int): Int {
        var carry = a and b
        var result = a xor b
        while (carry != 0) {
            val shiftCarry = carry shl 1
            carry = result and shiftCarry
            result = result xor shiftCarry
        }
        return result
    }
}

 

posted @ 2020-04-24 17:37  johnny_zhao  阅读(79)  评论(0编辑  收藏  举报