leetcode:到達目的城市
阿新 • • 發佈:2020-12-18
You are given the array paths,where paths[i]=[cityAi,cityBi] means there exists a direct path going from cityAi to cityBi.Return the destination city,that is, the city without any outgoing to another city. It is guaranteed that the graph of paths forms a line without any loop, therefore,there will be exactly one destination city. Example 1: Input:paths=[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]] Output:"Sao Paulo" Explanation:Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo". Example 2: Input: paths=[["B","C"],["D","B"],["C","A"]] Output:"A" Explanation:All possible trips are: "D" -> "B" -> "C" -> "A". "B" -> "C" -> "A". "C" -> "A". "A". Clearly the destination city is "A". 解題思路: 這道題剛開始沒讀懂題意,深思後,才意識到只需要找出最終點,因此先用陣列儲存第二點, 然後遍歷paths的是否存在該第一點.用map可能更快
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; class Solution{ public: string destcity(vector<vector<string>>& paths){ #if 0 vector<string> board; for(auto i=0;i<paths.size();i++) board.push_back(paths[i][1]); for(auto i=0;i<paths.size();i++){ auto it=find(board.begin(),board.end(),paths[i][0]); if(it!=board.end()) board.erase(it); } return board[0]; #endif map<string,string> ans; for(auto& i:paths) ans.insert(make_pair(i[0],i[1])); string key=paths[0][0]; while(ans.find(key)!=ans.end()){ key=ans[key]; } return key; } }; int main(int argc,char* argv[]){ vector<vector<string>> paths={{"B","C"},{"D","B"},{"C","A"}}; cout<<Solution().destcity(paths)<<endl; return 0; }