【LeetCode題解】136_只出現一次的數字
目錄
描述
給定一個非空整數數組,除了某個元素只出現一次以外,其余每個元素均出現兩次。找出那個只出現了一次的元素。
說明:
你的算法應該具有線性時間復雜度。 你可以不使用額外空間來實現嗎?
示例 1:
輸入: [2,2,1]
輸出: 1
示例 2:
輸入: [4,1,2,1,2]
輸出: 4
方法一:列表操作
思路
新建一個變量(列表對象),遍歷數組中的所有元素,如果元素在列表中存在,則刪除該元素;如果元素在列表中不存在,則向列表中添加該元素。最后,列表中剩余的唯一元素就是我們找的唯一只出現一次的數字。
Java 實現
class Solution {
public int singleNumber(int[] nums) {
List<Integer> list = new LinkedList<>();
for (int num : nums) {
if (list.contains(num)) {
list.remove(new Integer(num));
} else {
list.add(num);
}
}
return list.get(0);
}
}
復雜度分析:
- 時間復雜度:\(O(n^2)\)
- 空間復雜度:\(O(n)\)
Python 實現
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = list()
for num in nums:
if num in l:
l.remove(num)
else:
l.append(num)
return l.pop()
# 超出時間限制
復雜度分析同上。
方法二:哈希表
思路
思路和方法一相同,只是存放數據的容器改用哈希表,由於哈希表查詢操作的時間復雜度是 \(O(1)\) 的,因此,算法最終的時間復雜度降為 \(O(n)\)。
Java 實現
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num)) {
map.remove(num);
} else {
map.put(num, 1);
}
}
return map.entrySet().iterator().next().getKey();
}
}
復雜度分析:
- 時間復雜度:\(O(n)\)
- 空間復雜度:\(O(n)\)
Python 實現
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d = dict()
for num in nums:
try:
d.pop(num)
except:
d[num] = 1
return d.popitem()[0]
復雜度分析同上。
方法三:數學運算
思路
由於數組中的數字除了唯一一個數字只出現一次外,其余都是出現兩次的,因此,如果我們將所有的數字乘以 2 再減去數組中所有的元素,則可以得到只出現一次的數字,即
\[2 * (a_1 + a_2 + \ldots + a_n + a_x) - (a_1 + a_1 + \ldots + a_n + a_n + a_x) = a_x \]
其中,\(a_1\) 到 \(a_n\) 是出現兩次的數字,\(a_x\) 則是出現兩次的數字。
Java 實現
class Solution {
public int singleNumber(int[] nums) {
Set<Integer> set = new HashSet<>();
int arraySum = 0;
for (int num : nums) {
set.add(num);
arraySum += num;
}
int doubleSum = 0;
for (int num : set) {
doubleSum += (2 * num);
}
return doubleSum - arraySum;
}
}
復雜度分析:
- 時間復雜度:\(O(n)\)
- 空間復雜度:\(O(n)\)
Python 實現
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return 2 * sum(set(nums)) - sum(nums)
復雜度分析同上。
方法四:位運算
思路
先來回顧一下兩個位運算:
- 任何數與 0 異或都不改變它的值,即 \(a \oplus 0 = a\)
- 任何數與其自身異或都為 0,即 \(a \oplus a = 0\)
假設題目中描述的數組為 \([a_1,\, a_1,\, a_2,\, a_2,\, \ldots,\, a_n,\, a_n,\, a_x]\) ,其中,\(a_x\) 只出現一次,其余的元素都出現兩次。對該數組中的所有元素進行異或運算可得,
\[\begin{align*} & \,\,a_1 \oplus a_1 \oplus \ldots \oplus a_n \oplus a_n \oplus a_x \\ = & \,\,(a_1 \oplus a_1) \oplus \ldots \oplus (a_n \oplus a_n) \oplus a_x \\ = &\,\, 0 \,\oplus \ldots \oplus 0 \,\oplus a_x \\ = &\,\, a_x \end{align*} \]
因此,只需要遍歷數組中的所有元素,依次進行兩兩異或操作就可以找出只出現一次的元素。
Java 實現
class Solution {
public int singleNumber(int[] nums) {
int ret = nums[0];
for (int i = 1; i < nums.length; ++i) {
ret ^= nums[i];
}
return ret;
}
}
復雜度分析:
- 時間復雜度:\(O(n)\)
- 空間復雜度:\(O(1)\)
Python 實現
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ret = 0
for num in nums:
ret ^= num
return ret
復雜度分析同上。