题目来源:
leetcode题目,网址:1436. 旅行终点站 - 力扣(LeetCode)
解题思路:
使用哈希表存储所有起点,不在哈希表中的终点即为所求。
解题代码:
class Solution {
public String destCity(List<List<String>> paths) {
String res="";
Set<String> set=new HashSet<>();
for(List<String> list:paths){
set.add(list.get(0));
}
for(List<String> list:paths){
if(!set.contains(list.get(1))){
res=list.get(1);
break;
}
}
return res;
}
}
总结:
官方题解也是一样的思路。