Created
July 20, 2020 15:13
-
-
Save Irene-123/df0a78ccf833104e8c7beb7786b0a10d to your computer and use it in GitHub Desktop.
Destination City-LeetCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string destCity(vector<vector<string>>& paths) { | |
unordered_map<string,int> degreeMap; //create an unordered map | |
for(auto& e: paths){ //iterator e for paths' vector | |
degreeMap[e[0]] += 1; //for every city position increase the counter | |
degreeMap[e[1]] += 0; //dont increase the counter for destination city | |
} | |
for (auto& [k, v]: degreeMap) | |
if (v == 0) //if you find a city whose index is 0 you have got the destination city ! | |
return k; //return it | |
return ""; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment