Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Shixiaocaia
2023-09-03 22:02:11 +08:00
committed by GitHub
3 changed files with 115 additions and 12 deletions

View File

@ -345,6 +345,79 @@ class Solution {
}
```
```java
/* 该方法是对第二个方法的改进,主要变化在于将某点的所有终点变更为链表的形式,优点在于
1.添加终点时直接在对应位置添加节点避免了TreeMap增元素时的频繁调整
2.同时每次对终点进行增加删除查找时直接通过下标操作避免hashMap反复计算hash*/
class Solution {
//key为起点value是有序的终点的列表
Map<String, LinkedList<String>> ticketMap = new HashMap<>();
LinkedList<String> result = new LinkedList<>();
int total;
public List<String> findItinerary(List<List<String>> tickets) {
total = tickets.size() + 1;
//遍历tickets存入ticketMap中
for (List<String> ticket : tickets) {
addNew(ticket.get(0), ticket.get(1));
}
deal("JFK");
return result;
}
boolean deal(String currentLocation) {
result.add(currentLocation);
//机票全部用完,找到最小字符路径
if (result.size() == total) {
return true;
}
//当前位置的终点列表
LinkedList<String> targetLocations = ticketMap.get(currentLocation);
//没有从当前位置出发的机票了,说明这条路走不通
if (targetLocations != null && !targetLocations.isEmpty()) {
//终点列表中遍历到的终点
String targetLocation;
//遍历从当前位置出发的机票
for (int i = 0; i < targetLocations.size(); i++) {
targetLocation = targetLocations.get(i);
//删除终点列表中当前的终点
targetLocations.remove(i);
//递归
if (deal(targetLocation)) {
return true;
}
//路线走不通,将机票重新加回去
targetLocations.add(i, targetLocation);
result.removeLast();
}
}
return false;
}
/**
* 在map中按照字典顺序添加新元素
*
* @param start 起点
* @param end 终点
*/
void addNew(String start, String end) {
LinkedList<String> startAllEnd = ticketMap.getOrDefault(start, new LinkedList<>());
if (!startAllEnd.isEmpty()) {
for (int i = 0; i < startAllEnd.size(); i++) {
if (end.compareTo(startAllEnd.get(i)) < 0) {
startAllEnd.add(i, end);
return;
}
}
startAllEnd.add(startAllEnd.size(), end);
} else {
startAllEnd.add(end);
ticketMap.put(start, startAllEnd);
}
}
}
```
### Python
回溯 使用used数组

View File

@ -396,25 +396,33 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
### Rust
```rust
use std::collections::HashMap;
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 res, mut map) = (vec![-1; nums1.len()], HashMap::new());
if nums1.is_empty() {
return res;
}
nums1.into_iter().enumerate().for_each(|(v, k)| {
map.insert(k, v);
});
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;
for (i, &value) in nums2.iter().enumerate() {
while let Some(&top) = stack.last() {
if value <= nums2[top] {
break;
}
let stacked_index = stack.pop().unwrap();
if let Some(&mapped_index) = map.get(&nums2[stacked_index]) {
res[mapped_index] = value;
}
}
stack.push(idx);
stack.push(i);
}
ans
res
}
}
```

View File

@ -293,6 +293,28 @@ impl Solution {
}
```
> 版本二:
```rust
impl Solution {
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
let (mut stack, mut res) = (vec![], vec![-1; nums.len()]);
for i in 0..nums.len() * 2 {
while let Some(&top) = stack.last() {
if nums[i % nums.len()] <= nums[top] {
break;
}
let saved_index = stack.pop().unwrap();
res[saved_index] = nums[i % nums.len()];
}
stack.push(i % nums.len());
}
res
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">