簡體   English   中英

在 O(n) 時間內在已排序的鏈表中查找重復項

[英]find duplicates in a sorted linkedlist in O(n) time

最有效的方法是什么? 使用迭代器好嗎?

public class FindDuplicates {
    public static void main(String arg[]){
        int[] str={1 , 2 , 3 ,4  ,5 ,3 ,5 , 4,3,43,1,33,4,5};
        List<Integer> list = new LinkedList<Integer>();

        for(int x : str) {   
            list.add(x);    
        }

        Collections.sort(list);
        System.out.println(list);

        Iterator<Integer> it = list.listIterator();  
        while(it.hasNext() && it.next() != null) { 
            /*   Pseudocode =>   if(it.next().equals(it.next.next)); */
            /* OR Pseudocode =>  if(it.next() == it.next().next) */ 
            System.out.println(it) ;
        }
    }
}

您需要將先前的值保留在變量中,應該類似於以下內容:

 Iterator<Integer> it = list.listIterator(); 
 if (it.hasNext()) {
   Integer previous = it.next();
   while(it.hasNext()) { 
     Integer current = it.next();
     if (previous.equals(current)) {
       System.out.println("Dupe: " + current);
     }
     previous = current;
   }
 }

請注意,您在這里並不真正需要鏈表(正如其他人已經指出的那樣),您可以就地排序,然后使用單個循環掃描數組:

int[] str={1 , 2 , 3 ,4  ,5 ,3 ,5 , 4,3,43,1,33,4,5};
Arrays.sort(str);
for (int i = 1; i < str.length; i++) {
  if (str[i] == str[i - 1]) {
    System.out.println("Dupe: " + str[i];
  }
}

如果您不想更改 str,只需跟蹤 HashSet 中的元素:

int[] str={1 , 2 , 3 ,4  ,5 ,3 ,5 , 4,3,43,1,33,4,5};
HashSet<Integer> seen = new HashSet<Integer>();
for (int i: str) {
  if (seen.contains(i)) {
    System.out.println("Dupe: " + i);
  } else {
    seen.add(i);
  }
}

如果你考慮哈希表操作 O(1),這也會給你線性時間而不是 O(n log n)

首先,請注意您使用的是Collections.sort ,它的復雜度高於 O(n)。

關於您的問題:在復雜度為 O(n) 的排序列表中查找重復項:

您可以只將一個變量設置為您遇到的前一個值,這樣在循環中,您只需將當前值與該變量進行比較,然后將變量設置為當前值。

更好的方法是 . 除非您對space complexity有限制,否則請使用Hash Set代替高於O(N)復雜度的Collections.sort 一個基本的偽代碼會是這樣的。

  if(set.contains(it)){// Check if it.info is in map or not
    System.out.println(it.info); // If it was then it is duplicate element hence print it
    }
  else{
    set.put(it);// Else if this is the first time you saw this element put it in set.
    }

此代碼具有O(N)時間復雜度。

int[] str={1 , 2 , 3 ,4  ,5 ,3 ,5 , 4,3,43,1,33,4,5};

您不需要使用LinkedList來排序以查找重復項。 使用Arrays.sort(int[])對數組進行排序然后掃描:以下程序將打印具有重復項的整數以及重復項計數:

int a[] = {1, 1, 1, 5, 5, 4, 4, 3, 3};

        Arrays.sort(a);

        for(int i=1 ; i<a.length;)
        {
            if(a[i] == a[i-1])
            {
                int j = i;
                for(; j<a.length; j++)
                    if(a[j]!=a[i])break;

                if(j > i)
                    System.out.println("Found duplicate: "+a[i]+" counted: "+(j - i+1));

                i = j;
            }
            else i++;

        }

樣本輸出:

Found duplicate: 1 counted: 3
Found duplicate: 3 counted: 2
Found duplicate: 4 counted: 2
Found duplicate: 5 counted: 2

所有帶數組、帶列表、帶鏈表的表格。

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void withArray() {
        int[] str = { 1, 2, 3, 4, 5, 3, 5, 4, 3, 43, 1, 33, 4, 5 };
        Arrays.sort(str);

        int previous = str[0];
        for (int i = 1; i < str.length; ++i) {
            if (str[i] == previous) {
                System.out.println("Duplicate " + previous + " and " + str[i]);
            }
            previous = str[i];
        }
        System.out.println();
    }

    public static void withList() {
        List<Integer> str = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 4, 3, 43, 1, 33,
                4, 5);
        Collections.sort(str);

        int previous = str.get(0);
        for (int i = 1; i < str.size(); ++i) {
            if (str.get(i) == previous) {
                System.out.println("Duplicate " + previous + " and "
                        + str.get(i));
            }
            previous = str.get(i);
        }
        System.out.println();
    }

    public static void withLinkedList() {
        LinkedList<Integer> str = new LinkedList<Integer>(Arrays.asList(1, 2,
                3, 4, 5, 3, 5, 4, 3, 43, 1, 33, 4, 5));
        Collections.sort(str);

        Iterator<Integer> it = str.iterator();

        int previous = it.next();
        int cur;
        while (it.hasNext()) {
            cur = it.next();
            if (cur == previous) {
                System.out.println("Duplicate " + previous + " and " + cur);
            }
            previous = cur;
        }
        System.out.println();
    }

    public static void main(String arg[]) {
        withArray();
        withList();
        withLinkedList();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM