Lyft店面

题目是给你两个iterators, 实现一个iterator, 返回两个中的共同元素。哟下是我的代码,供参考。如果你有跟好的方法, 也请分享指教。祝大家都招工顺利, 拿到心仪的卧佛寺。节日快乐!

public class IntersectionIterator {
    private Iterator<Integer> it1, it2;
    private int nextElem;-baidu 1point3acres
    public IntersectionIterator(Iterator<Integer> it1, Iterator<Integer> it2) {
        this.it1 = it1;
        this.it2 = it2;
    }

    /**
     *
     * @return the next element in the iteration (coomon element in the two iterators).
     *
     * Caveat: this function must be guarded by hasNext().
     */
    public int next() {
        return nextElem;
    }

    /**
     * @return true if the iteration has more elements (common elements in the two interators).
     */
    public boolean hasNext() {
        if (!it1.hasNext() || !it2.hasNext()) {
            return false;
        }

        int elem1 = it1.next();
        int elem2 = it2.next();

        if (elem1 == elem2) {
            nextElem = elem1;
            return true;
        }

        while (it1.hasNext() || it2.hasNext()) {
            if (elem1 < elem2 && it1.hasNext()) {
                elem1 = it1.next();
            } else if (elem2 < elem1 && it2.hasNext()) {
                elem2 = it2.next();
            }

            if (!it1.hasNext() && elem1 < elem2) {
                return false;
            }

            if (!it2.hasNext() && elem2 < elem1) {
                return false;
            }

            if (elem1 == elem2) {
                nextElem = elem1;
                return true;
            }
        }

        return false;
    }

    public static class UnitTest {
-baidu 1point3acres
        // Helper for test
        private IntersectionIterator createInstanceFrom(int[] a, int[] b) {
            Iterator<Integer> it1 = Arrays.stream(a).boxed().collect(Collectors.toList()).iterator();-baidu 1point3acres
            Iterator<Integer> it2 = Arrays.stream(b).boxed().collect(Collectors.toList()).iterator();
            return new IntersectionIterator(it1, it2);
        }

        @Test
        public void test_partial_common_elements() {
            int[] a = {1, 2, 4, 5, 6};
            int[] b = {1, 3, 5};

            IntersectionIterator iter = createInstanceFrom(a, b);

            List<Integer> result = new ArrayList<>();
            while (iter.hasNext()) {
                result.add(iter.next());
            }

            int[] expected = {1, 5};
            int[] actual = result.stream().mapToInt(x->x).toArray();

            assertArrayEquals(expected, actual);
        }

        @Test
        public void test_all_common_elements() {
            int[] a = {1, 2, 3};
            int[] b = {1, 2, 3};

            IntersectionIterator iter = createInstanceFrom(a, b);


            List<Integer> result = new ArrayList<>();
            while (iter.hasNext()) {
                result.add(iter.next());
            }

            int[] expected = {1, 2, 3};
            int[] actual = result.stream().mapToInt(x->x).toArray();

            assertArrayEquals(expected, actual);
        }

        @Test
        public void test_no_common_elements() {
            int[] a = {1, 2, 4, 5, 6};
            int[] b = {3, 7, 9, 10};

            IntersectionIterator iter = createInstanceFrom(a, b);

            List<Integer> result = new ArrayList<>();
            while (iter.hasNext()) {
                result.add(iter.next());
            }

            int[] expected = {};
            int[] actual = result.stream().mapToInt(x->x).toArray();. check 1point3acres for more.

            assertArrayEquals(expected, actual);
        }


        @Test
        public void test_both_empty() {
            int[] a = {};
            int[] b = {};
. From 1point 3acres bbs
            IntersectionIterator iter = createInstanceFrom(a, b);

            List<Integer> result = new ArrayList<>();
            while (iter.hasNext()) {
                result.add(iter.next());
            }

            int[] expected = {};
            int[] actual = result.stream().mapToInt(x->x).toArray();

            assertArrayEquals(expected, actual);
        }


        @Test
        public void test_one_empty() {
            int[] a = {1, 3, 5};
            int[] b = {};

            IntersectionIterator iter = createInstanceFrom(a, b);

            List<Integer> result = new ArrayList<>();
            while (iter.hasNext()) {. From 1point 3acres bbs
                result.add(iter.next());
            }

            int[] expected = {};. check 1point3acres for more.
            int[] actual = result.stream().mapToInt(x->x).toArray();

            assertArrayEquals(expected, actual);
        }
    }
    public static void main(String[] args) {
        int[] a = {1, 2, 4, 5, 6};
        int[] b = {1, 3, 5};

        Iterator<Integer> it1 = Arrays.stream(a).boxed().collect(Collectors.toList()).iterator();
        Iterator<Integer> it2 = Arrays.stream(b).boxed().collect(Collectors.toList()).iterator();

        IntersectionIterator sol = new IntersectionIterator(it1, it2);

        while (sol.hasNext()) {
            System.out.println(sol.next());-baidu 1point3acres
        }. From 1point 3acres bbs
    }

}