文档章节

371. Sum of Two Integers

初雪之音
 初雪之音
发布于 2017/02/26 11:29
字数 126
阅读 6
收藏 0

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.

Hint:

    0 1 1

+   0 0 1

-----------

    0 1 0

+   0 1 0

-----------

    1 0 0

观察到只有相同位上都为1时才产生进位,我们可以通过左移实现

Code:

public class Solution {
    public int getSum(int a, int b) {
		int result = 0;

		while (b != 0) {
			int carry = (a & b) << 1;
			a = a ^ b;
			b = carry;
		}
		result = a;

		return result;
	}
}

 

 

© 著作权归作者所有

共有 人打赏支持
初雪之音
粉丝 42
博文 265
码字总数 148651
作品 0
广州
程序员
最接近的三个数的和

原题   Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume t......

一贱书生
2016/12/12
16
0
判断一个数是否是4个连续整数的和

Given a positive integer, tell whether it is equal to the sum of four consecutive integers. Input A positive integer(<231), each case will be on a separate line. A zero (0) deno......

wwwmmm
2012/06/19
920
19
Hackerrank,set、list时间复杂度问读

Problem Statement There is an array of n integers, and 2 disjoint sets of m integers each A and B. You like all integers in A and dislike all integers in B. Your initial happine......

zpengseu
2015/12/09
52
0
Atcoder Tenka1 Programmer Beginner Contest IntegerotS 【异或+思维】

IntegerotS Time Limit: 4000/2000 MS (Java/Others) Memory Limit:262144/262144 K (Java/Others) Problem Description Seisu-ya, a store specializing in non-negative integers, sellsN ......

my_sunshine26
2017/10/01
0
0
HDU - 1003 Max Sum

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5......

Daybreaking
07/29
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

day58-20180816-流利阅读笔记-待学习

苹果市值破万亿,iPhone 会涨价吗? Lala 2018-08-16 1.今日导读 苹果教父乔布斯曾经说过:“活着就是为了改变世界。”虽然他在 56 岁时就遗憾离世,但他极具创新和变革的精神早已深埋进苹果...

aibinxiao
28分钟前
4
0
[雪峰磁针石博客]python3快速入门教程1 turtle绘图-2函数

菲波那契序列: >>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:... print(b)... a, b = b, a+b...112......

python测试开发人工智能安全
今天
0
0
java环境变量配置最正确的方式

原贴:https://blog.csdn.net/qq_40007997/article/details/79784711,十分详细,亲测有效

kitty1116
今天
0
0
49.Nginx防盗链 访问控制 解析php相关 代理服务器

12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置(502的问题) 12.16 Nginx代理 扩展 502问题汇总 http://ask.apelearn.com/question/9109 location优先级 http://blog....

王鑫linux
今天
2
0
Nginx防盗链、访问控制、解析php相关配置、Nginx代理

一、Nginx防盗链 1. 编辑虚拟主机配置文件 vim /usr/local/nginx/conf/vhost/test.com.conf 2. 在配置文件中添加如下的内容 { expires 7d; valid_referers none blocked server_names *.tes......

芬野de博客
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部