【问题标题】:How to count duplicates in a list using a circular linked list如何使用循环链表计算列表中的重复项
【发布时间】:2021-11-10 16:45:47
【问题描述】:

我正在扩展我的循环链表,并实现了一些方法来添加和删除列表中的节点。我想创建一个方法来计算列表中的重复项,该方法应该返回列表中多次出现的对象的数量。 例如一个列表有 [30, 30, 50, 50, 80, 90, 10, 10] 方法应该返回:重复的数量是 3

我试过实现下面的方法,但是这个方法只计算列表中的节点总数,我应该添加什么来使它计算列表中的重复项。

public int countNode() {
    int count = 0;

    if (!isEmpty()) {
        Node<E> temp = current;
        Node<E> prev = current;

        do {
            if (prev.element == temp.element)
                count++;
                temp = temp.next;
                prev = prev.next;
        } while (temp != current);
    }

    return count;
}

【问题讨论】:

    标签: java do-while circular-list


    【解决方案1】:

    简单的解决方案是使用 Java 的 HashSet 数据结构,它只允许唯一值。您需要在迭代时将每个节点的值添加到集合中。最后,集合的大小和列表的大小之间的差异将是您的答案。

    这里我通过将循环链表表示为整数数组来简化示例代码:

    // Your linkedlist, shown as an array
    int[] arr = new int[] {30, 30, 50, 50, 80, 90, 10, 10};
    
    // Allows only unique values
    HashSet<Integer> set = new HashSet<Integer>();
    
    // Fill your set
    for(int n : arr) {
        set.add(n);
    }
    
    // answer = list's length - set's length
    System.out.println(arr.length - set.size());
    

    【讨论】:

      【解决方案2】:

      您正在使用同一节点初始化 prevtemp。所以比较总是返回true,两个节点一起继续

      【讨论】:

        【解决方案3】:

        要定义一个唯一的对象 Node(相对于原语)定义它的hasCodeequals 如下所示mre

        import java.util.*;
        
        public class Main   {
        
            public static void main(String[] args) {
                int[] values = new int[] {30, 30, 50, 50, 80, 90, 10, 10};
        
                Node root = null, node = null;
                for(int i : values){
                    if(root == null) {
                        root= new Node(i);
                    }else if(node == null){
                        node = new Node(i);
                        root.setNext(node);
                    }else{
                        Node temp = new Node(i);
                        node.setNext(temp);
                        node = temp;
                    }
                }
        
                System.out.println("Number of distinct Nodes: "+countUniqueNodes1(root));
                System.out.println("Number of distinct Nodes: "+countUniqueNodes2(root));
            }
        
            //not null safe
            public static int countUniqueNodes1(Node root){
                Node next = root.getNext();
                Set<Node> nodesSet = new HashSet<>();
                nodesSet.add(root);
        
                while (next != null){
                    nodesSet.add(next); 
                    next = next.getNext();
                }
                return nodesSet.size();
            }
        
            //not null safe
            public static int countUniqueNodes2(Node root){
                Node next = root.getNext();
                List<Node> nodes = new ArrayList<>();
                nodes.add(root);
        
                while (next != null){
                    nodes.add(next);
                    next = next.getNext();
                }
                return (int) nodes.stream().distinct().count();
            }
        }
        
        class Node{
        
            final int value;
            private Node next;
        
            public Node(int value) {
                this(value, null);
            }
        
            public Node(int value, Node next) {
                this.value = value;
            }
        
            public Node getNext() {
                return next;
            }
        
            public void setNext(Node next) {
                this.next = next;
            }
        
            @Override
            public int hashCode() {
                return String.valueOf(value).hashCode();
            }
        
            @Override
            public boolean equals(Object obj) {
                if (!(obj instanceof Node)) return false;
                return value == ((Node)obj).value;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-21
          • 2020-01-06
          • 1970-01-01
          • 2017-01-13
          • 1970-01-01
          • 2019-02-04
          • 2011-03-30
          • 1970-01-01
          相关资源
          最近更新 更多