LeetCode(260) Single Number III
题目
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;
}
};
LeetCode(260) Single Number III的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
随机推荐
- Spark Mllib里如何程序输出数据集的条数(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集
- 仙人掌(cactus)
题目描述LYK 在冲刺清华集训(THUSC)!于是它开始研究仙人掌,它想来和你一起分享它最近研究的结果.如果在一个无向连通图中任意一条边至多属于一个简单环(简单环的定义为每个点至多经过一次),且不存 ...
- angular2 基于webpack环境搭建
目录结构: angular-quickstart |_ ts |_ app.ts |_ index.ts |_ index.html |_ package.json |_ tsconfig.json ...
- js动态更换img的src问题
在本地开发测试过程中,通过js动态更换img的src没有问题,图片正常切换,但是放在服务器上后测试发现,图片不显示,解决方法为:在对应onclick事件执行切换图片的js函数后加上一个return f ...
- JAVA基础之Properties类、序列化流及打印流、commons-IO
个人理解: Properties类是个存储String类型的键值对的集合类,可以用其存储一些关键的账号密码什么的,同时后面的注释可以很好的帮助理解,但是需要注意的是其文件中不能出现其他的符号:序列化与 ...
- asp.net mvc整合Nhibernate的配置方法
http://blog.csdn.net/xz2001/article/details/8452794 http://www.cnblogs.com/GoodHelper/archive/2011/0 ...
- rest_framework之视图
写一个出版社的增删改查restful接口 models from django.db import models # Create your models here. from django.db i ...
- 在Office 365 添加就地保留用户邮箱
基于客户需求,要求将用户批量添加到Office 365中的现有就地保留.如您所了解的,我们可以通过Exchange在线图形用户GUI界面完成,也可以通过PowerShell完成. 要将用户批量添加到O ...
- node执行cmd命令方法
var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...
- ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决【转】
编写Winform程序,遇到comboBox的绑定事件和索引项变更事件的冲突问题,就是“设置 DataSource 属性后无法修改项集合”的错误问题,网上查了很多,大多说在索引项变更是进行非空判断,还 ...