题目

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

The order of the result is not important. So in the above example, [5, 3] is also correct.

Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

分析

给定一个无序数组,内含两个只出现一次的元素,其余元素均出现两次,找出这两个元素。

要求,线性时间,常量空间。

AC源码

class Solution {
public:
//方法一:借助数据结构unordered_set,占用了额外O(n)的空间
vector<int> singleNumber1(vector<int>& nums) {
if (nums.empty())
return vector<int>(); int len = nums.size(); unordered_set<int> us;
for (int i = 0; i < len; ++i)
{
if (us.count(nums[i]) == 0)
us.insert(nums[i]);
else
us.erase(nums[i]);
}//for return vector<int>(us.begin(), us.end());
} //方法二:线性时间复杂度,常量空间复杂度
vector<int> singleNumber(vector<int>& nums) {
if (nums.empty())
return vector<int>(); int len = nums.size();
int res = 0;
for (int i = 0; i < len; ++i)
{
res ^= nums[i];
}//for vector<int> ret(2, 0);
//利用位运算,将原数组一分为二,每个部分含有一个只出现一次的数字,其余数字出现两次
int n = res & (~(res - 1));
for (int i = 0; i < len; ++i)
{
if ((n & nums[i]) != 0)
{
ret[0] ^= nums[i];
}
else{
ret[1] ^= nums[i];
}//else
}//for return ret;
}
};

GitHub测试程序源码

LeetCode(260) Single Number III的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(136) Single Number

    题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  5. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  6. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  7. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  8. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  9. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

随机推荐

  1. 获取jar包当前的路径

    转自:http://kinganpo.iteye.com/blog/876243 import java.io.File; /** * 获取打包后jar的路径信息 * @author Administ ...

  2. Ubuntu14.04 使用scp远程传输命令进行服务器文件互传

    1.将另一个服务器上的文件拷贝到本地 sudo scp host_name@host_ip:/home/aaa/bbb /ccc/ddd/bbb 上面命令的意思是将远程服务器/home/aaa/目录下 ...

  3. nginx一个简单的反向代理设置

    location /aaaaa/ { proxy_pass http://localhost:8080/aaaaa/; } 经过配置,现在访问 http://localhost/aaaaa/   就会 ...

  4. NPOI导出EXCEL后公式无结果,公式重新计算开关

    但其实这个选项是可以控制的.在NPOI中,这个属性叫做XSSFSheet.ForceFormulaRecalculation. 打开该选项的话,代码为 sheet1.ForceFormulaRecal ...

  5. List和set集合:交集、差集、合集的区别retainAll,removeAll、addAll

    set .list集合的交集(retainAll).差集(removeAll)是没有区别的都是一样的. set .list集合的合集addAll是有区别的:set可以去重复:list不去重复 publ ...

  6. Java基础:(二)String字符串

    一.String概述 String被声明为final,因为它不可被继承. 内部使用char数组存储数据,该数组被声明为final,这意味着value数组初始化之后就不能再引用其他数组.并且String ...

  7. Spring Boot 集成 PageHelper

    配置一:在 [pom.xml] 文件中引入依赖 <!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pag ...

  8. sql 容易被忽视的点

    1 dual select查询语句只有select就可以,但为了规范,凑结构,可以加个dual 例:select now() from dual; 这个概念是Oracle中的.在mysql中可写可不写 ...

  9. css3弹性伸缩和使用

    columns  分栏 column的中文意思就是栏的意思,在html中,作用是分列,把一块内容相同比例均匀的分成一块一块的列,想报纸的内容似的,一篇文章在一张内容上分成好几栏那样显示,它的属性有 1 ...

  10. CSS中的定位机制

    CSS3 中有三种定位机制 : 普通文档流 (text)| 浮动(float) | 定位(position) 普通文档流 就是CSS中默认的文本文档 普通流中,元素位置由文档顺序和元素性质决定,块级元 ...