File tree Expand file tree Collapse file tree 3 files changed +161
-0
lines changed
Expand file tree Collapse file tree 3 files changed +161
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List , Tuple
2+
3+
4+ class MinStack :
5+ """
6+ stack
7+ (-3, -3)
8+ (0, -2)
9+ (-2, -2)
10+ """
11+
12+ def __init__ (self ):
13+ self .stack : List [Tuple [int , int ]] = []
14+
15+ def push (self , val : int ) -> None :
16+ if len (self .stack ) == 0 :
17+ self .stack .append ((val , val ))
18+ else :
19+ cur_mins = self .stack [- 1 ][1 ]
20+ if val < cur_mins :
21+ self .stack .append ((val , val ))
22+ else :
23+ self .stack .append ((val , cur_mins ))
24+
25+ def pop (self ) -> None :
26+ self .stack = self .stack [:- 1 ]
27+
28+ def top (self ) -> int :
29+ if self .stack :
30+ return self .stack [- 1 ][0 ]
31+
32+ def getMin (self ) -> int :
33+ if self .stack :
34+ return self .stack [- 1 ][1 ]
35+
36+
37+ if __name__ == "__main__" :
38+ # Your MinStack object will be instantiated and called as such:
39+ obj = MinStack ()
40+ obj .push (- 2 )
41+ obj .push (0 )
42+ obj .push (- 3 )
43+ assert obj .getMin () == - 3
44+ obj .pop ()
45+ assert obj .top () == 0
46+ assert obj .getMin () == - 2
Original file line number Diff line number Diff line change 1+ class Trie :
2+ """
3+ root {}
4+
5+ a {a: {}}
6+
7+ p {a: {p: {}}}
8+
9+ p {a: {p: {p: {}}}}
10+
11+ l {a: {p: {p: {l: {}}}}}
12+
13+ e {a: {p: {p: {l: {e: {}}}}}}
14+
15+ $ {a: {p: {p: {l: {e: {$:$}}}}}}
16+
17+ after insert app
18+ {a: {p: {p: {
19+ l: {e: {$:$}},
20+ $: $
21+ }}}}
22+ """
23+
24+ def __init__ (self ):
25+ self .root = {}
26+
27+ def insert (self , word : str ) -> None :
28+ root = self .root
29+ for char in word :
30+ if char not in root :
31+ root [char ] = {}
32+ root = root [char ]
33+ root ["$" ] = "$"
34+
35+ def search (self , word : str ) -> bool :
36+ root = self .root
37+ for char in word :
38+ if char not in root :
39+ return False
40+ root = root [char ]
41+ return root .get ("$" ) == "$"
42+
43+ def startsWith (self , prefix : str ) -> bool :
44+ root = self .root
45+ for char in prefix :
46+ if char not in root :
47+ return False
48+ root = root [char ]
49+ return True
50+
51+
52+ if __name__ == "__main__" :
53+ trie = Trie ()
54+ trie .insert ("apple" )
55+ assert trie .search ("apple" ) is True
56+ assert trie .search ("app" ) is False
57+ assert trie .startsWith ("app" ) is True
58+ trie .insert ("app" )
59+ assert trie .search ("app" ) is True
Original file line number Diff line number Diff line change 1+ class MyQueue :
2+ """
3+ stack: 后入先出
4+ queue: 先入先出
5+
6+ stack1 stack2
7+
8+ | | | |
9+ | 2 | | |
10+ | 1 | | |
11+
12+ push 操作: 往 stack1 里压
13+ peek 操作:
14+ 1. stack2 空: stack1 所有出栈并依次压入 stack2, 返回 stack2 栈顶
15+ 2. stack2 非空: 直接返回 stack2 栈顶
16+ pop 操作:
17+ 1. stack2 空: stack1 所有出栈并依次压入 stack2, 弹出 stack2 栈顶
18+ 2. stack2 非空: 直接弹出 stack2 栈顶
19+ empty:
20+ stack2 is empty and stack1 is empty
21+ """
22+
23+ def __init__ (self ):
24+ self .stack1 = []
25+ self .stack2 = []
26+
27+ def push (self , x : int ) -> None :
28+ self .stack1 .append (x )
29+
30+ def pop (self ) -> int :
31+ ans = self .peek ()
32+ self .stack2 = self .stack2 [:- 1 ]
33+ return ans
34+
35+ def peek (self ) -> int :
36+ if len (self .stack2 ) == 0 :
37+ self .stack2 = self .stack1 [::- 1 ]
38+ self .stack1 = []
39+ if len (self .stack2 ) > 0 :
40+ return self .stack2 [- 1 ]
41+
42+ def empty (self ) -> bool :
43+ return len (self .stack1 ) == len (self .stack2 ) == 0
44+
45+
46+ if __name__ == "__main__" :
47+ # Your MyQueue object will be instantiated and called as such:
48+ obj = MyQueue ()
49+ obj .push (1 )
50+ obj .push (2 )
51+ assert obj .peek () == 1
52+ assert obj .pop () == 1
53+ assert obj .empty () is False
54+ assert obj .pop () == 2
55+ assert obj .pop () is None
56+ assert obj .empty () is True
You can’t perform that action at this time.
0 commit comments