题目

类型:HashTable
难度:简单
image.png

解题思路

根据终点站的定义,终点站不会出现在 cityAi中,因为存在从 cityAi出发的线路,所以终点站只会出现在cityBi中。据此,我们可以遍历cityBi,返回不在cityAi中的城市,即为答案。

代码中用到了Set存放cityAi

代码

  1. class Solution {
  2. public String destCity(List<List<String>> paths) {
  3. Set<String> citiesA = new HashSet<String>();
  4. for (List<String> path : paths) {
  5. citiesA.add(path.get(0));
  6. }
  7. for (List<String> path : paths) {
  8. if (!citiesA.contains(path.get(1))) {
  9. return path.get(1);
  10. }
  11. }
  12. return "";
  13. }
  14. }