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

[pull] master from youngyangyang04:master #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 13 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
4fe73f3
书写错误,null写成了nullptr
lifejwang11 Jul 5, 2023
751ba68
null写成了nullptr
lifejwang11 Jul 5, 2023
873257d
Update 0001.两数之和.md
tlylt Jul 7, 2023
dba8820
Update 0739.每日温度.md
Ainevsia Jul 8, 2023
965afc9
修改java版本解法的位置,适合图的解法放在第一个,第二个解法(难理解版本)放在其后,新增java版本的迭代法
lifejwang11 Jul 8, 2023
0fd7b04
撤回之前的nullptr
lifejwang11 Jul 8, 2023
1c7b15b
Update 0496.下一个更大元素I.md
Ainevsia Jul 8, 2023
a31b586
组合新增java版本未剪枝优化版本
lifejwang11 Jul 10, 2023
3745145
Merge branch 'master' into master
youngyangyang04 Jul 25, 2023
c848094
Merge pull request #2173 from lifejwang11/master
youngyangyang04 Jul 25, 2023
3a99bee
Merge pull request #2171 from tlylt/patch-3
youngyangyang04 Jul 25, 2023
d337b0c
Merge pull request #2172 from Ainevsia/master
youngyangyang04 Jul 25, 2023
2e5c9d2
Merge pull request #2174 from Ainevsia/patch-1
youngyangyang04 Jul 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion problems/0001.两数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Solution:
for index, value in enumerate(nums):
if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key
return [records[target- value], index]
records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key
records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中
return []
```
(版本二)使用集合
Expand Down
26 changes: 25 additions & 1 deletion problems/0077.组合.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,32 @@ public:
## 其他语言版本


### Java

### Java:
未剪枝优化
```java
class Solution {
List<List<Integer>> result= new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
backtracking(n,k,1);
return result;
}

public void backtracking(int n,int k,int startIndex){
if (path.size() == k){
result.add(new ArrayList<>(path));
return;
}
for (int i =startIndex;i<=n;i++){
path.add(i);
backtracking(n,k,i+1);
path.removeLast();
}
}
}
```
剪枝优化:
```java
class Solution {
List<List<Integer>> result = new ArrayList<>();
Expand Down
87 changes: 70 additions & 17 deletions problems/0450.删除二叉搜索树中的节点.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,34 @@ public:


### Java
```java
// 解法1(最好理解的版本)
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return root;
if (root.val == key) {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
} else {
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
root = root.right;
return root;
}
}
if (root.val > key) root.left = deleteNode(root.left, key);
if (root.val < key) root.right = deleteNode(root.right, key);
return root;
}
}
```


```java
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
Expand Down Expand Up @@ -296,34 +324,59 @@ class Solution {
}
}
```
递归法
```java
// 解法2
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return root;
if (root.val == key) {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
} else {
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
root = root.right;
return root;
if (root == null){
return null;
}
//寻找对应的对应的前面的节点,以及他的前一个节点
TreeNode cur = root;
TreeNode pre = null;
while (cur != null){
if (cur.val < key){
pre = cur;
cur = cur.right;
} else if (cur.val > key) {
pre = cur;
cur = cur.left;
}else {
break;
}
}
if (root.val > key) root.left = deleteNode(root.left, key);
if (root.val < key) root.right = deleteNode(root.right, key);
if (pre == null){
return deleteOneNode(cur);
}
if (pre.left !=null && pre.left.val == key){
pre.left = deleteOneNode(cur);
}
if (pre.right !=null && pre.right.val == key){
pre.right = deleteOneNode(cur);
}
return root;
}

public TreeNode deleteOneNode(TreeNode node){
if (node == null){
return null;
}
if (node.right == null){
return node.left;
}
TreeNode cur = node.right;
while (cur.left !=null){
cur = cur.left;
}
cur.left = node.left;
return node.right;
}
}
```


### Python

递归法(版本一)
```python
class Solution:
Expand Down
26 changes: 26 additions & 0 deletions problems/0496.下一个更大元素I.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,32 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
};
```

Rust

```rust
impl Solution {
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
let mut ans = vec![-1; nums1.len()];
use std::collections::HashMap;
let mut map = HashMap::new();
for (idx, &i) in nums1.iter().enumerate() {
map.insert(i, idx);
}
let mut stack = vec![];
for (idx, &i) in nums2.iter().enumerate() {
while !stack.is_empty() && nums2[*stack.last().unwrap()] < i {
let pos = stack.pop().unwrap();
if let Some(&jdx) = map.get(&nums2[pos]) {
ans[jdx] = i;
}
}
stack.push(idx);
}
ans
}
}
```



<p align="center">
Expand Down
22 changes: 21 additions & 1 deletion problems/0739.每日温度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,27 @@ function dailyTemperatures(temperatures: number[]): number[] {
};
```


Rust:

```rust
impl Solution {
/// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
let mut res = vec![0; temperatures.len()];
let mut stack = vec![];
for (idx, &value) in temperatures.iter().enumerate() {
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value {
// 弹出,并计算res中对应位置的值
let i = stack.pop().unwrap();
res[i] = (idx - i) as i32;
}
// 入栈
stack.push(idx)
}
res
}
}
```


<p align="center">
Expand Down

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