【03_136】Single Number
感谢:http://www.cnblogs.com/changchengxiao/p/3413294.html
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?
琢磨了很久没找到满足题目的两个要求:O(1)和不开空间。实在没办法才上网查找,原来是汇编语言的知识,而且C++中还可以直接用。
原文:
o(n)的算法只能是线性扫描一遍,可能的相法是位运算。对于异或来说: . 异或运算是可交换,即 a ^ b = b ^ a . ^ a = a 那么如果对所有元素做异或运算,其结果为那个出现一次的元素,理解是a1 ^ a2 ^ ....
所以说,这是终极解法。
public class Solution {
public int singleNumber(int[] A) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(A == null || A.length == ){
return ;
}
int result = A[]; for(int i = ; i < A.length; i++){
result ^= A[i];
}
return result;
}
}
LeetCode很有趣,希望明天可以自己想出来!
【03_136】Single Number的更多相关文章
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【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】【Medium】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode79】Single Number III
题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...
- 【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 (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
随机推荐
- Linux 本人常用到的基本命令
cat -n FileName //查看FileName文件的内容.-n显示对应行号. yum install SoftName //安装软件,切记使用root权限. service //查看服务.例 ...
- js创建节点,小试牛刀
实现如下的功能 非常简单的一个小训练. 思想: 1.首先创建text和一个button 代码如下. <body> <input type="text" id=&q ...
- Python 爬虫—— requests BeautifulSoup
本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...
- C++Builder Berlin 编译
编译工程,一个单元有错误,就停下了, 能不能把所有单元都编译,一次处理所有单元的错误?
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍
一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...
- underscore 笔记
//reduce是汇总 递归 var perDesc=[ {name: "haha", "email": "haha@email.com"} ...
- jquery remove/add css
<input type="submit" class="btn btn-primary" id="submit" value=&quo ...
- 8.9 CSS知识点2
4.关系选择符 包含选择符(Descendant combinator) E F 选择所有被E元素包含的F元素 <style type="text/css"> h1 ...
- Cadence UVM基础视频介绍(UVM SV Basics)
Cadence关于UVM的简单介绍,包括UVM的各个方面.有中文和英文两种版本. UVM SV Basics 1 – Introduction UVM SV Basics 2 – DUT Exampl ...
- jquery 根据年 月设置报表表头
function setTblHeadr(thisTime){ $("#datatable_ajax1 thead").empty(); //获取星期 var weekday=ne ...