Lintcode—落单的数
来源:互联网 发布:q币拦截软件 编辑:程序博客网 时间:2024/05/01 10:11
落单的数(Java)
题目
给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。
输入:
[1,1,2,3,3,3,2,2,4,1]
输出:
4
要求:
一次遍历,常数级的额外空间复杂度
思路:
把数组中所有的数字做异或,因为相同的两个数组异或结果为0,所以最后得到的数字就是结果。
Java代码:
public class Solution { /** *@param A : an integer array *return : a integer */ public static int singleNumber(int[] A) { if (A.length == 0) { return 0; } int res = A[0]; for(int i = 1; i < A.length; i++){ res = res ^ A[i]; } return res; }}
0 0
- Lintcode—落单的数
- 落单的数—LintCode
- lintcode ----落单的数
- LintCode 落单的数
- lintcode-落单的数
- lintcode,落单的数
- lintcode->落单的数
- LintCode :落单的数
- 落单的数-LintCode
- lintcode:落单的数
- lintcode落单的数
- LintCode 落单的数
- Lintcode落单的数
- LintCode—贪心—落单的数
- Lintcode—落单的数II
- Lintcode—落单的数III
- LintCode-落单的数 II
- LintCode-落单的数 III
- 第1周 项目(A)-1电阻并联
- 五个案例让你明白GCD死锁
- 我使用过的Linux命令之exit - 退出当前shell
- epoll 再探--原理详解
- Crontab定时任务
- Lintcode—落单的数
- rhel 6.7简单安装postgresql 9.2.10
- NodeJs操作mysql数据库(coffee版)
- Android自定义类似ProgressDialog效果的Dialog
- iOS自动生成图片@1x,@2x ,@3x图片
- Codeforces Round #346 (Div. 2) C模拟
- iOS 去掉tableViewCell 点击效果 三个简单方法
- 大型网站之存储瓶颈(数据库的水平扩展)
- C#中偏移量和长度超出数组的界限的问题