leetcode:到达目的城市

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值