leetcode371. Sum of Two Integers
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.
一般步骤:
1、计算不用进位的位置,使用异或
2、计算进位,算数与操作,计算完后左一一位
3、如此循环
class Solution {
public:
int getSum(int a, int b) {
int carry = 0;
while(b)
{
a = a^ b;
carry = a&b;
b = carry << 1;
}
return a;
}
};
leetcode371. Sum of Two Integers的更多相关文章
- 每天一道LeetCode--371. Sum of Two Integers
alculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Examp ...
- [Swift]LeetCode371. 两整数之和 | 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 两数之和
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
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- 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】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 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 - ...
- 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 -. ...
随机推荐
- 期待中冷静前行,专家预测2017年VR产业5大发展趋势
VR在90年代火过一阵后,在2016年迎来了爆发.今年的VR领域,除了Oculus.HTC.索尼等发布的各家硬件,还有许多VR内容争奇斗艳的迸发,但是VR会一直保存热度吗? 事实上,对于科技圈巨头而言 ...
- webpack练手项目之easySlide(三):commonChunks(转)
Hello,大家好. 在之前两篇文章中: webpack练手项目之easySlide(一):初探webpack webpack练手项目之easySlide(二):代码分割 与大家分享了webpack的 ...
- ACM 图像有用区域
图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 “ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以 ...
- 【CodeVS】 p1696 奇怪的函数
题目描述 Description 自从得到上次的教训后,John的上课态度认真多了,也变得更爱动脑筋了.今天他又学习了一个新的知识:关于 xk 的位数. 如果x大于0小于l,那么位数=1+小数部分×k ...
- Android -- 编辑框更改样式
1. 效果图
- BZOJ4531: [Bjoi2014]路径
Description 在一个N个节点的无向图(没有自环.重边)上,每个点都有一个符号, 可能是数字,也可能是加号.减号.乘号.除号.小括号.你要在这个图上数 一数,有多少种走恰好K个节点的方法,使得 ...
- exp.validate.js
简单实用的js基础数据验证 prototype /// <reference path="/Scripts/expand-fn/exp_validate.js" /> ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- javascript 时间操作
javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=n ...
- 初识socket
socket也叫套接字,用于通信的一个句柄,描述IP与端口信息,程序通过套接字可以向网络发出请求或者应答网络请求. socket起源与unix,而unix/Linux基本哲学之一就是”一切皆文件“,对 ...