问题描述:

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

解题思路:

我们知道,机器内部是使用二进制表示数字,其中整数用补码表示。既然不能直接调用系统实现好的十进制“+、-”运算符,那就可以考虑自己实现补码加法,对于使用补码表示的整数有以下性质:[X]补 + [Y]补 = [X+Y]补

示例代码:

class Solution {
public:
int getSum(int a, int b) {
int result = 0;
int flag = 0, jie = 1;
for (int i = 0; i < 32; i++){
int lastA = a & 1;
int lastB = b & 1;
if (flag == 1) {
if (lastA == 1 && lastB == 1) {
flag = 1;
result |= jie;
}
else if (lastA == 0 && lastB == 0) {
flag = 0;
result |= jie;
}
else {
flag = 1;
}
}
else {
if (lastA == 1 && lastB == 1) {
flag = 1;
}
else if (lastA == 0 && lastB == 0) {
flag = 0;
}
else {
flag = 0;
result |= jie;
}
}
jie *= 2;
a = a >> 1;
b = b >> 1;
}
if (flag) {
result |= jie;
}
return result;
}
};

371. Sum of Two Integers -- Avota的更多相关文章

  1. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  2. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  3. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  4. LeetCode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. Leetcode 371: Sum of Two Integers(使用位运算实现)

    题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  6. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  7. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  8. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  9. 【leetcode】371. Sum of Two Integers

    题目描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

随机推荐

  1. bzoj1912

    由于k只有2,所以我们分类讨论显然当k=1时,我们只要连一条最长的路径即可就是树的直径L少走了L-1条边如果k=2时,我们再次连边成环后如果成环路径与上一次的最长路径没有相同的边,那少走的边数是路径长 ...

  2. WordPress Design Approval System插件‘step’参数跨站脚本漏洞

    漏洞名称: WordPress Design Approval System插件‘step’参数跨站脚本漏洞 CNNVD编号: CNNVD-201309-084 发布时间: 2013-09-11 更新 ...

  3. 搜索提示時jquery的focusout和click事件沖突問題完美解决

          在主流的搜索引擎上搜索時,輸入內容,往往會彈出智能提示.輸入框为input,智能提示區域为suggest.接下來一般有兩種操作:        1.選擇某一提示,則把內容复制到input中 ...

  4. Light OJ 1036 - A Refining Company

    题目大意: 一个m*n的矩阵,里面有两种矿物质铀和镭,现在要把铀和镭运送到指定位置.北边是炼镭厂,西边是了炼铀厂. 现在要建立传送带,传送带有两种,一种是从东到西,另一种是从南到北,传送带不能交叉,并 ...

  5. oracle查询语句2【转载】

    本文使用的实例表结构与表的数据如下: scott.emp员工表结构如下:   SQL> DESC SCOTT.EMP; Name     Type         Nullable Defaul ...

  6. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  7. 【Objective-C基础教程-读书笔记】第1章 启程

    在第1章里面,作者主要以一种站在世界中心呼唤爱的姿态,给读者们打打鸡血洗洗脑,鼓励大家,投入时间学习Objective-C,值得啊! 首先,Objective-C既能用来开发OS X平台上的APP,又 ...

  8. 最全面的 DNS 原理入门

    DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...

  9. springmvc报错 org.springframework.web.servlet.DispatcherServlet

    在写springMVC时,导入所有需要的包后,运行程序,控制台报以下错误: 严重: Servlet [springDispatcherServlet] in web application [/Spr ...

  10. C#邮件接收系统核心代码(接收POP3邮件IMAP邮件)

    /* * Created by SharpDevelop. * User: Administrator * Date: 2013/11/18 * Time: 20:55 * * To change t ...