LintCode 1.A+B的问题
LintCode 1.A+B的问题
描述
给出两个整数 a 和 b , 求他们的和。
答案
public class Solution {
/**
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here
int m = a^b; // 保留值不同的位
int n = (a&b) << 1; // 值相同的位进1
if(n != 0) {
m = aplusb(m, n);
}
return m;
}
}
LintCode 1.A+B的问题的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
- Lintcode 375.克隆二叉树
-------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...
随机推荐
- Java对象在Hibernate持久化层的状态
-临时状态:刚用new语句创建对象,还没有被持久化,并且不处于Session缓存中.处于临时状态的java对象被称为临时对象. -持久化状态:已经被持久化,并且加入到Session的缓存中.处于持久化 ...
- PCL近邻搜索相关的类
首先PCL定义了搜索的基类pcl::search::Search<PointInT> template<typename PointT> class Search 其子类包括: ...
- 使用commons.cli实现MyCP
目录 Commons.cli库 MyCP 测试代码 总结 Commons.cli库 考虑到这次的任务是实现自己的命令行命令cp,我认为简单地使用args[]无法很好的完成需求.经过网上的一番搜索,我找 ...
- Linux学习路线全解,Linux操作系统学习路线
大家都知道,在现在这个信息化飞速发展的时代,IT技术火速发展,信息的重要性,可想而知.现在,在北京当一个高级运维工程师,年薪百万已经不是梦想.当然我也想,谁不想挣大钱,开好车,住好房.下面说说自己的一 ...
- tensorflow(3)可视化,日志,调试
可视化 添加变量 tf.summary.histogram( "weights1", weights1) # 可视化观看变量 还有添加图像和音频. 常量 tf.summary.sc ...
- leaflet.toolbar.js
leaflet.toolbar.js 参考:https://www.javascriptcn.com/read-38464.html
- IIS网站 由http协议改变为https协议
https://www.cnblogs.com/boosasliulin/p/6811231.html?utm_source=itdadao&utm_medium=referral
- 简单的C++输出日志
myLog.h #ifndef __myLog_H_ #define __myLog_H_ #include <stdio.h> #include <stdlib.h> #in ...
- Qt QSpinBox 和 QDoubleSpinBox
展示一个效果: QDoubleSpinBox跟QSpinBox类似,只是多了一个decimal.
- 跟我一起学python(2)
学习总结: 1.数据类型 a.数据:表示一种状态 b.python不存在字符类型 c.可变与不可变 d.x = 10 既 x = int(10) 2.字符编码 3.文件处理 详细: 数据类型: is ...