371. Sum of Two Integers -- Avota
问题描述:
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的更多相关文章
- 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) { ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 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 ...
- 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 -. ...
- 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 ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- [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 ...
- 【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 - ...
随机推荐
- Java项目中使用配置文件配置
private String readConfig() { Properties p = new Properties(); InputStream in = getClass().getClassL ...
- 【转】Java中如何遍历Map对象的4种方法
原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...
- Android开源项目整理:个性化空间View篇(看遍论坛千万篇,不看此篇也枉然)
个性化View控件虽然你在多处可以找到 但是这个整理的不可以不看欧: 主要介绍不错的个性化View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridVi ...
- mac下的改装人生——关于mc700安装双硬盘以后的win重装问题
宝贝到家~玩了几天,感觉神清气爽~今天正式开始准备工作了哈. 今天早上开了mac的win7系统发现了几个小问题.好吧我承认这个真的是小问题,就是我没有办法能调整屏幕的亮度了.重装了bootcamp依然 ...
- 《University Calculus》-chape12-偏导数-基本概念
偏导数本质上就是一元微分学向多元函数的推广. 关于定义域的开域.闭域的推广: 其实这个定义本质上讲的就是xoy面上阴影区域的最外面的一周,只不过这里用了更加规范的数学语言. 二次函数的图形.层曲线(等 ...
- jQuery各种效果举例
jQuery 所有jQuery详细使用说明请见:http://www.php100.com/manual/jquery/ jQuery的作用是操作浏览器html,从而达到用户的可视化效果,按照功能可分 ...
- 第十七章、程序管理与 SELinux 初探
---恢复内容开始--- 什么是程序 (process) 在 Linux 底下所有的命令与你能够进行的动作都与权限有关, 而系统依据UID/GID以及文件的属性相关性判定你的权限!在 Linux 系统 ...
- Broken line - SGU 124(判断点与多边形的关系)
题目大意:RT 分析:构造一条射线,如果穿越偶数条边,那么就在多边形外面,如果穿越奇数条边,那么就在多边形里面. 代码如下: ===================================== ...
- myisam MySQL 锁问题
2016-07-23 (点击上方公众号,可快速关注) 来源:阿里云 RDS - 数据库内核组 链接:http://mysql.taobao.org/monthly/2016/03/10/ 前言 最近 ...
- 关于用jQuery知识来实现优酷首页轮播图!
▓▓▓▓▓▓ 大致介绍 看到了一个轮播图的思路,就想的自己动手实践一下,总体来说用jQuery实现起来简单多了 如果对代码中使用的方法有疑问,可以参考我的jQuery学习之路(持续更新),里面有讲解: ...