【问题标题】:How can I find if a number in a doubly linked list is between max and min?如何查找双向链表中的数字是否介于最大值和最小值之间?
【发布时间】:2019-02-28 02:32:10
【问题描述】:

我想创建一个名为 inBetween 的方法,它接受一个项目作为参数,如果项目位于最小和最大列表元素之间,则返回 true。即基于为列表元素定义的 compareTo 方法,item 大于最小列表元素且小于最大列表元素。否则,该方法返回 false(即使项目“匹配”最小或最大元素)。

public class DoublyLinkedList {

private Link first;               // ref to first item
private Link last;                // ref to last item
// -------------------------------------------------------------

public DoublyLinkedList() // constructor
{
    first = null;                  // no items on list yet
    last = null;
}
// -------------------------------------------------------------

public boolean isEmpty() // true if no links
{
    return first == null;
}
// -------------------------------------------------------------

public void insertFirst(long dd) // insert at front of list
{
    Link newLink = new Link(dd);   // make new link

    if (isEmpty()) // if empty list,
    {
        last = newLink;             // newLink <-- last
    } else {
        first.previous = newLink;   // newLink <-- old first
    }
    newLink.next = first;          // newLink --> old first
    first = newLink;               // first --> newLink
}
// -------------------------------------------------------------

public void insertLast(long dd) // insert at end of list
{
    Link newLink = new Link(dd);   // make new link
    if (isEmpty()) // if empty list,
    {
        first = newLink;            // first --> newLink
    } else {
        last.next = newLink;        // old last --> newLink
        newLink.previous = last;    // old last <-- newLink
    }
    last = newLink;                // newLink <-- last
}
// -------------------------------------------------------------

public Link deleteFirst() // delete first link
{                              // (assumes non-empty list)
    Link temp = first;
    if (first.next == null) // if only one item
    {
        last = null;                // null <-- last
    } else {
        first.next.previous = null; // null <-- old next
    }
    first = first.next;            // first --> old next
    return temp;
}
// -------------------------------------------------------------

public Link deleteLast() // delete last link
{                              // (assumes non-empty list)
    Link temp = last;
    if (first.next == null) // if only one item
    {
        first = null;               // first --> null
    } else {
        last.previous.next = null;  // old previous --> null
    }
    last = last.previous;          // old previous <-- last
    return temp;
}
// -------------------------------------------------------------
// insert dd just after key

public boolean insertAfter(long key, long dd) {                              
// (assumes non-empty list)
    Link current = first;          // start at beginning
    while (current.dData != key) // until match is found,
    {
        current = current.next;     // move to next link
        if (current == null) {
            return false;            // didn't find it
        }
    }
    Link newLink = new Link(dd);   // make new link

    if (current == last) // if last link,
    {
        newLink.next = null;        // newLink --> null
        last = newLink;             // newLink <-- last
    } else // not last link,
    {
        newLink.next = current.next; // newLink --> old next
        // newLink <-- old next
        current.next.previous = newLink;
    }
    newLink.previous = current;    // old current <-- newLink
    current.next = newLink;        // old current --> newLink
    return true;                   // found it, did insertion
}
// -------------------------------------------------------------

public Link deleteKey(long key) // delete item w/ given key
{                              // (assumes non-empty list)
    Link current = first;          // start at beginning
    while (current.dData != key) // until match is found,
    {
        current = current.next;     // move to next link
        if (current == null) {
            return null;             // didn't find it
        }
    }
    if (current == first) // found it; first item?
    {
        first = current.next;       // first --> old next
    } else // not first
    // old previous --> old next
    {
        current.previous.next = current.next;
    }

    if (current == last) // last item?
    {
        last = current.previous;    // old previous <-- last
    } else // not last
    // old previous <-- old next
    {
        current.next.previous = current.previous;
    }
    return current;                // return value
}
// -------------------------------------------------------------

public void displayForward() {
    System.out.print("List (first-->last): ");
    Link current = first;          // start at beginning
    while (current != null) // until end of list,
    {
        current.displayLink();      // display data
        current = current.next;     // move to next link
    }
    System.out.println("");
}
// -------------------------------------------------------------

public void displayBackward() {
    System.out.print("List (last-->first): ");
    Link current = last;           // start at end
    while (current != null) // until start of list,
    {
        current.displayLink();      // display data
        current = current.previous; // move to previous link
    }
    System.out.println("");
}
// -------------------------------------------------------------

public DoublyLinkedList inBetween(long n) {

}
}  // end class DoublyLinkedList
////////////////////////////////////

