def singleNumber(self, nums): for i in nums: if nums.count(i)==1: return i
Every time you call
nums.count(i), python needs to iterate across the entire list
Since you are making this call within the loop
for i in nums:, you are forcing python to iterate through the list n^2 times.
This works for short inputs, but the larger your input list the longer it will take.
Try to implement this by looping over the input list only one time.
def singleNumber(self, nums):
:type nums: List[int]
for k in nums:
if k not in words:
it's my solution by py,maybe it is not best way,but AC.
@繁华1994 you're using extra memory
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.