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. ...
随机推荐
- 安装Jaspersoft Studio
下载位置:http://community.jaspersoft.com/project/jaspersoft-studio/releases.
- HTML——传统布局的使用
传统布局:使用table来做整体页面的布局 总结:这种方式来制作页面现在也不是很多了,感觉并不是很高效. 需要先用photoshop量出页面布局具体的尺寸位置,再将其划分为表格,对每个格子进行编辑. ...
- Apache Atlas是什么?
不多说,直接上干货! Apache Atlas是Hadoop社区为解决Hadoop生态系统的元数据治理问题而产生的开源项目,它为Hadoop集群提供了包括数据分类.集中策略引擎.数据血缘.安全和生命周 ...
- vconsole移动端调试技巧(禁止webviuew,inspect等)
如果由于某种原因(天朝FQ),不能支持google 的 inspect 调试 或者再想在某个APP里面调试你的页面,但是没有打开APP的webview ,也不能授权调试 在或者,Fider 可以拦截 ...
- 不小心踩到的XMAPP的N种问题
1.在win10上的xampp集成环境中安装mongo扩展 按照网上搜索的下载对应文件后,在phpinfo里面还是找不到mongo的扩展信息,后面也是请教同事帮忙解决: http://www.theg ...
- SQL查询-约束-多表
一.SQL语句查询 1.聚合函数 COUNT()函数,统计表中记录的总数量 注:COUNT()返回的值为Long类型;可通过Long类的intValue()方法 ...
- C#字段声明部分如何调用该类中的方法进行初始化?
问题描述: 有时,功能需求,需要在初始化字段时,需要视不同情况赋予不同字段值. 解决办法: 将方法设为static即可. e.g. public string str = SetStr(); publ ...
- 链接服务器"(null)"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 "Cannot start more transactions on this session."
开发同事反馈一个SQL Server存储过程执行的时候,报"链接服务器"(null)"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 ...
- Outlook 客户端无法通过 MAPI over HTTP协议 连接
随着Exchange 版本更新升级,是否进行验证客户端建立MapiHttp连接所需的服务器设置已正确配置.即使服务器,负载均衡器和反向代理的所有设置都正确,您可能会遇到连接到Exchange Serv ...
- 洛谷 P1588 丢失的牛
题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接走到2*x的位置.计算他 ...