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. ...
随机推荐
- Net Core开源日志框架
Net Core开源日志框架 Exceptionless - .Net Core开源日志框架 作者:markjiang7m2原文地址:https://www.cnblogs.com/markjiang ...
- net core WebApi 使用Swagger
Asp.net core WebApi 使用Swagger生成帮助页 最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core ...
- java课后思考题(五)
1.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件. import java.io.IOException;import java.nio.fil ...
- 快速开启MySQL慢日志查询的方法
MySQL慢日志查询对于很多刚接触MySQL数据的新人来说比较陌生,下面就为您介绍MySQL慢日志查询的用法和好处,供您参考. mysql有一个功能就是可以log下来运行的比较慢的sql语句,默认是 ...
- 值类型 VS 引用类型~
问 题 值 类 型 引 用 类 型 这个类型分配在哪里? 分配在栈上 分配在托管堆上 变量是怎么表示的? 值类型变量是局部复制 引用类型变量指向被分配得实例所占的内存 基类型是什么? 必须 ...
- VirtualBox中出现UUID have already exists ,并且数字键盘numlock效果相反
原文地址:https://www.cnblogs.com/xqzt/p/5053338.html 原因:由于linux密码登录错误,修改也报错误,所以只能重新安装虚拟机并在其中安装镜像文件,但是安装镜 ...
- 对于拼接进去的html原来绑定的jq事件失效
JQ拼接显示的页面中鼠标事件失效 由于是先加载html在用js层绑定的所有后来加进来的html内容就不再绑定js了 所以我们需要利用delegate绑定,但是同样道理也不能写在普通的方法层里,因为这样 ...
- 读Linear Algebra -- Gilbert Strang
转眼间我的学士学位修读生涯已经快要到期了,重读线性代数,一是为了重新理解Algebra的的重要概念以祭奠大一刷过的计算题,二是为了将来的学术工作先打下一点点(薄弱的)基础.数学毫无疑问是指导着的科研方 ...
- 创建XML的用法
注意:在实际开发中,注意createElement().createAttribute().createTextNode().appendchild()等方法的具体使用. // root根节点的属性数 ...
- SQLServer 2012 报表服务部署配置(1)
由于最近客户项目中,一直在做SQL Server 方面配置.就给大家概况简述一下 报表服务安装及遇到问题.安装和运行 SQL Server 2012 的微软原厂都有最低硬件和软件要求,对于我们大多数新 ...