Skip to content

Commit f1df81c

Browse files
committed
python_145
1 parent a14026d commit f1df81c

14 files changed

+425
-47
lines changed
Binary file not shown.

Python/delete-node-in-a-linked-list.py

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def deleteNode(self, node):
3636
:type node: ListNode
3737
:rtype: void Do not return anything, modify node in-place instead.
3838
"""
39+
# 首先,如果时间复杂度为O(1),那么以下爱代码是基于删除的节点必定再到航前链表中为假设前提的。
40+
# 分为两种情况,
41+
# 1、如果要删除的结点不是尾节点,可以直接删除。
42+
# 2、否则还是需要遍历到链表的末尾,这是最坏情况。
3943

4044
node.val = node.next.val
4145
node.next = node.next.next
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
3+
4+
Implement the following operations of a queue using stacks.
5+
6+
push(x) -- Push element x to the back of queue.
7+
pop() -- Removes the element from in front of queue.
8+
peek() -- Get the front element.
9+
empty() -- Return whether the queue is empty.
10+
Example:
11+
12+
MyQueue queue = new MyQueue();
13+
14+
queue.push(1);
15+
queue.push(2);
16+
queue.peek(); // returns 1
17+
queue.pop(); // returns 1
18+
queue.empty(); // returns false
19+
Notes:
20+
21+
You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
22+
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
23+
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
24+
25+
26+
'''
27+
28+
29+
30+
class MyQueue:
31+
32+
def __init__(self):
33+
"""
34+
Initialize your data structure here.
35+
"""
36+
self.stack = []
37+
38+
39+
40+
def push(self, x: int) -> None:
41+
"""
42+
Push element x to the back of queue.
43+
"""
44+
self.stack.append(x)
45+
46+
47+
def pop(self) -> int:
48+
"""
49+
Removes the element from in front of queue and returns that element.
50+
"""
51+
if self.stack: return self.stack.pop(0)
52+
else: return None
53+
54+
55+
def peek(self) -> int:
56+
"""
57+
Get the front element.
58+
"""
59+
60+
if self.stack: return self.stack[0]
61+
else: return None
62+
63+
def empty(self) -> bool:
64+
"""
65+
Returns whether the queue is empty.
66+
"""
67+
68+
if self.stack == []: return True
69+
else: return False
70+
71+
72+
73+
74+
# Your MyQueue object will be instantiated and called as such:
75+
# obj = MyQueue()
76+
# obj.push(x)
77+
# param_2 = obj.pop()
78+
# param_3 = obj.peek()
79+
# param_4 = obj.empty()
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'''
2+
Implement the following operations of a stack using queues.
3+
4+
push(x) -- Push element x onto stack.
5+
pop() -- Removes the element on top of the stack.
6+
top() -- Get the top element.
7+
empty() -- Return whether the stack is empty.
8+
Example:
9+
10+
MyStack stack = new MyStack();
11+
12+
stack.push(1);
13+
stack.push(2);
14+
stack.top(); // returns 2
15+
stack.pop(); // returns 2
16+
stack.empty(); // returns false
17+
Notes:
18+
19+
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
20+
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
21+
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
22+
23+
'''
24+
25+
26+
class MyStack:
27+
28+
def __init__(self):
29+
"""
30+
Initialize your data structure here.
31+
"""
32+
self.q1 = []
33+
self.q2 = []
34+
35+
36+
def push(self, x: int) -> None:
37+
"""
38+
Push element x onto stack.
39+
"""
40+
if self.q1: self.q1.append(x)
41+
else: self.q2.append(x)
42+
43+
def pop(self) -> int:
44+
"""
45+
Removes the element on top of the stack and returns that element.
46+
"""
47+
if self.q1:
48+
while len(self.q1) > 1:
49+
self.q2.append(self.q1.pop(0))
50+
return self.q1.pop(0)
51+
else:
52+
while len(self.q2) > 1:
53+
self.q1.append(self.q2.pop(0))
54+
return self.q2.pop(0)
55+
56+
57+
58+
def top(self) -> int:
59+
"""
60+
Get the top element.
61+
"""
62+
if self.q1:
63+
while len(self.q1) > 1:
64+
self.q2.append(self.q1.pop(0))
65+
key = self.q1.pop(0)
66+
self.q2.append(key)
67+
return key
68+
else:
69+
while len(self.q2) > 1:
70+
self.q1.append(self.q2.pop(0))
71+
key = self.q2.pop(0)
72+
self.q1.append(key)
73+
return key
74+
75+
def empty(self) -> bool:
76+
"""
77+
Returns whether the stack is empty.
78+
"""
79+
return self.q1 == [] and self.q2 == []
80+
81+
82+
83+
# Your MyStack object will be instantiated and called as such:
84+
# obj = MyStack()
85+
# obj.push(x)
86+
# param_2 = obj.pop()
87+
# param_3 = obj.top()
88+
# param_4 = obj.empty()
89+
90+
91+
92+
# Your MyStack object will be instantiated and called as such:
93+
# obj = MyStack()
94+
# obj.push(x)
95+
# param_2 = obj.pop()
96+
# param_3 = obj.top()
97+
# param_4 = obj.empty()

Python/invert-binary-tree.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929

3030

31-
3231
# Definition for a binary tree node.
3332
# class TreeNode:
3433
# def __init__(self, x):
@@ -37,12 +36,30 @@
3736
# self.right = None
3837

3938
class Solution:
40-
def invertTree(self, root):
41-
"""
42-
:type root: TreeNode
43-
:rtype: TreeNode
44-
"""
45-
# method one 一个漂亮的递归(把一个复杂的问题逐步地剥开)
46-
if root:
47-
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
39+
def invertTree(self, root: TreeNode) -> TreeNode:
40+
41+
# Approach one 递归求解
42+
# if not root : return None
43+
# root.right , root.left = self.invertTree(root.left), self.invertTree(root.right)
44+
# return root
45+
46+
47+
# Approach two 栈+循环
48+
# if not root : return None
49+
# stack = [root]
50+
# while stack:
51+
# node = stack.pop()
52+
# node.left , node.right = node.right , node.left
53+
# if node.left : stack.append(node.left)
54+
# if node.right: stack.append(node.right)
55+
# return root
56+
57+
# Approach three 队列也是一样
58+
if not root : return None
59+
q = [root]
60+
while q:
61+
node = q.pop()
62+
node.left , node.right = node.right , node.left
63+
if node.left : q.append(node.left)
64+
if node.right: q.append(node.right)
4865
return root

Python/maximum-depth-of-binary-tree.py

+3
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ def maxDepth(self, root):
3333
:type root: TreeNode
3434
:rtype: int
3535
"""
36+
# 尾递归的程序递归是最后一步,进入递归之后相当于进入了一个新的函数,与当前的函数环境不再交互,不用担心栈溢出,比普通递归消耗的资源较少。
37+
# 所以此函数不是尾递归
38+
3639
if not root : return 0
3740
return max(self.maxDepth(root.left), self.maxDepth(root.right))+1

