【leetcode】Single Number (Medium) ☆
题目:
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?
要求O(n)算法,还不能有辅助空间。开始用了各种O(n^2)的方法,都超时,后来突然顿悟了。异或可以消掉两个相同数字。
直接把所有数字异或在一起,就是单独的那个数字了。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public://异或可以把两个相同的数字消除 O(n) AC
int singleNumber3(int A[], int n) {
int ans = ;
for(int i = ; i < n; i++)
{
ans ^= A[i];
}
return ans;
}
}; int main()
{
Solution s;
int a[] = {,,,,,,,,};
int n = s.singleNumber3(a, ); return ;
}
【leetcode】Single Number (Medium) ☆的更多相关文章
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【Leetcode】 - Single Number II
Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
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 II
int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- 安装cocoods
http://www.tuicool.com/articles/7VvuAr3 http://blog.csdn.net/gf771115/article/details/50403253(详细,用终 ...
- jquery隐藏按钮
$(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...
- 2015多校.Zero Escape (dp减枝 && 滚动数组)
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 如何配置和使用Spring框架的bean
1. 首先在src目录下新建beans.xml文件,该文件名可更改. 2. 编辑xml文件如下,这里需要注意的是beans的表头中信息需要根据不同的版本对应的内容不同,本例中使用的spring的版本为 ...
- MySQL Cluster 配置文件(config.ini)详解
MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...
- Apache开发模块
输入perl Configure.pl 安装目录...\apache2.2 “httpd.exe” 生成apxs命令, apache2.2下build目录中的config_vars.mk文件 将CC ...
- struts2-(2)HelloWorld
1.环境配置 1).进入http://struts.apache.org/download.cgi#struts23241 下载 struts官方源码 2).解压,进入apps/struts2-bla ...
- 最牛X的GCC 内联汇编
导读 正如大家知道的,在C语言中插入汇编语言,其是Linux中使用的基本汇编程序语法.本文将讲解 GCC 提供的内联汇编特性的用途和用法.对于阅读这篇文章,这里只有两个前提要求,很明显,就是 x86 ...
- 14个Xcode中常用的快捷键操作(转)
2014-12-24 17:31 编辑: suiling 分类:iOS开发 来源:iPhoneDev.tv 13 31621 Xcode 6Xcode快捷键 招聘信息: 开发工程师 iOS开发工程师 ...
- qt-5.6.0 移植之tslib 配置及编译
tslib 是qt启动时的一个触屏校正检验程序. 它的配置以及编译比较简单. 第一步, 下载tslib源码包: http://download.csdn.net/detail/MKNDG/329156 ...