mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'master' of github.com:jinbudaily/leetcode-master
This commit is contained in:
@ -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 []
|
||||
```
|
||||
(版本二)使用集合
|
||||
|
@ -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<>();
|
||||
|
@ -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) {
|
||||
@ -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;
|
||||
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 {
|
||||
TreeNode cur = root.right;
|
||||
break;
|
||||
}
|
||||
}
|
||||
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 = 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;
|
||||
cur.left = node.left;
|
||||
return node.right;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Python
|
||||
|
||||
递归法(版本一)
|
||||
```python
|
||||
class Solution:
|
||||
|
@ -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">
|
||||
|
@ -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">
|
||||
|
Reference in New Issue
Block a user