12
返回列表 发新帖
楼主: jrou
跳转到指定楼层
上一主题 下一主题
收起左侧

lyft店面+店面总结

🔗
amethlex 2018-10-23 15:13:37 | 只看该作者
全局:
[quote]jrou 发表于 2018-7-21 01:33


  1. public class CommonElement implements Iterable {
  2. [/quote]
  3. 不好意思,不是很熟悉Java,用python刷的题。请问下为啥要+2和+4?
  4. [code]    public Iterator<Integer> iterator() {
  5.         List<Integer> l1 = new ArrayList<>();
  6.         List<Integer> l2 = new ArrayList<>();
  7.         for (int i = 0; i <= 20; i+= 2) {
  8.             l1.add(i);
  9.         }
  10.         for (int i = 0; i <= 20; i+= 4) {
  11.             l2.add(i);
  12.         }
  13.          
  14.         return new CommentElementIterator(l1.iterator(), l2.iterator());
  15.     }
复制代码

另外,它不是给了两个sorted iterator作为input吗,为何你写了iterator()?
在这里,为啥只会在tmp1 = tmp2的情况下才return,其他时候都设它们为null?
  1. private Integer findNext()
复制代码
回复

使用道具 举报

🔗
amethlex 2018-10-23 15:17:23 | 只看该作者
全局:
jrou 发表于 2018-7-21 01:33
[mw_shl_code=java,true]

public class CommonElement implements Iterable {

你觉得这么写可以吗?
https://stackoverflow.com/questi ... sts-using-iterators
package com.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class IteratorMerge {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String> list1 = Arrays.asList(new String[]{"A", "B", "C", "D"});
        List<String> list2 = Arrays.asList(new String[]{"B", "D", "F", "G"});

        System.out.println(merge(list1, list2));
    }

    public static List<String> merge(List<String> L1,List<String> L2) {
        List<String> L3 = new ArrayList<String>();

        Iterator<String> it1 = L1.iterator();
        Iterator<String> it2 = L2.iterator();

        String s1 = it1.hasNext() ? it1.next() : null;
        String s2 = it2.hasNext() ? it2.next() : null;
        while (s1 != null && s2 != null) {
            if (s1.compareTo(s2) < 0) { // s1 comes before s2
                L3.add(s1);
                s1 = it1.hasNext() ? it1.next() : null;
            }
            else { // s1 and s2 are equal, or s2 comes before s1
                L3.add(s2);
                s2 = it2.hasNext() ? it2.next() : null;
            }
        }

        // There is still at least one element from one of the lists which has not been added
        if (s1 != null) {
            L3.add(s1);
            while (it1.hasNext()) {
                L3.add(it1.next());
            }
        }
        else if (s2 != null) {
            L3.add(s2);
            while (it2.hasNext()) {
                L3.add(it2.next());
            }
        }

        return L3;
    }
}

补充内容 (2018-10-23 15:21):
啊,好像不对,这就不是iterator了,但可以不可以吧merge分成hasNext 和 next?
hasNext就是s1或s2还有继续,next就是return较小的那个数?
回复

使用道具 举报

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

本版积分规则

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