LeetCode刷题实战332:重新安排行程
示例
解题
class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
HashMap<String, PriorityQueue<String>> map = new HashMap();
for(List<String> ticket: tickets) {
if(map.containsKey(ticket.get(0))) {
map.get(ticket.get(0)).add(ticket.get(1));
} else {
PriorityQueue<String> temp = new PriorityQueue<String>();
temp.add(ticket.get(1));
map.put(ticket.get(0), temp);
}
}
List<String> result = new ArrayList();
build("JFK", map, result);
Collections.reverse(result);
return result;
}
void build(String from, HashMap<String, PriorityQueue<String>> map, List<String> res) {
PriorityQueue<String> cur = map.get(from);
while(cur!= null && !cur.isEmpty()) {
String nfrom = cur.poll();
build(nfrom, map, res);
}
res.add(from);
}
}
LeetCode刷题实战325:和等于 k 的最长子数组长度