LeetCode/solutions/136. Single Number.md
2019-09-13 23:08:41 +08:00

24 lines
721 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [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++
## 思路二
``` C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for(int num: nums) res = res ^ num;
return res;
}
};
```