如何检查LinkedList中的数字是否按顺序排列

我需要检查我存储在 LinkedList 中的数字是否按顺序排列。


示例集为:123、124、125、1900、1901。


如果代码遇到 123,它会检查下一个 124 直到 125,然后停止,因为当您自然计数时,1900 不是 125 之后的下一个数字。所以我需要获取第一个(123)和最后一个序列(125)的索引。然后进入下一个序列,1900 年和 1901 年。


    for(int o = 0; o < zeroIndex.size(); o++)

    {

        if(-1 == (zeroIndex.get(o) - zeroIndex.get(o+1)))

        {

            System.out.println(zeroIndex.get(o) + "trailing");

        }

    }


慕标琳琳
浏览 273回答 3
3回答

慕无忌1623718

这适用于 O(n)import java.util.LinkedList;public class TestLinkedList {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; LinkedList<Integer> a = new LinkedList<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; a.add(124);&nbsp; &nbsp; &nbsp; &nbsp; a.add(125);&nbsp; &nbsp; &nbsp; &nbsp; a.add(126);&nbsp; &nbsp; &nbsp; &nbsp; a.add(1900);&nbsp; &nbsp; &nbsp; &nbsp; a.add(1901);&nbsp; &nbsp; &nbsp; &nbsp; int index = 0;&nbsp; &nbsp; &nbsp; &nbsp; int index1 = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < a.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i+1 < a.size() && a.get(i) + 1 == a.get(i + 1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index1 = i + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index != index1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(index + " " + index1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = i+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index1 = i+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出0 23 4

慕尼黑5688855

&nbsp; String serialIndex = "";&nbsp; &nbsp; for(int o = 1; o < zeroIndex.size(); o++)&nbsp; &nbsp; &nbsp; &nbsp; {serialIndex += "("+Integer.toString(o-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(i<zeroIndex.size() && zeroIndex.get(o-1)+1 == zeroIndex.get(o))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(zeroIndex.get(o) + "trailing");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;serialIndex = serialIndex+Integer.toString(i-1)+"),";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;System.out.println(serialIndex);我们将循环到链表并检查前一个值是否比当前值小 1。如果此条件为真,我们将增加 i 否则我们将 break 循环并将该 i 添加到 ans例如123, 124, 125, 1900, 1901. 我们将从124开始 ----- 我们的serialIndex字符串将为(0和 124 比 123 大 1,因此我们增加 i。当我们达到 1900 时,我们将打破 while 循环: 1900 不是 1 大于 125,现在我们的 serialIndex 字符串将是 b (0,2)。最后我们将有 serialIndex 字符串为(0,2),(3,4)我没有你的完整代码来测试,所以这是我能做的最好的。如果你遇到任何错误,请告诉我。

慕侠2389804

这是有关如何执行此操作的快速示例。首先,创建我们的列表。List<Integer> a = new LinkedList<Integer>();a.add(124);a.add(125);a.add(126);a.add(1900);a.add(1901);所以,既然我们有了一个清单,让我们开始吧。首先,声明我们的变量int current; //will hold the current value during the iteration&nbsp; &nbsp;int indexStart = 0; //the index of the beginning of the current sequenceint previous = a.get(0); //the previous valueint length = a.size(); //the length (optionnal, but this will be used later)然后,有趣的标准来了(完全评论)//Iterate from 1 to the end (0 is already in `previous`for(int i = 1 ; i < length; ++i){&nbsp; &nbsp; //get the current value&nbsp; &nbsp; current = a.get(i);&nbsp;&nbsp; &nbsp; //if the sequence is broken, print the index and print also the sublist using `List.subList`.&nbsp; &nbsp; if(current != previous + 1){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.format("Sequence from %d to %d%n", indexStart, i - 1);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a.subList(indexStart, i));&nbsp; &nbsp; &nbsp; &nbsp; //reset the start of the current sequence&nbsp; &nbsp; &nbsp; &nbsp; indexStart = i;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; //update the previous value with the current for the next iteration.&nbsp; &nbsp; previous = current;}//Print the last sequence.System.out.format("Sequence from %d to %d%n", indexStart, length - 1);System.out.println(a.subList(indexStart, length));这将打印:从 0 到 2 的序列[124, 125, 126]从 3 到 4 的序列[1900, 1901]这很简单,只需迭代循环并保留前一个和当前值即可检查序列是否正确。请注意,对于 a LinkedList,我会使用 anIterator但我需要 anint index所以这会提供更长的解决方案,因此为了保持简单,我使用了List.get.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java