leetcode 371
2016-07-21 15:17
260 查看
371. Sum of Two Integers
Total Accepted: 15459Total Submissions: 29688
Difficulty: Easy
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.
思路:两相加,但是不能用加号和减号,那么从进位和异火等符号入手。异或计算加法,相与计算进位。
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);
}
}
相关文章推荐
- iOS中上传Array形式的json数据
- PostgreSQL相关配置
- servlet的filter过滤器
- ListView图片加载错乱03
- TCP协议中的三次握手和四次挥手(图解)
- Hbase的Observer
- java中的移位运算符:<<,>>,>>>总结
- IP地址的分类,有效主机范围,私有地址
- 【基于QMediaPlayer的简易视频播放器】— 1、创建基本布局
- PHP isset()与empty()的使用区别详解
- 内核、系统启动崩溃分析
- Uncowed Forces Codeforces Round #334
- 通过局域网安装centos6.4
- servlet的开发流程介绍
- 关于Git中SSH出现的Permission denied (publickey)问题
- C语言笔记
- Patrick and Shopping
- cannot open shared object file: No such file or director解决方案
- EL表达式及${对象}
- [置顶] 模式识别(Pattern Recognition)学习笔记(三十六)-- 动态聚类算法