[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 ...
随机推荐
- java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException错误的解决
java.lang.NoClassDefFoundError 是运行时jvm找不到对应类.这种情况是少包的导致的.根据提示语添加对应的jar包就可以. 感叹一下:maven真是一个伟大的东西,在包的依 ...
- python学习笔记-(十四)进程&协程
一. 进程 1. 多进程multiprocessing multiprocessing包是Python中的多进程管理包,是一个跨平台版本的多进程模块.与threading.Thread类似,它可以利用 ...
- How to use FTP
Forward from: https://www.server-world.info/en/note?os=CentOS_7&p=ftp&f=2 Thanks for your sh ...
- rpm---linux软件安装与管理
linux的安装命令选项太多,整理一下,方便后期查找. 汇总: install: rpm -ivh 包全名 安装 upgrade: rpm -Uvh 包全名 升级 erase: rpm -e 包名 删 ...
- 删除Tomcat服务及其它注意
使用sc delete Tomcat7(注意服务名是Tomcat7 不是Apache......Tomcat7.0). 绿色版无法启动的话,需要先ervice.bat install注册一下服务. 如 ...
- MySQL 拷贝数据库表方式备份,还原后提示 table xxx '' doesn`t exist
MySQL很强大,支持直接拷贝数据库文件快速备份,那数据库文件在哪里呢? 打开MySQL的配置文件 my.ini,找到 datadir 节点,如 datadir="D:/Program Fi ...
- T-SQL实用查询之常用SQL语句
删除数据库所有的表: declare @sql varchar() begin SELECT @sql='drop table ' + name FROM sysobjects WHERE (type ...
- 用原生js实现的链式调用函数
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- poj 1141
简单的dp 忘了\n,调了半天(目测不是第一次了) 直接贴代码: #include<cstdio> #include<cstring> using namespace std; ...
- Python自动化之django的ORM操作——Python源码
""" The main QuerySet implementation. This provides the public API for the ORM. " ...