137. Single Number II

137. Single Number II

Question

Given an integer array nums where every element appears three times except for one, which appears exactly onceFind the single element and return it.

You must implement a solution with a linear runtime complexity and use only constant extra space.

Solution

用字典记录出现次数,返回只出现一次的key

Code

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def singleNumber(self, nums: List[int]) -> int:
m = {}
for i in range(len(nums)):
if nums[i] in m:
m[nums[i]] += 1
else:
m[nums[i]] = 1
for key in m:
if m[key] == 1:
return key
Author

Xander

Posted on

2023-07-03

Updated on

2023-07-03

Licensed under

Comments