LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/
题目:
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example 1:
Input: a = 1, b = 2
Output: 3
Example 2:
Input: a = -2, b = 3
Output: 1
题解:
两个数79和16 相加,不考虑进位相加得85, 只考虑进位进位是10, 85+10 = 95正好是结果.
二进制表示时, 不考虑进位0+0=0, 1+0=1, 0+1=1, 1+1=0, 其实就是xor.
只考虑进位0+0=0, 1+0=0, 0+1=0, 1+1=1, 其实就是&.
进位是放到更前以为,所以需要左移动一位, <<1.
可递推调用.
Time Complexity: O(1).
Space: O(1).
AC Java:
public class Solution {
public int getSum(int a, int b) {
if(b == 0){
return a;
}
int sum = a^b;
int carry = (a&b)<<1;
return getSum(sum, carry);
}
}
也可Iteration.
Time Complexity: O(1).
Space: O(1).
AC Java:
public class Solution {
public int getSum(int a, int b) {
while(b != 0){
int carry = (a&b)<<1;
a ^= b;
b = carry;
}
return a;
}
}
LeetCode Sum of Two Integers的更多相关文章
- LeetCode——Sum of Two Integers
LeetCode--Sum of Two Integers Question Calculate the sum of two integers a and b, but you are not al ...
- [LeetCode] 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: Sum of Two Integers && Summary: Bit Manipulation
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 -. Exam ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
随机推荐
- 开放封闭原则(Open Closed Principle)
在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...
- Java基本数据类型与位运算
>>赋值运算符 赋值使用操作符“=”.它的意思是“取右边的值(即右值),把它复制给左边(即左值)”.右值可以是任何 常数.变量或者表达式 (只要它能 生成 一个值就行).但左值必须是一个明 ...
- SpringMVC学习(一)
Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分. Web MVC架构 1.用户发起request请求至控制器(Controller) 控制接收用户请求的数 ...
- windows消息钩子注册底层机制浅析
标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
第三方类库源码 将一网友的XMPP代码从ADT转到AS时,发现其使用了第三方类库,源码放在了lib下,直接在AS中Import project,第三方类库并没有自动导入进来,看来需要自己动手了. 项目 ...
- Mysql与PostgreSql数据库学习笔记---打酱油的日子
mysql 从最基础的数据引擎,到进程结构,都不能支持数据版本.导致其职能阻塞“并发”,不支持最基本的事务,innodb达不到基本事务要求,任何写数据,都导致整个表锁住.充其量只能算是一个玩具,或者说 ...
- Only Link: Inheritance and the prototype chain
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_cha ...
- [转] JS中简单的继承与多态
这里讲了一个最最最简单的JS中基于原型链的继承和多态. 先看一下以下这段代码的实现(A是“父类”,B是“子类”): var A = function(){ this.value = 'a'; this ...
- HDU 3306 Another kind of Fibonacci(快速幂矩阵)
题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...
- ZeroMQ接口函数之 :zmq_plain - 明文认证
ZeroMQ 官方地址 :http://api.zeromq.org/4-2:zmq_plain zmq_plain(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_plain ...