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. ...
随机推荐
- IIS 在 Windows 上托管 .NET Core2.0
使用 IIS 在 Windows 上托管 ASP.NET Core2.0 https://www.cnblogs.com/sundar/p/9195550.html 阅读目录 准备: 第一步:新建项目 ...
- C#远程连接sqlserver时,尝试读取或写入受保护的内存
管理员身份运行 cmd -> 输入 netsh winsock reset
- 使用MRUnit对MapReduce进行单元测试
1. 为什么需要单元测试 一旦MapReduce项目提交到集群之后,若是出现问题是很难定位和修改的,只能通过打印日志的方式进行筛选.又如果数据和项目较大时,修改起来则更加麻烦.所以,在将MapRedu ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 9865: illegal multibyte sequence 解决办法
解决办法 ; a=r.text.replace('\xa0',' ') 详情参见https://stackoverflow.com/questions/10993612/python-removing ...
- Echarts的重点
官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...
- Android ORM对象关系映射之GreenDAO建立多表关联
https://blog.csdn.net/u010687392/article/details/48496299 利用GreenDAO可以非常方便的建立多张表之间的关联 一对一关联 通常我们在操作数 ...
- tomcat配置https 和 http强制跳转https
https是http+ssl的可进行加密传输,身份认证的网络协议,防止数据在传输过程中被窃取.因此,https将得到越来越广泛的应用,下面是如何配置tomcat服务器让http自动转到https的步骤 ...
- iOS 应用架构 (二)
iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第二篇,主要讲 View 层的组织和调用方案.上篇主要讲 View ...
- reactnative资源
http://facebook.github.io/react-native/docs/getting-started.html
- vue分环境打包配置不同命令
1.安装cross-env (cross-env能跨平台地设置及使用环境变量)cnpm/npm i cross-env -D 2.新建模板 红色的为相关文件 3.配置各个文件 (1)config下 ...