Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50ad46b

Browse files
author
lucifer
committed
2 parents ec2bd02 + 7856de2 commit 50ad46b

File tree

4 files changed

+170
-22
lines changed

4 files changed

+170
-22
lines changed

problems/23.merge-k-sorted-lists.md

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
4141

4242
## 代码
4343

44+
代码支持 JavaScript, Python3
45+
46+
JavaScript Code:
47+
4448
```js
4549
/*
4650
* @lc app=leetcode id=23 lang=javascript
@@ -49,27 +53,6 @@ https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
4953
*
5054
* https://leetcode.com/problems/merge-k-sorted-lists/description/
5155
*
52-
* algorithms
53-
* Hard (33.14%)
54-
* Total Accepted: 373.7K
55-
* Total Submissions: 1.1M
56-
* Testcase Example: '[[1,4,5],[1,3,4],[2,6]]'
57-
*
58-
* Merge k sorted linked lists and return it as one sorted list. Analyze and
59-
* describe its complexity.
60-
*
61-
* Example:
62-
*
63-
*
64-
* Input:
65-
* [
66-
* 1->4->5,
67-
* 1->3->4,
68-
* 2->6
69-
* ]
70-
* Output: 1->1->2->3->4->4->5->6
71-
*
72-
*
7356
*/
7457
function mergeTwoLists(l1, l2) {
7558
const dummyHead = {};
@@ -129,6 +112,51 @@ var mergeKLists = function(lists) {
129112
};
130113
```
131114

115+
Python3 Code:
116+
117+
``` python
118+
# Definition for singly-linked list.
119+
# class ListNode:
120+
# def __init__(self, x):
121+
# self.val = x
122+
# self.next = None
123+
124+
class Solution:
125+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
126+
n = len(lists)
127+
128+
# basic cases
129+
if lenth == 0: return None
130+
if lenth == 1: return lists[0]
131+
if lenth == 2: return self.mergeTwoLists(lists[0], lists[1])
132+
133+
# divide and conqure if not basic cases
134+
mid = n // 2
135+
return self.mergeTwoLists(self.mergeKLists(lists[:mid]), self.mergeKLists(lists[mid:n]))
136+
137+
138+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
139+
res = ListNode(0)
140+
c1, c2, c3 = l1, l2, res
141+
while c1 or c2:
142+
if c1 and c2:
143+
if c1.val < c2.val:
144+
c3.next = ListNode(c1.val)
145+
c1 = c1.next
146+
else:
147+
c3.next = ListNode(c2.val)
148+
c2 = c2.next
149+
c3 = c3.next
150+
elif c1:
151+
c3.next = c1
152+
break
153+
else:
154+
c3.next = c2
155+
break
156+
157+
return res.next
158+
```
159+
132160
## 相关题目
133161

134162
-[88.merge-sorted-array](./88.merge-sorted-array.md)

problems/42.trapping-rain-water.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ for(let i = 0; i < height.length; i++) {
5252

5353
## 代码
5454

55+
代码支持 JavaScript,Python3:
56+
57+
JavaScript Code:
58+
5559
```js
5660

5761
/*
@@ -88,3 +92,30 @@ var trap = function(height) {
8892
};
8993

9094
```
95+
96+
Python Code:
97+
98+
```python
99+
100+
class Solution:
101+
def trap(self, height: List[int]) -> int:
102+
maxLeft, maxRight, volum = 0, 0, 0
103+
maxLeftStack, maxRightStack = [], []
104+
for h in height:
105+
if h > maxLeft:
106+
maxLeftStack.append(h)
107+
maxLeft = h
108+
else:
109+
maxLeftStack.append(maxLeft)
110+
for h in height[::-1]:
111+
if h > maxRight:
112+
maxRightStack.append(h)
113+
maxRight = h
114+
else:
115+
maxRightStack.append(maxRight)
116+
maxRightStack = maxRightStack[::-1]
117+
for i in range(1, len(height) - 1):
118+
minSide = min(maxLeftStack[i], maxRightStack[i])
119+
volum += minSide - height[i]
120+
return volum
121+
```

problems/445.add-two-numbers-ii.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Output: 7 -> 8 -> 0 -> 7
3737

3838
## 代码
3939

40-
- 语言支持:JS,C++
40+
- 语言支持:JS,C++, Python3
4141

4242
JavaScript Code:
4343

@@ -210,3 +210,50 @@ private:
210210
}
211211
};
212212
```
213+
214+
Python Code:
215+
216+
```python
217+
# Definition for singly-linked list.
218+
# class ListNode:
219+
# def __init__(self, x):
220+
# self.val = x
221+
# self.next = None
222+
223+
class Solution:
224+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
225+
def listToStack(l: ListNode) -> list:
226+
stack, c = [], l
227+
while c:
228+
stack.append(c.val)
229+
c = c.next
230+
return stack
231+
232+
# transfer l1 and l2 into stacks
233+
stack1, stack2 = listToStack(l1), listToStack(l2)
234+
235+
# add stack1 and stack2
236+
diff = abs(len(stack1) - len(stack2))
237+
stack1 = ([0]*diff + stack1 if len(stack1) < len(stack2) else stack1)
238+
stack2 = ([0]*diff + stack2 if len(stack2) < len(stack1) else stack2)
239+
stack3 = [x + y for x, y in zip(stack1, stack2)]
240+
241+
# calculate carry for each item in stack3 and add one to the item before it
242+
carry = 0
243+
for i, val in enumerate(stack3[::-1]):
244+
index = len(stack3) - i - 1
245+
carry, stack3[index] = divmod(val + carry, 10)
246+
if carry and index == 0:
247+
stack3 = [1] + stack3
248+
elif carry:
249+
stack3[index - 1] += 1
250+
251+
# transfer stack3 to a linkedList
252+
result = ListNode(0)
253+
c = result
254+
for item in stack3:
255+
c.next = ListNode(item)
256+
c = c.next
257+
258+
return result.next
259+
```

problems/73.set-matrix-zeroes.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,48 @@ var setZeroes = function(matrix) {
179179
};
180180
```
181181
Python3 Code:
182+
183+
直接修改第一行和第一列为0的解法:
184+
```python
185+
class Solution:
186+
def setZeroes(self, matrix: List[List[int]]) -> None:
187+
"""
188+
Do not return anything, modify matrix in-place instead.
189+
"""
190+
def setRowZeros(matrix: List[List[int]], i:int) -> None:
191+
C = len(matrix[0])
192+
matrix[i] = [0] * C
193+
194+
def setColZeros(matrix: List[List[int]], j:int) -> None:
195+
R = len(matrix)
196+
for i in range(R):
197+
matrix[i][j] = 0
198+
199+
isCol = False
200+
R = len(matrix)
201+
C = len(matrix[0])
202+
203+
for i in range(R):
204+
if matrix[i][0] == 0:
205+
isCol = True
206+
for j in range(1, C):
207+
if matrix[i][j] == 0:
208+
matrix[i][0] = 0
209+
matrix[0][j] = 0
210+
for j in range(1, C):
211+
if matrix[0][j] == 0:
212+
setColZeros(matrix, j)
213+
214+
for i in range(R):
215+
if matrix[i][0] == 0:
216+
setRowZeros(matrix, i)
217+
218+
if isCol:
219+
setColZeros(matrix, 0)
220+
221+
```
222+
223+
另一种方法是用一个特殊符合标记需要改变的结果,只要这个特殊标记不在我们的题目数据范围(0和1)即可,这里用None。
182224
```python
183225
class Solution:
184226
def setZeroes(self, matrix: List[List[int]]) -> None:

0 commit comments

Comments
 (0)