371. Sum of Two Integers

时间:2020-04-24 18:14:15   收藏:0   阅读:45
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
    }
}

 

原文:https://www.cnblogs.com/johnnyzhao/p/12768969.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!