[LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
感谢cnblog博主feiling,这篇博客一方面参考了feiling的博客,也加入了自己的一些看法。
[解题思路]
要求线性时间复杂度,同时空间复杂度为O(1),即只允许开常数个空间。
最直接的思路是对每一个元素尝试查找是否有重,如果没有重,就返回。
class Solution {
public:
int singleNumber(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int i = , j = ;
int ans = ;
for(i = ;i < n;i++)
{
for(j = ;j < n;j++)
{
if(i == j)
continue;
else if(A[i] == A[j])
break;
else continue;
}
if(j == n)
return A[i];
}
}
};
不幸的是虽然是o(n2)的时间复杂度,但是还是超时了。于是乎就得想一个o(n)的算法。
o(n)的算法只能是线性扫描一遍,可能的相法是位运算。对于异或来说:
1. 异或运算是可交换,即 a ^ b = b ^ a
2. 0 ^ a = a
那么如果对所有元素做异或运算,其结果为那个出现一次的元素,理解是a1 ^ a2 ^ ....,可以将所有相同元素交换至相邻位置,首先运算相同元素,则会产生(n - 1)/2个0异或积,剩余一个单一元素,他们的异或积为这个单一元素自己,得解。

1 public class Solution {
2 public int singleNumber(int[] A) {
3 // Note: The Solution object is instantiated only once and is reused by each test case.
4 if(A == null || A.length == 0){
5 return 0;
6 }
7 int result = A[0];
8
9 for(int i = 1; i < A.length; i++){
10 result = result ^ A[i];
11 }
12 return result;
13 }
14 }

本题扩展
1.一个数组中有两个元素只出现一次,其他所有元素都出现两次,求这两个只出现一次的元素
[解题思路]
将数组所有元素都进行异或得到一个不为0的结果,根据这个结果中的不为0的某一位将数组分成两组
将两组中的元素进行异或,如两个数组的异或值都不为0,则得到最后结果
2.一个数组中有一个元素只出现1次,其他所有元素都出现k次,求这个只出现1次的元素
[解题思路]
当k为偶数时,同lss
当k为奇数时,将数组中每个元素的每一位相加mod k,得到结果即位出现1次的元素,时间复杂度O(nlen),空间复杂度为O(1)
[LeetCode] Single Number的更多相关文章
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
- [LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
随机推荐
- C#之数组篇
大杂烩 一.数组初始化 1.一维数组 String[] str = new String[3] { "1","2","3"}; ...
- ppt2013技术整理
1. 显示选择窗格 便于选择该页的所有元素.分组.隐藏与显示等. 位于:开始-编辑-选择-选择窗格 2. 显示动画窗格 便于调节页面中元素的动画状态. 位于:动画-高级动画-动画窗格 3. 绑定动画触 ...
- ngInclude与script加载模板
ng-include: 官网实例: <p>ng-include:</p> <select ng-model="template" ng-options ...
- 创建面注记PolygonElement
1.根据4点创建一个面 /// <summary> /// 根据4个点创建图形,点序要顺时针 /// </summary> /// <param name="p ...
- thinkphp 3.2与phpexcel
thinkphp版本:3.2 1.在http://phpexcel.codeplex.com/下载最新PHPExcel 2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件 ...
- ssh保持链接
修改/etc/ssh/sshd_config配置文件 ClientAliveInterval 300(默认为0), 参数的是意思是每5分钟,服务器向客户端发一个消息,用于保持连接,使用service ...
- passive 的事件监听器
很久以前,addEventListener() 的参数约定是这样的: addEventListener(type, listener, useCapture) 后来,最后一个参数,也就是控制监听器是在 ...
- 【Tomcat 6.0官方文档翻译】—— 简介
Tomcat作为使用最多的web容器,研究其原理过程,对掌握java web开发有很重要的影响. 因此下定决心,从官方文档入手,好好学学web相关的知识. 介绍 本篇是Apache Tomca ...
- tyvj1005 采药
描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞 ...
- poj1062 昂贵的聘礼
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...