# 目的地城市Leetcode解决方案

`paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]`
`Sao Paulo`

`paths = [["A","Z"]]`
`Z`

## 目的地城市Leetcode解决方案的代码

### C ++代码

```#include <bits/stdc++.h>
using namespace std;

string destCity(vector<vector<string>>& paths) {
unordered_map<string, int> outdegree;
for(auto x: paths){
outdegree[x[0]]++;
}
for(auto x: paths)
if(outdegree[x[0]] == 0)
return x[0];
else if(outdegree[x[1]] == 0)
return x[1];
return paths[0][0];
}

int main(){
vector<vector<string>> paths = {{"London","New York"},{"New York","Lima"},{"Lima","Sao Paulo"}};
string output = destCity(paths);
cout<<output;
}
```
`Sao Paulo`

### Java代码

```import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
public static String destCity(List<List<String>> paths) {
HashMap<String, Integer> outdegree = new HashMap<String, Integer>();
for(List<String> x: paths)
if(outdegree.containsKey(x.get(0)))
outdegree.put(x.get(0), outdegree.get(x.get(1))+1);
else
outdegree.put(x.get(0), 1);

for(List<String> x: paths)
if(!outdegree.containsKey(x.get(0)))
return x.get(0);
else if(!outdegree.containsKey(x.get(1)))
return x.get(1);

return paths.get(0).get(0);
}

public static void main (String[] args) throws java.lang.Exception{
List<List<String>> paths = new ArrayList<List<String>>();
System.out.print(destCity(paths));
}
}```
`Sao Paulo`