📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

奥瑞口奇葩店面

🔗
匿名用户-L9TYX  2020-4-17 06:57:38 |倒序浏览

2020(1-3月) 码农类General 硕士 全职@oracle - 网上海投 - 技术电面  | | Fail | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
贡献一个欧瑞口的店面,领口上没找到类似的题。
一只狗有父母两个节点,狗孩子的节点存有狗父母两个ID。Q1,  如果输入是两只狗的ID,如果判断他们是不是有
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
明不是反过来的二叉树,又在上面标了很多重复的点,让自己脑补这些是一个点, 就变成一个复杂的有向图。

评分

参与人数 4大米 +11 收起 理由
MilkyKMKY + 1 给你点个赞!
bc2615 + 2 给你点个赞!
cee + 1 给你点个赞!
匿名用户-RFJ87 + 7

查看全部评分


上一篇:脸视频上门
下一篇:2sigma OA掛
🔗
cee 2020-5-18 09:04:51 | 只看该作者
全局:
除了 BFS/DFS 还能有什么方案?
回复

使用道具 举报

🔗
wx6807 2020-7-1 13:58:41 | 只看该作者
全局:
这个题应该是用双向BFS,同时遍历两个狗,用两个set存各自祖先,先分别找一代祖先看在对方set里面不,再找二代,。。。
这是我想到的复杂度最低的方法。类似word ladder,但是是个有向图
回复

使用道具 举报

全局:
这个题我写过,其实是一个变相的LCA,用直白的recursion可解,代码如下
  1. public class DogQuestion {
  2.     static class Dog {
  3.         public int id;
  4.         Dog parent1;
  5.         Dog parent2;
  6.         public Dog(int id) {
  7.             this.id = id;
  8.         }
  9.         public Dog() {}
  10.     }

  11.     static Dog findAncestor(Dog d1, Dog d2) {
  12.         if(d1==null || d2==null) return null;
  13.         if(d1.id==d2.id) return d1;

  14.         Dog a1 = findAncestor(d1.parent1, d2.parent1);
  15.         Dog a2 = findAncestor(d1.parent1, d2.parent2);
  16.         Dog a3 = findAncestor(d1.parent2, d2.parent1);
  17.         Dog a4 = findAncestor(d1.parent2, d2.parent2);

  18.         Dog a5 = findAncestor(d1, d2.parent1);
  19.         Dog a6 = findAncestor(d1, d2.parent2);
  20.         Dog a7 = findAncestor(d1.parent1, d2);
  21.         Dog a8 = findAncestor(d1.parent2, d2);

  22.         if(a1!=null) return a1;
  23.         if(a2!=null) return a2;
  24.         if(a3!=null) return a3;
  25.         if(a4!=null) return a4;
  26.         if(a5!=null) return a5;
  27.         if(a6!=null) return a6;
  28.         if(a7!=null) return a7;
  29.         if(a8!=null) return a8;

  30.         return null;
  31.     }

  32.     public static void main(String[] args) {
  33.         //parents
  34.         Dog p1 = new Dog(1);
  35.         Dog p2 = new Dog(2);
  36.         Dog p3 = new Dog(3);
  37.         Dog p4 = new Dog(4);
  38.         Dog p5 = new Dog(5);
  39.         Dog p6 = new Dog(6);
  40.         Dog p8 = new Dog(8);
  41.         //2 dogs
  42.         Dog d1 = new Dog(11);
  43.         Dog d2 = new Dog(12);

  44.         d1.parent1 = p1;
  45.         d1.parent2 = p2;
  46.         p1.parent1 = p3;
  47.         p1.parent2 = p4;

  48.         d2.parent1 = p2;
  49.         d2.parent2 = p8;

  50.         Dog ancestor = findAncestor(d1, d2);
  51.         System.out.println(ancestor.id);
  52.     }
  53. }
复制代码


评分

参与人数 1大米 +3 收起 理由
kang1415926 + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表