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 ...
随机推荐
- 【转载】CentOS服务器配置VPN详解
转载来自: https://bbs.aliyun.com/read/162297.html http://www.wanghailin.cn/centos-7-vpn/ 操作系统:CentOS 6.3 ...
- Python安装包或模块的多种方式汇总
windows下安装python第三方包.模块汇总如下(部分方式同样适用于其他平台): 1. windows下最常见的*.exe,*msi文件,直接运行安装即可: 2. 安装easy_install, ...
- JS将指定的时间戳转为UTC时间
Js中获取时间戳可用var dayMiliseconds = parseInt(new Date().valueOf());Js的时间戳单位为毫秒(1s = 1000 ms),下面是一个将制定的格式转 ...
- .NET开发者如何愉快的进行微信公众号开发
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:这篇文章只是一个如何提高开发效率的简单指导和记录,不会涉及具体的微信公众号开发内容. ...
- eventbus实时更新
1.发送方 EventBus.getDefault().post(new FriendApprovalEvent()); 2.接收方 /** * 收到好友消息 * * @param event */ ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
- jq switch case
switch (cnt) { case ("string1"): ... ...
- .NET Oracle Developer的福音——ODP.NET Managed正式推出
在.NET平台下开发Oracle应用的小伙伴们肯定都知道一方面做Oracle开发和实施相比SqlServer要安装Oracle客户端(XCopy.自己提取相关文件也有一定复杂性),另一方面相比JAVA ...
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...