A + B 问题
2016-05-31 18:32
405 查看
给出两个整数a和b, 求他们的和, 但不能使用
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
if (b == 0){
return a;
}
return aplusb(a ^ b , (a & b) << 1);
//用位运算代替加法时 ^表示相加 &表示进位
//二者相加时进位要左移一位,因为进位是跟上一位相加的
//!!!!!!!!注意不要忘记后面的括号!
}
};
+等数学运算符。【位运算】
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
if (b == 0){
return a;
}
return aplusb(a ^ b , (a & b) << 1);
//用位运算代替加法时 ^表示相加 &表示进位
//二者相加时进位要左移一位,因为进位是跟上一位相加的
//!!!!!!!!注意不要忘记后面的括号!
}
};
相关文章推荐
- 最佳优先搜索(Best-First Search)
- UITableView使用详解及技巧大全
- Android获取系统震动的调用
- stringbuild和stringbuffer的区别
- CentOS 6.4 x64 Percona-Server-5.6.15 源码安装
- Monitorix 监控 安装配置
- Peer-to-Peer 综述
- 关于写博客的初心
- iOS通讯录排序与显示
- JQuery中的样式切换
- 配置 nginx location 实时查看 php-fpm 的状态
- CentOS 6.4 x64 安装 配置 Redmine 2.4.1
- MySQL必知必会——命令行使用MySQL图文教程
- imageloder缓存图片
- linux 系统安装后网卡配置,解决不能上网(重启后不能上网)(新手篇)
- 一位女程序员的奋斗路程
- NOIP2014 day2 T2 洛谷P2296 寻找道路
- NOIP2014 day2 T2 洛谷P2296 寻找道路
- Web 网站 故障常用分析命令
- Nginx 中 nginx.conf 详解