给你一份航线列表 tickets
,其中 tickets[i] = [fromi, toi]
表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。
所有这些机票都属于一个从 JFK
(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK
开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。
- 例如,行程
["JFK", "LGA"]
与["JFK", "LGB"]
相比就更小,排序更靠前。
假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。
示例 1:
输入:tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] 输出:["JFK","MUC","LHR","SFO","SJC"]
示例 2:
输入:tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] 输出:["JFK","ATL","JFK","SFO","ATL","SFO"] 解释:另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"] ,但是它字典排序更大更靠后。
提示:
1 <= tickets.length <= 300
tickets[i].length == 2
fromi.length == 3
toi.length == 3
fromi
和toi
由大写英文字母组成fromi != toi
题解:
回溯法:
type pair struct {
target string
visited bool
}
type pairs []*pair
func (p pairs) Len() int {
return len(p)
}
func (p pairs) Less(i, j int) bool {
return p[i].target < p[j].target
}
func (p pairs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func findItinerary(tickets [][]string) []string {
var result []string
// 初始化各项航程数据
targets := make(map[string]pairs)
for _, ticket := range tickets {
if targets[ticket[0]] == nil {
targets[ticket[0]] = make(pairs, 0)
}
targets[ticket[0]] = append(targets[ticket[0]], &pair{
target: ticket[1],
visited: false,
})
}
for k, _ := range targets {
sort.Sort(targets[k])
}
// 飞机出发机场
result = append(result, "JFK")
var backtracking func() bool
backtracking = func() bool {
if len(result) == len(tickets)+1 {
return true
}
// 取出起飞航班对应的目的地
lastFlight := result[len(result)-1]
for _, pair := range targets[lastFlight] {
if pair.visited {
continue
}
result = append(result, pair.target)
pair.visited = true
if backtracking() {
return true
}
// 回溯
result = result[:len(result)-1]
pair.visited = false
}
return false
}
backtracking()
return result
}
评论前必须登录!
注册