[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论坛
linux在国内经过十多年的发展,已慢慢走向成熟.昔日如雨后春笋般成长的linux网站,现在已出现了明显的两极分化.一部分已成长壮大,公司化运作,一部分面临域名出售或关闭的境地. 以笔者经验,以下十 ...
- POJ2442 Sequence
题目链接. #include <iostream> #include <cstdio> #include <cstring> #include <cstdli ...
- phpMyAdmin 跨站脚本漏洞
漏洞名称: phpMyAdmin 跨站脚本漏洞 CNNVD编号: CNNVD-201307-647 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 低危 漏洞类型: ...
- Agri-Net(prim算法,最小生成树问题)
看图便知道: 来来上代码: #include <cstdio>#include <cstdlib>#include <cstring>#includ ...
- 《A First Course in Probability》-chaper5-连续型随机变量-基本概念
在利用基本的概率论模型解决实际问题的时候,我们很容易发现一些随机变量的连续分布的,例如火车进站的时间.台灯的寿命等一些和时间相关的随机变量,此时我们发现我们难以求出某个点的概率了,因为随机变量是连续的 ...
- [转]stringstream的用法
使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中, ...
- Jenkins 十二: 集成 selenium 测试
我的selenium采用的是python版本. 其实 selenium java版本也类似. 1. 在jenkins里面新建selenium 测试项目. 源码管理采用 “Subversion”,输入 ...
- TCP 的那些事儿(下)
这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇<TCP的那些事儿(上)> 上篇中,我们介绍了TCP的协议头.状态机.数据重传中的东西.但是TCP要解决一个很大的事,那就是要 ...
- 一些正则在js使用方法
输入框直接正则判断 <input type="password" name="pwd" placeholder="密码只能以数字\英文\@\.& ...
- 利用d3.js绘制雷达图
利用d3,js将数据可视化,能够做到数据与代码的分离.方便以后改动数据. 这次利用d3.js绘制了一个五维的雷达图.即将多个对象的五种属性在一张图上对照. 数据写入data.csv.数据类型写入typ ...