Skip to content

Instantly share code, notes, and snippets.

@Irene-123
Created July 20, 2020 15:13
Show Gist options
  • Save Irene-123/df0a78ccf833104e8c7beb7786b0a10d to your computer and use it in GitHub Desktop.
Save Irene-123/df0a78ccf833104e8c7beb7786b0a10d to your computer and use it in GitHub Desktop.
Destination City-LeetCode
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