楼主: williamchai
跳转到指定楼层
上一主题 下一主题
收起左侧

脸熟店面

🔗
Soomeone 2018-5-20 01:59:01 | 只看该作者
全局:
根据hzyfree的版本改了个
    public List<Interval> merge(Interval[] int1, Interval[] int2) {
        //check null
        int1 = (int1 == null) ? new Interval[]{} : int1;
        int2 = (int2 == null) ? new Interval[]{} : int2;
        List<Interval> res = new ArrayList<>();
        int p1 = 0, p2 = 0;
        Interval pre = new Interval(Integer.MIN_VALUE, Integer.MIN_VALUE);
        while(p1 < int1.length || p2 < int2.length) {
            int p1Start = p1 < int1.length ? int1[p1].start : Integer.MAX_VALUE;
            int p2Start = p2 < int2.length ? int2[p2].start : Integer.MAX_VALUE;
            //cur interval always points smaller one, and always move smaller one pointer.
            Interval cur = p1Start < p2Start ? int1[p1++] : int2[p2++];
            //merge intervals
            if(cur.start <= pre.end) {
                pre.end = Math.max(pre.end, cur.end);
            } else {
                res.add(pre);
                pre = cur;
            }
        }
        //don't forget the last one...
        res.add(pre);
        return res.subList(1, res.size());
    }
回复

使用道具 举报

🔗
hzyfree 2018-5-20 02:27:07 | 只看该作者
全局:
Soomeone 发表于 2018-5-20 01:38
即便A或者B中一个为空不能直接返回吧,比如A为空,那B里的intervals是不是也应该要Merge?

我默认了每个list里面的interval没有overlap,如果有的话可以删掉第一个if语句,然后第7行加上处理这种情况的语句应该就行
回复

使用道具 举报

🔗
hzyfree 2018-5-20 02:28:31 | 只看该作者
全局:
landy622 发表于 2018-5-20 01:08
怎么加微信?拉个群?

我的微信号就是我的论坛id
回复

使用道具 举报

全局:
这个应该就是 李特口的 无视刘 吧
回复

使用道具 举报

🔗
pengsy89 2018-5-22 11:02:52 | 只看该作者
全局:
给一个我写的扫面线做法
public List<Interval> merge(List<Interval> intervals) {
        List<Interval> res=new ArrayList<>();
        if(intervals.size()==0) return res;
        List<int[]> points=new ArrayList<>();
        for(Interval i: intervals){
            points.add(new int[]{i.start, 1});
            points.add(new int[]{i.end, -1});
        }
        
        Collections.sort(points, (i1, i2) -> i1[0]==i2[0] ? i2[1]-i1[1] : i1[0]-i2[0]);//值相同,这里需要开始的在前,和meeting room不一样
        int start=0;
        int count=0;
        int preCount=0;
        for(int[] i: points){
            preCount=count;
            if(i[1]==1) count++;
            else count--;
            if(preCount<1 && count==1){
                start=i[0];
            }else if(preCount>0 && count==0){
                res.add(new Interval(start, i[0]));
            }
        }
        return res;
}
回复

使用道具 举报

🔗
 楼主| williamchai 2018-5-25 12:27:03 | 只看该作者
全局:
misty1007 发表于 2018-5-18 04:04
是原题嘛 我还以为fb的店面都会变变形。。

稍微变了一点,输入是两个list
回复

使用道具 举报

🔗
 楼主| williamchai 2018-5-25 12:27:39 | 只看该作者
全局:
sw7eets 发表于 2018-5-18 06:14
请问有详细问简历嘛?

没有,电面就简单聊几句现在做什么,然后就做题了
回复

使用道具 举报

🔗
 楼主| williamchai 2018-5-25 12:28:33 | 只看该作者
全局:
fernando 发表于 2018-5-19 02:28
请教LZ 这题最优解或者你当时给的解法是什么? 我之前面经看的不多 ==
我能想到的就是 对第二个 扫一遍  ...

我就是用两个指针,跟9楼的方法一样
回复

使用道具 举报

🔗
 楼主| williamchai 2018-5-25 12:29:05 | 只看该作者
全局:
landy622 发表于 2018-5-19 12:28
请问下楼主用什么方法做的,双指针感觉还是有bug,可以第二个向第一个 insert吗

跟9楼一样的做法,应该没有问题
回复

使用道具 举报

🔗
 楼主| williamchai 2018-5-25 12:30:41 | 只看该作者
全局:
hzyfree 发表于 2018-5-20 02:27
我默认了每个list里面的interval没有overlap,如果有的话可以删掉第一个if语句,然后第7行加上处理这种情 ...

对的,当时是说输入的两个list分别没有overlap
回复

使用道具 举报

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

本版积分规则

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