Algorithm to tell if two arrays have identical members

前端 未结 16 3014
渐次进展
渐次进展 2020-11-29 06:52

What\'s the best algorithm for comparing two arrays to see if they have the same members?

Assume there are no duplicates, the members can be in any order, and that n

16条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 07:11

    Here is another option, let me know what you guys think.It should be T(n)=2n*log2n ->O(nLogn) in the worst case.

    private boolean compare(List listA, List listB){
        if (listA.size()==0||listA.size()==0) return true;
        List runner = new ArrayList();
        List maxList = listA.size()>listB.size()?listA:listB;
        List minList = listA.size()>listB.size()?listB:listA;
        int macthes = 0;
        List nextList = null;;
        int maxLength = maxList.size();
        for(int i=0;i

    }

提交回复
热议问题