public class InBetweenDemo
{
public static void main(String[] args)
  {                             // make a new list
  DoublyLinkedList theList = new DoublyLinkedList();

  theList.insertFirst(22);      // insert at front
  theList.insertFirst(44);
  theList.insertFirst(66);

  theList.insertLast(11);       // insert at rear
  theList.insertLast(33);
  theList.insertLast(55);

  theList.displayForward(); 
  int n=55;// display list forward
  System.out.println("inBetween("+n+") "+ inBetween(n));
  theList.displayBackward();    // display list backward

  theList.deleteFirst();        // delete first item
  n=55;
  System.out.println("inBetween("+n+") "+ theList.inBetween(n));

  theList.deleteLast(); 
  n=33;
  System.out.println("inBetween("+n+") "+ theList.inBetween(n));
  theList.deleteKey(22);        // delete item with key 11
  System.out.println("inBetween("+n+") "+ theList.inBetween(n));

  theList.displayForward();     // display list forward

  theList.insertAfter(11, 77);  // insert 77 after 22
  theList.insertAfter(33, 88);  // insert 88 after 33

  theList.displayForward();     // display list forward
  }  // end main()
}  // end class DoublyLinkedApp
////////////////////////////////////////////////////////////////

我在想我可以分配一个最大值和最小值,然后检查参数是否小于和大于每个相应的值。如果是,那么我会返回 true,如果不是,则返回 false。我不确定如何开始在无序列表中查找最大值和最小值的代码。

【问题讨论】:

  • 只需浏览您的链接列表,创建最小、最大变量。将 min 和 max 设置为第一个值,如果下一个值是 max 那么 max = value。
  • 为什么inBetween()返回DoublyLinkedList??
  • @shmosel 我应该使用布尔值来代替,因为我想返回 true 吗?

标签: java


【解决方案1】:

只需迭代列表并确保您的项目至少少于一个元素且多于另一个:

public boolean inBetween(long n) {
    boolean less = false, more = false;
    for (Link current = first; current != null; current = current.next)
        if ((less |= n < current.dData) & (more |= n > current.dData))
            return true;
    return false;
}

【讨论】:

    【解决方案2】:

    我认为你应该首先创建两个变量,MAX 和 MIN。之后,遍历列表并找到 MAX 和 MIN 值。所以,选择你想要的值并进行比较。如果该值大于 MIN 且小于 MAX,则为有效数字。我建议您在 List 的类上添加一个名为 listLenght 的变量。添加内容时,更新变量 listLenght。删除时,请执行相同操作。

    【讨论】:

    • 我在整理列表时遇到了麻烦。到目前为止,我将最小值和最大值以及当前初始化为列表中的第一个值。然后我让它检查 current.next 是否小于 min 但我在那里遇到错误
    【解决方案3】:

    有两种方法可以解决您的问题。 (可能还有其他方法)

    1. 您可以创建另一种方法,通过您的链接列表找到min 和max。只需在您的 inBetween 方法中调用此方法即可。此方法将具有 O(n) 的最坏情况。 (如果您打算这样做,那么拥有min 和max 的变量是不够的,每次调用inBetween 时都必须调用该方法,因为值可能已更新)
    2. 为min 和max 设置一个变量,然后在每次插入和删除后更新它。它必须是Link 类型。在插入中,它只会有 O(1) 的运行时间,因为您将直接比较它们的值。在删除时,您必须比较密钥,如果它具有相同的密钥,那么您必须找到另一个 min 或 max。因此,您还应该创建一个方法来查找min 和max。在您的inBetween 方法中,您只需要获取变量min 和max。 min 和 max 的值不可能在执行 inBetween 时更新,因为您在每次插入和删除时都会更新 min 和 max。

    所以你去吧,只需从两者中选择你要实现的。

    【讨论】:

    • 最好的答案在这里,因为它提供了两个选项。哪个选项“更好”取决于您是经常拨打insert 而很少拨打inBetween,还是相反。
    【解决方案4】:

    遍历列表以找到min 和max 值,然后如果输入值大于min 且小于max,则返回true:

    public static boolean isBetween(List<Integer> list, int value){
    
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
    
        for (int i : list){
    
            if (i < min)
                min = i;
    
            if (i > max)
                max = i;
        }
    
        return value > min && value < max;
    }
    

    【讨论】:

    • return value &gt; Collections.min(list) &amp;&amp; value &lt; Collections.max(list);
    • 这将无缘无故地遍历列表两次
    • IntSummaryStatistics summary = list.stream().mapToInt(i -&gt; i).summaryStatistics(); return value &gt; summary.getMin() &amp;&amp; value &lt; summary.getMax();
    • @shmosel - 在 LinkedIn 上连接。链接在我的个人资料上。
    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多