Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6df2438

Browse files
author
luzhipeng
committed
2 parents f1c88c5 + 03ebc74 commit 6df2438

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

‎problems/104.maximum-depth-of-binary-tree.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var maxDepth = function(root) {
4949
- 树的基本操作- 遍历 - 层次遍历(BFS)
5050

5151
## 代码
52-
* 语言支持:JS,C++
52+
* 语言支持:JS,C++,Python
5353

5454
JavaScript Code:
5555
```js
@@ -130,6 +130,23 @@ public:
130130
}
131131
};
132132
```
133+
134+
Python Code:
135+
```python
136+
class Solution:
137+
def maxDepth(self, root: TreeNode) -> int:
138+
if not root: return 0
139+
q, depth = [root, None], 1
140+
while q:
141+
node = q.pop(0)
142+
if node:
143+
if node.left: q.append(node.left)
144+
if node.right: q.append(node.right)
145+
elif q:
146+
q.append(None)
147+
depth += 1
148+
return depth
149+
```
133150
## 相关题目
134151
- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md)
135152
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)

‎problems/121.best-time-to-buy-and-sell-stock.md‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
4040

4141
## 代码
4242

43-
语言支持:JS,Python,C++
43+
语言支持:JS,Python,C++
4444

4545
JS Code:
4646

@@ -60,31 +60,31 @@ JS Code:
6060
*
6161
* Say you have an array for which the i^th element is the price of a given
6262
* stock on day i.
63-
*
63+
*
6464
* If you were only permitted to complete at most one transaction (i.e., buy
6565
* one and sell one share of the stock), design an algorithm to find the
6666
* maximum profit.
67-
*
67+
*
6868
* Note that you cannot sell a stock before you buy one.
69-
*
69+
*
7070
* Example 1:
71-
*
72-
*
71+
*
72+
*
7373
* Input: [7,1,5,3,6,4]
7474
* Output: 5
7575
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit
7676
* = 6-1 = 5.
7777
* Not 7-1 = 6, as selling price needs to be larger than buying price.
78-
*
79-
*
78+
*
79+
*
8080
* Example 2:
81-
*
82-
*
81+
*
82+
*
8383
* Input: [7,6,4,3,1]
8484
* Output: 0
8585
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
86-
*
87-
*
86+
*
87+
*
8888
*/
8989
/**
9090
* @param {number[]} prices
@@ -111,28 +111,19 @@ var maxProfit = function(prices) {
111111
Python Code:
112112

113113
```python
114-
# 应用Kadane's algorithms
115114
class Solution:
116115
def maxProfit(self, prices: 'List[int]') -> int:
117-
"""
118-
step by step
119-
"""
120-
# error case
121-
if len(prices) < 1:
122-
return 0
123-
124-
# caluate the daily gains, break into a subarray problem
125-
gains = [prices[i]-prices[i-1] for i in range(1, len(prices))]
126-
127-
loc_max = global_max = 0 #not gains[0] in case of negative
128-
for i in range(len(gains)):
129-
loc_max = max(loc_max + gains[i], gains[i])
130-
if loc_max > global_max:
131-
global_max = loc_max
132-
"""
133-
Runtime: 48 ms, faster than 34.50% of Python3 online submissions for Best Time to Buy and Sell Stock.
134-
Memory Usage: 14.1 MB, less than 10.26% of Python3 online submissions for Best Time to Buy and Sell Stock.
135-
"""
116+
if not prices: return 0
117+
118+
min_price = float('inf')
119+
max_profit = 0
120+
121+
for price in prices:
122+
if price < min_price:
123+
min_price = price
124+
elif max_profit < price - min_price:
125+
max_profit = price - min_price
126+
return max_profit
136127
```
137128

138129
C++ Code:
@@ -164,4 +155,3 @@ public:
164155
165156
- [122.best-time-to-buy-and-sell-stock-ii](./122.best-time-to-buy-and-sell-stock-ii.md)
166157
- [309.best-time-to-buy-and-sell-stock-with-cooldown](./309.best-time-to-buy-and-sell-stock-with-cooldown.md)
167-

‎problems/88.merge-sorted-array.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ function merge(nums1, nums2) {
147147
* @return {void} Do not return anything, modify nums1 in-place instead.
148148
*/
149149
var merge = function(nums1, m, nums2, n) {
150-
// 设置一个指针,指针初始化指向nums1的末尾
150+
// 设置一个指针,指针初始化指向nums1的末尾(根据#62,应该是index为 m+n-1 的位置,因为nums1的长度有可能更长)
151151
// 然后不断左移指针更新元素
152-
let current = nums1.length - 1;
152+
let current = m + n - 1;
153153

154154
while (current >= 0) {
155155
// 没必要继续了
@@ -181,7 +181,7 @@ C++ code:
181181
class Solution {
182182
public:
183183
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
184-
int current = nums1.size()- 1;
184+
int current = m + n - 1;
185185
while (current >= 0) {
186186
if (n == 0) return;
187187
if (m < 1) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /