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

LinkedIn 电面

全局:

2015(10-12月) 码农类General 博士 全职@linkedin - 内推 - 技术电面  | | Other | 应届毕业生

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

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

x
Intersection of two sorted array

Question:Get the intersection of two sorted array. Input interface:
Iterable intersection(Iterator a, Iterator b);



因为输入是Iterator, 我就用两个变量来存值。。不知道还有没有更好的方法?


Code (Java):
[backcolor=white !important][size=1em]
[color=white !important][size=1em]

您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
lse {
[size=1em]                    currB = null;
[size=1em]                }
[size=1em]            }
[size=1em]        }
[size=1em]         
[size=1em]        return result;
[size=1em]    }
[size=1em]     
[size=1em]    public static void main(String[] args) {
[size=1em]        List<Integer> a = new ArrayList<>();
[size=1em]        a.add(1);
[size=1em]        a.add(3);
[size=1em]        a.add(5);
[size=1em]         
[size=1em]        List<Integer> b = new ArrayList<>();
[size=1em]        b.add(1);
[size=1em]        b.add(2);
[size=1em]        b.add(3);
[size=1em]        b.add(5);
[size=1em]        b.add(6);
[size=1em]         
[size=1em]        Iterable<Integer> result = intersection(a.iterator(), b.iterator());
[size=1em]         
[size=1em]        for (Integer num : result) {
[size=1em]            System.out.println(num);
[size=1em]        }
[size=1em]    }
[size=1em]}





评分

参与人数 1大米 +30 收起 理由
whdawn + 30

查看全部评分


上一篇:Amazon offer 奇遇以及我对video的想法
下一篇:amazon phone
推荐
 楼主| butterwang 2015-11-19 01:57:55 | 只看该作者
全局:
怎么帖代贴成这样子了。。。
重新来一遍。。
  1. import java.io.*;
  2. import java.util.*;

  3. public class Solution {
  4.     public static Iterable<Integer> intersection(Iterator<Integer> a, Iterator<Integer> b) {
  5.         List<Integer> result = new ArrayList<>();
  6.         
  7.         if (!a.hasNext() || !b.hasNext()) {
  8.             return result;
  9.         }
  10.         
  11.         Integer currA = a.next();
  12.         Integer currB = b.next();
  13.         
  14.         while (currA != null && currB != null) {
  15.             if (currA.equals(currB)) {
  16.                 result.add(currA);
  17.                
  18.                 if (a.hasNext()) {
  19.                     currA = a.next();
  20.                 } else {
  21.                     currA = null;
  22.                 }
  23.                
  24.                 if (b.hasNext()) {
  25.                     currB = b.next();
  26.                 } else {
  27.                     currB = null;
  28.                 }
  29.             } else if (currA < currB) {
  30.                 if (a.hasNext()) {
  31.                     currA = a.next();
  32.                 } else {
  33.                     currA = null;
  34.                 }
  35.             } else {
  36.                 if (b.hasNext()) {
  37.                     currB = b.next();
  38.                 } else {
  39.                     currB = null;
  40.                 }
  41.             }
  42.         }
  43.         
  44.         return result;
  45.     }
  46.    
  47.     public static void main(String[] args) {
  48.         List<Integer> a = new ArrayList<>();
  49.         a.add(1);
  50.         a.add(3);
  51.         a.add(5);
  52.         
  53.         List<Integer> b = new ArrayList<>();
  54.         b.add(1);
  55.         b.add(2);
  56.         b.add(3);
  57.         b.add(5);
  58.         b.add(6);
  59.         
  60.         Iterable<Integer> result = intersection(a.iterator(), b.iterator());
  61.         
  62.         for (Integer num : result) {
  63.             System.out.println(num);
  64.         }
  65.     }
  66. }
复制代码
回复

使用道具 举报

🔗
Iancss 2015-11-23 12:12:54 | 只看该作者
全局:
你这个题目啥意思?看不是很懂你题目意思
回复

使用道具 举报

🔗
Iancss 2015-11-23 12:13:32 | 只看该作者
全局:
就是让你求两个数组的交集?
回复

使用道具 举报

🔗
glad2mu 2016-11-16 08:07:13 | 只看该作者
全局:
感觉这题的意思是让你先用a的iterator建成一个hashset(如果没有重复),再用b的iterator remove 不存在于b中的a中的元素,剩下的就是intersection了。
回复

使用道具 举报

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

本版积分规则

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