Skip to content

Commit 04cb2f7

Browse files
committed
142_Linked_List_Cycle_II
143_Reorder_List 144_Binary_Tree_Preorder_Traversal 145_Binary_Tree_Postorder_Traversal 146_LRU_Cache
1 parent 3265197 commit 04cb2f7

6 files changed

+230
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/137_Single_Number_II.py) | 1. ctypes 32 % 3 and &, O(n) and O(1)<br>2. ones, twos, threes as bitmask (e.g. ones represents ith bit had appeared once), O(n) and O(1) |
4242
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/138_Copy_List_with_Random_Pointer.py) | 1. Hash O(n) and O(n)<br>2. Modify original structure: Original->Copy->Original, then node.next.random = node.random.next, O(n) and O(1) |
4343
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/.py) | 1. Hash or set<br> 2. Two points (fast and slow)<br>3. Add a max and check if reach the max |
44+
| 142 | [Linked List Cycle II](https://discuss.leetcode.com/topic/2975/o-n-solution-by-using-two-pointers-without-change-anything) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/142_Linked_List_Cycle_II.py) | Two points, a+b=nr |
45+
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/143_Reorder_List.py) | 1. List as index to rebuild relation, O(n) and O(n)<br>2. Two points, O(n) and O(1) |
46+
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/144_Binary_Tree_Preorder_Traversal.py) | 1. Recursion, O(n) and O(n)<br>2. Stack, O(n) and O(n) |
47+
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/145_Binary_Tree_Postorder_Traversal.py) | 1. Recursion, O(n) and O(n)<br>2. Stack, O(n) and O(n) |
48+
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/146_LRU_Cache.py) | 1. Queue and dict<br>2. OrderedDict |
4449
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/150_Evaluate_Reverse_Polish_Notation.py) | Stack |
4550
| 151 | [Reverse Words in a String](https://discuss.leetcode.com/category/159/reverse-words-in-a-string) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/151_Reverse_Words_in_a_String.py)| 1. Python split by space <br>2. Reverse all and reverse words |
4651
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/155_Min_Stack.py) | Add another stack for min stack, maintance this stack when the main stack pop or push |

python/142_Linked_List_Cycle_II.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def detectCycle(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
# Two points
14+
# https://discuss.leetcode.com/topic/2975/o-n-solution-by-using-two-pointers-without-change-anything
15+
try:
16+
fast = head.next.next
17+
slow = head.next
18+
19+
while fast != slow:
20+
fast = fast.next.next
21+
slow = slow.next
22+
except:
23+
return None
24+
slow = head
25+
while fast != slow:
26+
fast = fast.next
27+
slow = slow.next
28+
return fast
29+

python/143_Reorder_List.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
# def reorderList(self, head):
9+
# """
10+
# :type head: ListNode
11+
# :rtype: void Do not return anything, modify head in-place instead.
12+
# """
13+
# # List as index to rebuild relation
14+
# if not head:
15+
# return
16+
# dmap = []
17+
# current = head
18+
# while current is not None:
19+
# dmap.append(current)
20+
# current = current.next
21+
# ls = len(dmap)
22+
# for i in range(ls / 2):
23+
# t = -1 * (i + 1)
24+
# dmap[t].next = dmap[i].next
25+
# dmap[i].next = dmap[t]
26+
# dmap[ls / 2].next = None
27+
28+
def reorderList(self, head):
29+
# Two points
30+
if head is None or head.next is None:
31+
return
32+
p1, p2 = head, head.next
33+
while p2 and p2.next:
34+
p1 = p1.next
35+
p2 = p2.next.next
36+
head2 = p1.next
37+
p1.next = None
38+
p2 = head2.next
39+
head2.next = None
40+
# reverse mid->end to end->mid
41+
while p2:
42+
temp = p2.next
43+
p2.next = head2
44+
head2 = p2
45+
p2 = temp
46+
p1, p2 = head, head2
47+
# merge
48+
while p1:
49+
temp = p1.next
50+
p1.next = p2
51+
p1 = p1.next
52+
p2 = temp
53+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
# def __init__(self):
10+
# self.result = []
11+
#
12+
# def preorderTraversal(self, root):
13+
# """
14+
# :type root: TreeNode
15+
# :rtype: List[int]
16+
# """
17+
# if root is None:
18+
# return []
19+
# self.preorderTraversalHelper(root)
20+
# return self.result
21+
#
22+
# def preorderTraversalHelper(self, node):
23+
# if node is None:
24+
# return
25+
# self.result.append(node.val)
26+
# self.preorderTraversalHelper(node.left)
27+
# self.preorderTraversalHelper(node.right)
28+
29+
def preorderTraversal(self, root):
30+
# stack
31+
if root is None:
32+
return []
33+
res = []
34+
stack = [root]
35+
while len(stack) > 0:
36+
curr = stack.pop()
37+
res.append(curr.val)
38+
if curr.right is not None:
39+
stack.append(curr.right)
40+
if curr.left is not None:
41+
stack.append(curr.left)
42+
return res
43+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
# def postorderTraversal(self, root):
10+
# """
11+
# :type root: TreeNode
12+
# :rtype: List[int]
13+
# """
14+
# # Recursion
15+
# if root is None:
16+
# return []
17+
# res = []
18+
# self.postorderHelp(root, res)
19+
# return res
20+
#
21+
# def postorderHelp(self, node, stack):
22+
# if node is None:
23+
# return
24+
# self.postorderHelp(node.left, stack)
25+
# self.postorderHelp(node.right, stack)
26+
# stack.append(node.val)
27+
28+
def postorderTraversal(self, root):
29+
# Stack
30+
if root is None:
31+
return []
32+
res = []
33+
stack = [root]
34+
while len(stack) > 0:
35+
curr = stack.pop()
36+
res.insert(0, curr.val)
37+
if curr.left is not None:
38+
stack.append(curr.left)
39+
if curr.right is not None:
40+
stack.append(curr.right)
41+
return res

python/146_LRU_Cache.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class LRUCache:
2+
def __init__(self, capacity):
3+
"""
4+
:type capacity: int
5+
"""
6+
self.capacity = capacity
7+
self.cache = {}
8+
self.queue = []
9+
10+
def updateQueue(self, key):
11+
self.queue.remove(key)
12+
self.queue.insert(0, key)
13+
14+
def get(self, key):
15+
"""
16+
:rtype: int
17+
"""
18+
if key in self.cache:
19+
self.updateQueue(key)
20+
return self.cache[key]
21+
else:
22+
return -1
23+
24+
def set(self, key, value):
25+
"""
26+
:type key: int
27+
:type value: int
28+
:rtype: nothing
29+
"""
30+
if not key or not value:
31+
return None
32+
if key in self.cache:
33+
self.queue.remove(key)
34+
elif len(self.queue) == self.capacity:
35+
del self.cache[self.queue.pop(-1)]
36+
37+
self.cache[key] = value
38+
self.queue.insert(0, key)
39+
40+
# def __init__(self, capacity):
41+
# self.dic = collections.OrderedDict()
42+
# self.remain = capacity
43+
#
44+
# def get(self, key):
45+
# if key not in self.dic:
46+
# return -1
47+
# v = self.dic.pop(key)
48+
# self.dic[key] = v # set key as the newest one
49+
# return v
50+
#
51+
# def set(self, key, value):
52+
# if key in self.dic:
53+
# self.dic.pop(key)
54+
# else:
55+
# if self.remain > 0:
56+
# self.remain -= 1
57+
# else: # self.dic is full
58+
# self.dic.popitem(last=False)
59+
# self.dic[key] = value

0 commit comments

Comments
 (0)