[LeetCode][python3]Day28. First Unique Number (30-Day LeetCoding Challenge)

  1. My solution
class FirstUnique:

def __init__(self, nums: List[int]):
self.queue=set()
self.uni=OrderedDict()
for i in nums:
self.add(i)

def showFirstUnique(self) -> int:
return next(iter(self.uni.items()))[0] if self.uni else -1

def add(self, value: int) -> None:
if value not in self.queue:
self.uni[value]=True
self.queue.add(value)
elif value in self.uni:
del self.uni[value]
#print(self.queue,self.uni)
class FirstUnique:

def __init__(self, nums: List[int]):
self.c = Counter(nums)
self.d = deque(nums)

def showFirstUnique(self) -> int:
while self.d and self.c[self.d[0]] != 1:
self.d.popleft()
return self.d[0] if self.d else -1

def add(self, value: int) -> None:
self.c[value] += 1
self.d.append(value)

--

--

--

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introduction To Pegasus: Seed Security

Enjoying the Ride

Await for the Async

Advise on API Business Development for Corporates in Open Banking

Percona Series / Zero downtime migration from 5.7 to 8.0

Quick start with MongoDB Golang Driver

The RTE Toy Show Appeal — converting millions of viewers to millions of Euros with Serverless

Find the matching FIT file for your video

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
N2I

N2I

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

More from Medium

Implementing Linked List Operations in Python

Leetcode 1209 Remove All Adjacent Duplicates in String II

Sorting Algorithms Explained Using Python: Bucket Sort

Glass Cube and Dots

LeetCode #167 | Two Sum II — Input Array Is Sorted (Python)