Skip to content

Commit 26afd7e

Browse files
committed
Merge branch 'master' of github.com:jinbudaily/leetcode-master
2 parents f4a74a4 + 2e5c9d2 commit 26afd7e

File tree

5 files changed

+143
-20
lines changed

5 files changed

+143
-20
lines changed

problems/0001.两数之和.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Solution:
163163
for index, value in enumerate(nums):
164164
if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key
165165
return [records[target- value], index]
166-
records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key
166+
records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中
167167
return []
168168
```
169169
(版本二)使用集合

problems/0077.组合.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,32 @@ public:
343343
## 其他语言版本
344344

345345

346-
### Java
347346

347+
### Java:
348+
未剪枝优化
349+
```java
350+
class Solution {
351+
List<List<Integer>> result= new ArrayList<>();
352+
LinkedList<Integer> path = new LinkedList<>();
353+
public List<List<Integer>> combine(int n, int k) {
354+
backtracking(n,k,1);
355+
return result;
356+
}
357+
358+
public void backtracking(int n,int k,int startIndex){
359+
if (path.size() == k){
360+
result.add(new ArrayList<>(path));
361+
return;
362+
}
363+
for (int i =startIndex;i<=n;i++){
364+
path.add(i);
365+
backtracking(n,k,i+1);
366+
path.removeLast();
367+
}
368+
}
369+
}
370+
```
371+
剪枝优化:
348372
```java
349373
class Solution {
350374
List<List<Integer>> result = new ArrayList<>();

problems/0450.删除二叉搜索树中的节点.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,34 @@ public:
268268
269269
270270
### Java
271+
```java
272+
// 解法1(最好理解的版本)
273+
class Solution {
274+
public TreeNode deleteNode(TreeNode root, int key) {
275+
if (root == null) return root;
276+
if (root.val == key) {
277+
if (root.left == null) {
278+
return root.right;
279+
} else if (root.right == null) {
280+
return root.left;
281+
} else {
282+
TreeNode cur = root.right;
283+
while (cur.left != null) {
284+
cur = cur.left;
285+
}
286+
cur.left = root.left;
287+
root = root.right;
288+
return root;
289+
}
290+
}
291+
if (root.val > key) root.left = deleteNode(root.left, key);
292+
if (root.val < key) root.right = deleteNode(root.right, key);
293+
return root;
294+
}
295+
}
296+
```
297+
298+
271299
```java
272300
class Solution {
273301
public TreeNode deleteNode(TreeNode root, int key) {
@@ -296,34 +324,59 @@ class Solution {
296324
}
297325
}
298326
```
327+
递归法
299328
```java
300-
// 解法2
301329
class Solution {
302330
public TreeNode deleteNode(TreeNode root, int key) {
303-
if (root == null) return root;
304-
if (root.val == key) {
305-
if (root.left == null) {
306-
return root.right;
307-
} else if (root.right == null) {
308-
return root.left;
309-
} else {
310-
TreeNode cur = root.right;
311-
while (cur.left != null) {
312-
cur = cur.left;
313-
}
314-
cur.left = root.left;
315-
root = root.right;
316-
return root;
331+
if (root == null){
332+
return null;
333+
}
334+
//寻找对应的对应的前面的节点,以及他的前一个节点
335+
TreeNode cur = root;
336+
TreeNode pre = null;
337+
while (cur != null){
338+
if (cur.val < key){
339+
pre = cur;
340+
cur = cur.right;
341+
} else if (cur.val > key) {
342+
pre = cur;
343+
cur = cur.left;
344+
}else {
345+
break;
317346
}
318347
}
319-
if (root.val > key) root.left = deleteNode(root.left, key);
320-
if (root.val < key) root.right = deleteNode(root.right, key);
348+
if (pre == null){
349+
return deleteOneNode(cur);
350+
}
351+
if (pre.left !=null && pre.left.val == key){
352+
pre.left = deleteOneNode(cur);
353+
}
354+
if (pre.right !=null && pre.right.val == key){
355+
pre.right = deleteOneNode(cur);
356+
}
321357
return root;
322358
}
359+
360+
public TreeNode deleteOneNode(TreeNode node){
361+
if (node == null){
362+
return null;
363+
}
364+
if (node.right == null){
365+
return node.left;
366+
}
367+
TreeNode cur = node.right;
368+
while (cur.left !=null){
369+
cur = cur.left;
370+
}
371+
cur.left = node.left;
372+
return node.right;
373+
}
323374
}
324375
```
325376

377+
326378
### Python
379+
327380
递归法(版本一)
328381
```python
329382
class Solution:

problems/0496.下一个更大元素I.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,32 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
387387
};
388388
```
389389

390+
Rust
391+
392+
```rust
393+
impl Solution {
394+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
395+
let mut ans = vec![-1; nums1.len()];
396+
use std::collections::HashMap;
397+
let mut map = HashMap::new();
398+
for (idx, &i) in nums1.iter().enumerate() {
399+
map.insert(i, idx);
400+
}
401+
let mut stack = vec![];
402+
for (idx, &i) in nums2.iter().enumerate() {
403+
while !stack.is_empty() && nums2[*stack.last().unwrap()] < i {
404+
let pos = stack.pop().unwrap();
405+
if let Some(&jdx) = map.get(&nums2[pos]) {
406+
ans[jdx] = i;
407+
}
408+
}
409+
stack.push(idx);
410+
}
411+
ans
412+
}
413+
}
414+
```
415+
390416

391417

392418
<p align="center">

problems/0739.每日温度.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,27 @@ function dailyTemperatures(temperatures: number[]): number[] {
455455
};
456456
```
457457

458-
458+
Rust:
459+
460+
```rust
461+
impl Solution {
462+
/// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标
463+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
464+
let mut res = vec![0; temperatures.len()];
465+
let mut stack = vec![];
466+
for (idx, &value) in temperatures.iter().enumerate() {
467+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value {
468+
// 弹出,并计算res中对应位置的值
469+
let i = stack.pop().unwrap();
470+
res[i] = (idx - i) as i32;
471+
}
472+
// 入栈
473+
stack.push(idx)
474+
}
475+
res
476+
}
477+
}
478+
```
459479

460480

461481
<p align="center">

0 commit comments

Comments
 (0)