阿里2015笔试附加题-一个数组中存在一组数字,其中有一个数字重复3遍,其他2遍,在O(1)空间找到那个重复3次的数
2015-08-24 09:54
447 查看
题目就如标题,还要求时间尽可能短。这一题类似于LeetCode上的SingleNum那题,思路是将所有数字进行异或操作,最后剩下的那个数字就是重复3遍的。
代码如下:
代码如下:
package test2; public class test2 { public static int getNum(int[] array){ int result = 0; for(int i = 0; i < array.length;i ++){ result ^= array[i]; } return result; } public static void main(String[] args){ int[] array = {88, 459, 5262, 88, -17, 677, 88, 677, -17, 459, 5262}; System.out.print(getNum(array)); } }
相关文章推荐
- 随笔-NSClassFromString isKindOfClass
- 统计php更改文件,可疑文件
- (hdoj 5137 floyd)How Many Maos Does the Guanxi Worth
- 设置整体App页面跳转的动画效果
- springmvc整合shiro (2)
- 第2组UI-ImageView及子类
- Eclipse快捷键
- (hdoj 5137 floyd)How Many Maos Does the Guanxi Worth
- [置顶] ceph 0.80.1源码安装
- Highcost SQL
- Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from
- 黑马程序员_java基础之面向对象(一)
- IOS画线条
- Apache POI组件操作Excel,制作报表(二)
- Python 深拷贝和浅拷贝
- Unix 哲学:Elixir 将会替代 Go
- hdoj-1201 18岁生日【日期和时间处理】
- Kalman filter
- iOS中的动画
- POJ1329Circle Through Three Points