国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Sum of Two Integers

2019-11-08 20:16:43
字體:
來源:轉載
供稿:網友

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.

Python solution with no "+-*/%", completely bit manipulation guaranteedclass Solution(object):    def getSum(self, a, b):        """        :type a: int        :type b: int        :rtype: int        """        # 32 bits integer max        MAX = 0x7FFFFFFF        # 32 bits interger min        MIN = 0x80000000        # mask to get last 32 bits        mask = 0xFFFFFFFF        while b != 0:            # ^ get different bits and & gets double 1s, << moves carry            a, b = (a ^ b) & mask, ((a & b) << 1) & mask        # if a is negative, get a's 32 bits complement positive first        # then get 32-bit positive's Python complement negative        return a if a <= MAX else ~(a ^ mask)
class Solution {public:    int getSum(int a, int b) {        int sum=a;        while(b!=0){            sum = a^b;      //add different bits            b = (a&b)<<1;  //left same bits            a = sum;        }        return sum;    }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无为县| 元阳县| 泸定县| 屏东县| 威信县| 中江县| 台南市| 亚东县| 即墨市| 陇西县| 平远县| 涞水县| 古蔺县| 腾冲县| 锦州市| 淮南市| 内江市| 江陵县| 宿松县| 定襄县| 库车县| 达拉特旗| 吴桥县| 八宿县| 河池市| 繁昌县| 泸水县| 吉木萨尔县| 弋阳县| 得荣县| 达日县| 大化| 图们市| 宜章县| 吉安市| 灵石县| 都匀市| 铁岭县| 大足县| 光泽县| 三江|