-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1436.旅行终点站.cpp
38 lines (37 loc) · 1.02 KB
/
1436.旅行终点站.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* @lc app=leetcode.cn id=1436 lang=cpp
*
* [1436] 旅行终点站
*/
// @lc code=start
class Solution {
public:
// 64 81;
string destCity(vector<vector<string>>& paths) {
// 注意起点也只有一个
set<string> st;
int size = paths.size();
// 处理到达站
for(int i = 0; i < size; ++i){
auto it = st.find(paths[i][1]); // 收集目的地,有可能是终点
if(it == st.end()){
st.insert(paths[i][1]);
}
}
// 处理出发站
for(int i = 0; i < size; ++i){
auto it = st.find(paths[i][0]); // 目的地成为了出发地,不可能是终点
if(it != st.end()){
st.erase(it);
}
}
return *(st.begin());
}
};
// @lc code=end
/*
[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
[["B","C"],["D","B"],["C","A"]]
[["A","Z"]]
[["pYyNGfBYbm","wxAscRuzOl"],["kzwEQHfwce","pYyNGfBYbm"]]
*/