-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc39c5f
commit 8557962
Showing
8 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
pair = {"(":")", ")":"(", | ||
"{":"}", "}":"{", | ||
"[":"]", "]":"["} | ||
|
||
opening = ['(', '{', '['] | ||
|
||
stck = [] | ||
|
||
for char in s: | ||
if char in opening: | ||
stck.append(char) | ||
else: | ||
if not stck: | ||
return False | ||
elif stck.pop() != pair[char]: | ||
return False | ||
|
||
if stck: | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import collections | ||
class MyStack: | ||
def __init__(self): | ||
self.q = collections.deque() | ||
|
||
|
||
def push(self, x: int) -> None: | ||
self.q.append(x) | ||
for _ in range(len(self.q) - 1): | ||
self.q.append(self.q.popleft()) | ||
|
||
|
||
def pop(self) -> int: | ||
return self.q.popleft() | ||
|
||
|
||
def top(self) -> int: | ||
return self.q[0] | ||
|
||
|
||
def empty(self) -> bool: | ||
return len(self.q) == 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
import collections | ||
|
||
# recursive | ||
class Solution: | ||
def removeDuplicateLetters(self, s: str) -> str: | ||
for char in sorted(set(s)): | ||
suffix = s[s.index(char):] | ||
|
||
if set(s) == set(suffix): | ||
return char + self.removeDuplicateLetters(suffix.replace(char, '')) | ||
|
||
return '' | ||
|
||
|
||
class Solution2: | ||
def removeDuplicateLetters(self, s: str) -> str: | ||
counter, seen, stack = collections.Counter(s), set(), [] | ||
|
||
for char in s: | ||
counter[char] -= 1 | ||
if char in seen: | ||
continue | ||
|
||
while stack and char < stack[-1] and counter[stack[-1]] > 0: | ||
seen.remove(stack.pop()) | ||
|
||
stack.append(char) | ||
seen.add(char) | ||
|
||
return ''.join(stack) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class MyCircularQueue: | ||
|
||
def __init__(self, k: int): | ||
self.q = [None] * k | ||
self.maxlen = k | ||
self.p1 = 0 | ||
self.p2 = 0 | ||
|
||
def enQueue(self, value: int) -> bool: | ||
if self.q[self.p2] == None: | ||
self.q[self.p2] = value | ||
self.p2 = (self.p2 + 1) % self.maxlen | ||
return True | ||
else: | ||
return False | ||
|
||
def deQueue(self) -> bool: | ||
if self.q[self.p1] is None: | ||
return False | ||
else: | ||
self.q[self.p1] = None | ||
self.p1 = (self.p1 + 1) % self.maxlen | ||
return True | ||
|
||
|
||
def Front(self) -> int: | ||
return -1 if self.q[self.p1] is None else self.q[self.p1] | ||
|
||
def Rear(self) -> int: | ||
return -1 if self.q[self.p2 - 1] is None else self.q[self.p2 - 1] | ||
|
||
def isEmpty(self) -> bool: | ||
return self.p1 == self.p2 and self.q[self.p1] is None | ||
|
||
def isFull(self) -> bool: | ||
return self.p1 == self.p2 and self.q[self.p1] is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def dailyTemperatures(self, T: List[int]) -> List[int]: | ||
answer = [0] * len(T) | ||
stack = [] | ||
for i, cur in enumerate(T): | ||
while stack and cur > T[stack[-1]]: | ||
last = stack.pop() | ||
answer[last] = i - last | ||
stack.append(i) | ||
|
||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ListNode: | ||
def __init__(self, item, next = None): | ||
self.item = item | ||
self.next = next | ||
|
||
|
||
head = ListNode(0) | ||
head.next = ListNode(1) | ||
head.next.next = ListNode(2) | ||
|
||
while head: | ||
print(head.item) | ||
head = head.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Implement stack using linked list | ||
|
||
class Node: | ||
def __init__(self, item, next): | ||
self.item = item | ||
self.next = next | ||
|
||
|
||
class Stack: | ||
def __init__(self): | ||
self.last = None | ||
|
||
def push(self, item): | ||
self.last = Node(item, self.last) | ||
|
||
def pop(self): | ||
item = self.last.item | ||
self.last = self.last.next | ||
return item |