题目
解题思路
根据终点站的定义,终点站不会出现在 cityAi中,因为存在从 cityAi出发的线路,所以终点站只会出现在cityBi中。据此,我们可以遍历cityBi,返回不在cityAi中的城市,即为答案。
代码
class Solution {
public String destCity(List<List<String>> paths) {
Set<String> citiesA = new HashSet<String>();
for (List<String> path : paths) {
citiesA.add(path.get(0));
}
for (List<String> path : paths) {
if (!citiesA.contains(path.get(1))) {
return path.get(1);
}
}
return "";
}
}