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 - ...
随机推荐
- 最棒的Visual Studio扩展
isual Studio是微软公司推出的开发环境,Visual Studio可以用来创建Windows平台下的Windows应用程序和网络应用程序,也可以用来创建网络服务.智能设备应用程序和Offic ...
- Python异常处理实例
#coding=utf-8 #---异常处理--- # 写一个自己定义的异常类 class MyInputException(Exception): def __init__(self, length ...
- HDOJ(HDU) 2524 矩形A + B(推导公式、)
Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- Windows下父进程监视子进程状态
最近研究自动化测试,需要获取程序的运行状态及结果,下面是些参考资料. 原文地址:http://blog.csdn.net/ariesjzj/article/details/7226443 Linux下 ...
- Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备
Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备 转载自: http://news.chinaventure.com.cn/11/7/1381032922.shtml 今年是 Skype 网络电 ...
- 关于数据库一致改关闭下redo日志文件丢失的处理办法的总结
数据库一致性关闭下redo日志文件丢失的处理办法(归档和非归档都行) 1. inactive log 在一致性关闭后删除重启时可以在mount下(不丢失数据) alter database clea ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- C# Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- Nginx系列~负载均衡服务器与WWW服务器的实现
上两讲主要是关于Nginx的环境的介绍,没有涉及到真正环境的开发,这次我们以一个实现的例子,来说明一下负载均衡服务器与WWW服务器的Nginx是如何配置的,并最终如何实现的. 如下是一个实际场景,一台 ...