[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.
class Solution {
public:
int singleNumber(int A[], int n) {
int i,j;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
{
if(A[j]==A[i])
{
int temp = A[i+];
A[i+] = A[j];
A[j] = temp;
i++;
break;
}
}
if(j==n)
return A[i];
}
}
};
上面的是传统方法,时间复杂度是O(n2),空间复杂度是O(1)。
class Solution {
public:
int singleNumber(int A[], int n) {
int result=;
for(int i=; i<n; i++)
result = result ^ A[i];
return result;
}
};
用位异或运算实现,时间复杂度和空间复杂度均为O(1).
运用了异或运算具有交换律和结合律的性质。
交换律: a^b = b^a
结合律: (a^b)^c = a^(b^c)
另外,a^a=0。
[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.的更多相关文章
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- LeetCode OJ 33. Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- Linux&shell 之基本Shell命令
写在前面:案例.常用.归类.解释说明.(By Jim) 文件和目录列表lsls -F (用斜杠区分目录和文件)ls -a (把隐藏文件一并显示出来)ls -l (同ll,显示详细信息)ls -l 文件 ...
- 【转】java提高篇(二三)-----HashMap
原文网址: http://www.cnblogs.com/chenssy/p/3521565.html HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以 ...
- 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ...
- 使用DateDiff方法获取日期时间的间隔数
一:用DateDiff方法获取日期时间的间隔数,截图 二:代码 using System; using System.Windows.Forms; using Microsoft.VisualBasi ...
- 用Processon在线绘制UML的尝试
地址https://www.processon.com/ ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询 ...
- HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...
- Assumptions
理想情况下,写测试用例的开发人员可以明确的知道所有导致他们所写的测试用例不通过的地方,但是有的时候,这 些导致测试用例不通过的地方并不是很容易的被发现,可能隐藏的很深,从而导致开发人员在写测试用例时很 ...
- Java中的字符串流的读取和写入(创建文件并判断重复账户)
各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!! import java.io.BufferedOutputStream; import java.io.Buffered ...
- Amoeba for MySQL 非常好用的mysql集群软件
http://docs.hexnova.com/amoeba/ Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专 ...
- [转] linux中cat more less head tail 命令
1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名. 比如: [root@localhost ~]# c ...