Python/merge-k-sorted-lists.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
3+
4+
Example:
5+
6+
Input:
7+
[
8+
1->4->5,
9+
1->3->4,
10+
2->6
11+
]
12+
Output: 1->1->2->3->4->4->5->6
13+
14+
'''
15+
16+
17+
# Definition for singly-linked list.
18+
# class ListNode:
19+
# def __init__(self, x):
20+
# self.val = x
21+
# self.next = None
22+
23+
class Solution:
24+
# Approach one 分治的思想两两合并 O(NlogK) , O(1)
25+
# 想想二叉树,K个有序链表合并可以变成k-1次的“两个有序链表合并问题”
26+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
27+
28+
29+
# def merge2lists(l1,l2):
30+
# if not l1: return l2
31+
# if not l2: return l1
32+
# if l1.val <= l2.val:
33+
# l1.next = merge2lists(l1.next , l2)
34+
# return l1
35+
# else:
36+
# l2.next = merge2lists(l1 , l2.next)
37+
# return l2
38+
39+
40+
41+
def merge2lists(l1,l2):
42+
res = cur = ListNode(None)
43+
while l1 and l2:
44+
if l1.val <= l2.val:
45+
cur.next , l1 = l1 , l1.next
46+
else:
47+
cur.next , l2 = l2 , l2.next
48+
cur = cur.next
49+
cur.next = l1 if l1 else l2
50+
return res.next
51+
52+
while len(lists) > 1:
53+
lists.append(merge2lists(lists.pop(0),lists.pop(0)))
54+
return lists[0] if lists else None # 注意输入为空

Python/merge-two-sorted-lists.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,22 @@ def mergeTwoLists(self, l1, l2):
2727
"""
2828

2929
# Approach one 递归的方法
30-
# res = None
3130
# if l1 == None:
3231
# return l2
3332
# elif l2 == None:
3433
# return l1
35-
3634
# if l2.val >= l1.val:
37-
# res = l1
38-
# res.next = self.mergeTwoLists(l1.next, l2)
35+
# l1.next = self.mergeTwoLists(l1.next, l2)
36+
# return l1
3937
# else:
40-
# res = l2
41-
# res.next = self.mergeTwoLists(l1, l2.next)
42-
43-
# return res
44-
38+
# l2.next = self.mergeTwoLists(l1, l2.next)
39+
# return l2
4540

4641

4742

48-
# Approach two 牺牲存储空间,换取时间
49-
head = ListNode(0)
50-
curr = head
5143

44+
# Approach two # 循环的解法需要两个新头结点,并注意拼接尾链
45+
head = curr = ListNode(0)
5246
while l1 and l2:
5347
if l1.val >= l2.val:
5448
curr.next = l2
@@ -57,9 +51,5 @@ def mergeTwoLists(self, l1, l2):
5751
curr.next = l1
5852
l1 = l1.next
5953
curr = curr.next
60-
if l1 == None:
61-
curr.next = l2
62-
if l2 == None:
63-
curr.next = l1
64-
54+
curr.next = l1 if l1 else l2
6555
return head.next

Python/number-of-1-bits.py

+24
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ def hammingWeight(self, n):
3636
n &= n - 1
3737
result += 1
3838
return result
39+
40+
41+
# 复数的补码前面自动填充1
42+
# 方法一
43+
# return sum([(n>>i & 1) for i in range(0,32)])
44+
45+
# 方法二
46+
# 标志位一路左移,做“与”操作
47+
#flag = 1
48+
#count = 0
49+
#for _ in range(32):
50+
# if flag & n: count += 1
51+
# flag = flag << 1
52+
#return count
53+
54+
55+
# python中 -4294967296 的二进制位 Ob0, 虽然一个负数在做了若干次“与”操作后,二进制为零,但是其十进制数是一个越来越小的负数
56+
# 因此在采用,“n与(n-1)做与操作恰好去掉n的最右位1”,这一性质时,终止条件不能以十进制来表示,否则是个死循环。
57+
# 方法三
58+
count = 0
59+
while n & 0xffffffff != 0:
60+
count += 1
61+
n = n & (n - 1)
62+
return count

0 commit comments

Comments
 (0)