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

Commit 77f448e

Browse files
authored
0332.重新安排行程,添加java优化方法
1 parent e916a0e commit 77f448e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

‎problems/0332.重新安排行程.md‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,84 @@ for (pair<const string, int>& target : targets[result[result.size() - 1]])
261261

262262
### Java
263263

264+
```java
265+
/* 首先遍历所有机票,将机票转换成map,其中起点作为key,终点按照字典顺序插入到终点的列表中
266+
然后进入递归,递归中首先在result中加入当前位置,如果票用完了就说明这条路走通了
267+
递归中遍历从当前位置出发的终点(也就是map中key为当前位置的列表),因为列表是有序的,因此只要出现能走通的路径,这条路径就是字典排序最低的
268+
在遍历中,首先从map中移除当前的机票,将遍历到的终点作为起点进入下一层的递归中,如果发现当前机票往后走走不通,再把当前的机票加到map中*/
269+
class Solution {
270+
//key为起点,value是有序的终点的列表
271+
Map<String, LinkedList<String>> ticketMap = new HashMap<>();
272+
LinkedList<String> result = new LinkedList<>();
273+
274+
public List<String> findItinerary(List<List<String>> tickets) {
275+
//遍历tickets,存入ticketMap中
276+
for (List<String> ticket : tickets) {
277+
addNew(ticket.get(0), ticket.get(1));
278+
}
279+
deal("JFK");
280+
return result;
281+
}
282+
283+
boolean deal(String currentLocation) {
284+
result.add(currentLocation);
285+
//机票全部用完,找到最小字符路径
286+
if (ticketMap.isEmpty()) {
287+
return true;
288+
}
289+
//当前位置的终点列表
290+
LinkedList<String> targetLocations = ticketMap.get(currentLocation);
291+
//机票没用完,但是没有从当前位置出发的机票了,说明这条路走不通
292+
if (targetLocations == null) {
293+
return false;
294+
}
295+
//终点列表中遍历到的终点
296+
String targetLocation;
297+
//遍历从当前位置出发的机票
298+
for (int i = 0; i < targetLocations.size(); i++) {
299+
targetLocation = targetLocations.get(i);
300+
//删除map中的机票,如果当前位置只有一个终点,直接删除k,v,有多个终点则删除终点列表中当前的终点
301+
if (targetLocations.size() == 1) {
302+
ticketMap.remove(currentLocation);
303+
} else {
304+
targetLocations.remove(i);
305+
}
306+
//递归
307+
if (deal(targetLocation)) {
308+
return true;
309+
} else {
310+
//路线走不通,将机票重新加到map中
311+
addNew(currentLocation, targetLocation);
312+
result.removeLast();
313+
}
314+
}
315+
return false;
316+
}
317+
318+
/**
319+
* 在map中添加新元素
320+
*
321+
* @param start 起点
322+
* @param end 终点
323+
*/
324+
void addNew(String start, String end) {
325+
LinkedList<String> startAllEnd = ticketMap.get(start);
326+
if (startAllEnd != null) {
327+
for (int i = 0; i < startAllEnd.size() + 1; i++) {
328+
if (i == startAllEnd.size() || end.compareTo(startAllEnd.get(i)) < 0) {
329+
startAllEnd.add(i, end);
330+
break;
331+
}
332+
}
333+
} else {
334+
LinkedList<String> ends = new LinkedList<>();
335+
ends.add(end);
336+
ticketMap.put(start, ends);
337+
}
338+
}
339+
}
340+
```
341+
264342
```java
265343
class Solution {
266344
private LinkedList<String> res;

0 commit comments

Comments
(0)

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