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 7d77050

Browse files
authored
完善java新方法的注释
1 parent 99eb035 commit 7d77050

File tree

1 file changed

+73
-78
lines changed

1 file changed

+73
-78
lines changed

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

Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -261,84 +261,6 @@ 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-
342264
```java
343265
class Solution {
344266
private LinkedList<String> res;
@@ -423,6 +345,79 @@ class Solution {
423345
}
424346
```
425347

348+
```java
349+
/* 该方法是对第二个方法的改进,主要变化在于将某点的所有终点变更为链表的形式,优点在于
350+
1.添加终点时直接在对应位置添加节点,避免了TreeMap增元素时的频繁调整
351+
2.同时每次对终点进行增加删除查找时直接通过下标操作,避免hashMap反复计算hash*/
352+
class Solution {
353+
//key为起点,value是有序的终点的列表
354+
Map<String, LinkedList<String>> ticketMap = new HashMap<>();
355+
LinkedList<String> result = new LinkedList<>();
356+
int total;
357+
358+
public List<String> findItinerary(List<List<String>> tickets) {
359+
total = tickets.size() + 1;
360+
//遍历tickets,存入ticketMap中
361+
for (List<String> ticket : tickets) {
362+
addNew(ticket.get(0), ticket.get(1));
363+
}
364+
deal("JFK");
365+
return result;
366+
}
367+
368+
boolean deal(String currentLocation) {
369+
result.add(currentLocation);
370+
//机票全部用完,找到最小字符路径
371+
if (result.size() == total) {
372+
return true;
373+
}
374+
//当前位置的终点列表
375+
LinkedList<String> targetLocations = ticketMap.get(currentLocation);
376+
//没有从当前位置出发的机票了,说明这条路走不通
377+
if (targetLocations != null && !targetLocations.isEmpty()) {
378+
//终点列表中遍历到的终点
379+
String targetLocation;
380+
//遍历从当前位置出发的机票
381+
for (int i = 0; i < targetLocations.size(); i++) {
382+
targetLocation = targetLocations.get(i);
383+
//删除终点列表中当前的终点
384+
targetLocations.remove(i);
385+
//递归
386+
if (deal(targetLocation)) {
387+
return true;
388+
}
389+
//路线走不通,将机票重新加回去
390+
targetLocations.add(i, targetLocation);
391+
result.removeLast();
392+
}
393+
}
394+
return false;
395+
}
396+
397+
/**
398+
* 在map中按照字典顺序添加新元素
399+
*
400+
* @param start 起点
401+
* @param end 终点
402+
*/
403+
void addNew(String start, String end) {
404+
LinkedList<String> startAllEnd = ticketMap.getOrDefault(start, new LinkedList<>());
405+
if (!startAllEnd.isEmpty()) {
406+
for (int i = 0; i < startAllEnd.size(); i++) {
407+
if (end.compareTo(startAllEnd.get(i)) < 0) {
408+
startAllEnd.add(i, end);
409+
return;
410+
}
411+
}
412+
startAllEnd.add(startAllEnd.size(), end);
413+
} else {
414+
startAllEnd.add(end);
415+
ticketMap.put(start, startAllEnd);
416+
}
417+
}
418+
}
419+
```
420+
426421
### Python
427422
回溯 使用used数组
428423

0 commit comments

Comments
(0)

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