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 - ...
随机推荐
- bzoj2821
其实和bzoj2724是一样的都是先处理多个块的答案,然后多余部分暴力空间要注意一下,还是O(nsqrt(n)); ..,..] of longint; g:..,..] of longint; a, ...
- poj1849
不难发现每条边最多走两次,最少走一次也就是我们要在所有走两次的边中选两条从根出发没有公共边的路径使路径上的边少走一次显然我们找的是最长路径
- WordPress Design Approval System插件‘step’参数跨站脚本漏洞
漏洞名称: WordPress Design Approval System插件‘step’参数跨站脚本漏洞 CNNVD编号: CNNVD-201309-084 发布时间: 2013-09-11 更新 ...
- 关于android屏幕适配
好吧 我承认被美工虐的够呛,而且美工他么是个男的!一点也不美, 废话不多说 急着赶路, 之前不怎么重视 直到遇见这个美工给我一套1080x1920的 图,没错 就一套 1dp=3px没错的啊 问题是就 ...
- CSU 1511 残缺的棋盘 第十届湖南省赛题
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 题目大意:在一个8*8的棋盘中,给你一个起点位置和一个终点位置,同时也给你一个陷阱 ...
- TCP11种状态分析和测试
-->简介 -->正文 -->测试一些状态 --------------------------------------------------------------------- ...
- leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...
- oracle中多表查询优化笔记
ORACLE有个高速缓冲的概念,这个高速缓冲呢就是存放执行过的SQL语句,对应已经执行过的sql语句,第二次执行的时候速度会比第一次块,用的就是高速缓冲.ORACLE的高速缓冲是全字符匹配的,如果sq ...
- oracle hints
oracle hints 今天是2013-10-08,对于oracle hint有很多,具体可以参考联机手册: http://docs.oracle.com/cd/E11882_01/server.1 ...
- C# SQL 整表插入 分类: C# 2014-09-17 16:18 369人阅读 评论(2) 收藏
说明: (1)表A的一部分数据插入到表B (2)DataAccess 类,是放在DAL层下的底层类; da.StrConnection 写在DataAccess类中; //整表插入方法 private ...