Tutorial: 371. Sum of Two Integers
来源:互联网 发布:身份证假软件下载 编辑:程序博客网 时间:2024/06/05 09:54
题目
给定两个int变量a、b,要求返回两者之和,但不准使用运算符+和-。
算法
这个题目不准使用运算符,那就只能通过位操作来模拟加减运算了。
1. a和b相与,能得出产生进位的地方carry
2. a和b相异或,能得到本次加法结果
3. b=carry<<1,作为下一个循环的加数
代码
private int getSum(int a,int b){ int carry=0; while (b!=0){ carry=a&b; a=a^b; b=carry<<1; } return a;}
关于此文章
本文首发于作者的个人博客stormlin.com。你也扫描以下二维码来关注我的个人微信公众号来获取更多更新鲜的内容哦。
0 0
- Tutorial: 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- SVM----(Python)
- 做个工具帮你找源码
- 结合Scikit-learn介绍几种常用的特征选择方法
- caffe源码阅读1:caffe.proto解析
- 动态代码修改xib约束
- Tutorial: 371. Sum of Two Integers
- 关于OC中的Block使用以及ARC和MAR下的内存管理方式
- Swift3.0-KVC构造函数与基本数据类型
- 用KAOS进行目标建模
- Android中的IPC方式
- 常见Python运行时报错整理
- TCP循环服务器 代码tcpser
- python笔记(十一)
- jquery插入元素的几种方法 insertBefore() insertAfter() prependTo() appendTo()