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 ...
随机推荐
- Arch Linux中文乱码解决
Arch Linux中文乱码解决 1.安装中文字体 pacman -S wqy-zenhei ttf-fireflysung (flash乱码) ---乱码的原因就是缺少中文字体的支持,下载文泉驿 ...
- AutoMapper搬运工之配置
回顾 前几篇搬运了AutoMapper的基本用法,自定义映射,相信有看的同学已经会使用AutoMapper这个强大的Mapping工具了.不过细心的你是否还记得前几篇中有提到Map的创建并非是每次都需 ...
- java发送GET和post请求
package com.baqingshe.bjs.util; import java.io.BufferedReader; import java.io.IOException; import ja ...
- ajax 传递JSON对象参数
https://msdn.microsoft.com/zh-cn/library/cc836466(v=vs.94).aspx https://msdn.microsoft.com/zh-cn/lib ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- Python爬虫学习(5): 简单的爬取
学习了urllib,urlib2以及正则表达式之后就可以做一些简单的抓取以及处理工作.为了抓取方便,这里选择糗事百科的网页作为抓取对象. 1. 获取数据: In [293]: url = " ...
- Create a REST API with Attribute Routing in ASP.NET Web API 2
原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute- ...
- Apache rewrite
Apache rewrite mod_rewrite简介和配置 实URL跳转隐藏真实地址 拟目录 域名跳转 防止盗链 Apache配置支持httpd.conf配置.htaccess配置 启用rewri ...
- js中的call和apply
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:赵望野链接:https://www.zhihu.com/question/20289071/answer/14745394来源 ...
- 【原】iOS学习之苹果开发者账号的相关操作
1.苹果开发者账号分类 按价格分类 免费 ① 个人申请账号 仅可以用于真机调试 ② 院校账号 仅可以用于真机调试 通过苹果认证的高校可以使用 99$ ① 个人账号 ② 企业(公司)账号 申请所需的条件 ...