How to intersect two sorted integer arrays without duplicates?

后端 未结 6 2013
庸人自扰
庸人自扰 2020-12-30 08:02

This is an interview question that I am using as a programming exercise.

Input: Two sorted integer arrays A and B in increasing order and of differe

6条回答
  •  自闭症患者
    2020-12-30 08:50

    Here's a memory improvement:

    It would be better to store your results (C) in a dynamic structure, like a linked list, and create an array after you're done finding the intersecting elements (exactly as you do with array r). This technique would be especially good if you have very large arrays for A and B and expect the common elements to be few in comparison (why search for a huge chunk of contiguous memory when you only need a small amount?).

    EDIT: one more thing that I would change, and this might be just a little nit-picky, is that I would avoid using unbound loops when the worst case number of iterations is known before hand.

提交回复
热议问题