Create 136. Single Number.md

This commit is contained in:
唐树森 2018-09-12 22:06:36 +08:00 committed by GitHub
parent 534bcb7dfb
commit cb30e5543e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

23
136. Single Number.md Normal file
View File

@ -0,0 +1,23 @@
# [136. Single Number](https://leetcode.com/problems/single-number/description/)
# 思路
## 思路一
最常规的思路就是用一个map或者散列(unordered_map)记录是否出现过。
map: 时间复杂度O(nlogn), 空间复杂度O(n)
unordered_map: 时间复杂度O(n), 空间复杂度O(N)
## 思路二*
若某个数出现两次则异或操作后得0所以可以考虑将数组所有元素进行异或操作最终得到的值就是欲求值。
时间复杂度O(n), 空间复杂度O(1), 完美
# C++
## 思路二
```
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for(int num: nums) res = res ^ num;
return res;
}
};
```