mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
完善java新方法的注释
This commit is contained in:
@ -261,84 +261,6 @@ for (pair<const string, int>& target : targets[result[result.size() - 1]])
|
|||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
```java
|
|
||||||
/* 首先遍历所有机票,将机票转换成map,其中起点作为key,终点按照字典顺序插入到终点的列表中
|
|
||||||
然后进入递归,递归中首先在result中加入当前位置,如果票用完了就说明这条路走通了
|
|
||||||
递归中遍历从当前位置出发的终点(也就是map中key为当前位置的列表),因为列表是有序的,因此只要出现能走通的路径,这条路径就是字典排序最低的
|
|
||||||
在遍历中,首先从map中移除当前的机票,将遍历到的终点作为起点进入下一层的递归中,如果发现当前机票往后走走不通,再把当前的机票加到map中*/
|
|
||||||
class Solution {
|
|
||||||
//key为起点,value是有序的终点的列表
|
|
||||||
Map<String, LinkedList<String>> ticketMap = new HashMap<>();
|
|
||||||
LinkedList<String> result = new LinkedList<>();
|
|
||||||
|
|
||||||
public List<String> findItinerary(List<List<String>> tickets) {
|
|
||||||
//遍历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 (ticketMap.isEmpty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//当前位置的终点列表
|
|
||||||
LinkedList<String> targetLocations = ticketMap.get(currentLocation);
|
|
||||||
//机票没用完,但是没有从当前位置出发的机票了,说明这条路走不通
|
|
||||||
if (targetLocations == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//终点列表中遍历到的终点
|
|
||||||
String targetLocation;
|
|
||||||
//遍历从当前位置出发的机票
|
|
||||||
for (int i = 0; i < targetLocations.size(); i++) {
|
|
||||||
targetLocation = targetLocations.get(i);
|
|
||||||
//删除map中的机票,如果当前位置只有一个终点,直接删除k,v,有多个终点则删除终点列表中当前的终点
|
|
||||||
if (targetLocations.size() == 1) {
|
|
||||||
ticketMap.remove(currentLocation);
|
|
||||||
} else {
|
|
||||||
targetLocations.remove(i);
|
|
||||||
}
|
|
||||||
//递归
|
|
||||||
if (deal(targetLocation)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
//路线走不通,将机票重新加到map中
|
|
||||||
addNew(currentLocation, targetLocation);
|
|
||||||
result.removeLast();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在map中添加新元素
|
|
||||||
*
|
|
||||||
* @param start 起点
|
|
||||||
* @param end 终点
|
|
||||||
*/
|
|
||||||
void addNew(String start, String end) {
|
|
||||||
LinkedList<String> startAllEnd = ticketMap.get(start);
|
|
||||||
if (startAllEnd != null) {
|
|
||||||
for (int i = 0; i < startAllEnd.size() + 1; i++) {
|
|
||||||
if (i == startAllEnd.size() || end.compareTo(startAllEnd.get(i)) < 0) {
|
|
||||||
startAllEnd.add(i, end);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LinkedList<String> ends = new LinkedList<>();
|
|
||||||
ends.add(end);
|
|
||||||
ticketMap.put(start, ends);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
private LinkedList<String> res;
|
private LinkedList<String> res;
|
||||||
@ -423,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
|
### Python
|
||||||
回溯 使用used数组
|
回溯 使用used数组
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user