题解 | Sharing

Sharing

https://www.nowcoder.com/practice/2577bea713cf4eed874afccff1928113

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
struct node {
    int pos;
    char data;
    int next;
    node(int pos, char data, int next) :pos(pos), data(data), next(next) {
    }
    node(){}
};


int main()
{
    int start1, start2, n;
    while (cin >> start1 >> start2 >> n) {
        unordered_map<int, node>edge;
        for (int i = 0; i < n; i++) {
            int pos, next;
            char data;
            cin >> pos >> data >> next;
            edge.insert({ pos,node(pos,data,next) });
        }
        int len1 = 0, len2 = 0;
        //先遍历链表一
        int ptr1 = start1;
        while (ptr1 != -1) {
            ptr1 = edge[ptr1].next;
            len1++;
        }
        int ptr2 = start2;
        while (ptr2 != -1) {
            ptr2 = edge[ptr2].next;
            len2++;
        }
        ptr1 = start1, ptr2 = start2;
        //然后长度那个先走几步
        while (len1 > len2)ptr1 = edge[ptr1].next, len1--;
        while (len1 < len2)ptr2 = edge[ptr2].next, len2--;
        bool isame = false;
        while (ptr1 != -1 && ptr2 != -1) {
            if (edge[ptr1].pos == edge[ptr2].pos) {
                cout << edge[ptr1].pos << endl;
                isame = true;
                break;
            }
            ptr1 = edge[ptr1].next, ptr2 = edge[ptr2].next;
        }
        if (isame == false)cout << -1 << endl;
    }
}

全部评论

相关推荐

门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务