为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕标琳琳 2021-08-13 16:41:34
我需要检查我存储在 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");        }    }
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

这适用于 O(n)


import java.util.LinkedList;


public class TestLinkedList {


    public static void main(String[] args) {


        LinkedList<Integer> a = new LinkedList<Integer>();

        a.add(124);

        a.add(125);

        a.add(126);

        a.add(1900);

        a.add(1901);


        int index = 0;

        int index1 = 0;


        for (int i = 0; i < a.size(); i++) {

            if (i+1 < a.size() && a.get(i) + 1 == a.get(i + 1)) {

                index1 = i + 1;

            } else {

                if (index != index1) {

                    System.out.println(index + " " + index1);

                }

                index = i+1;

                index1 = i+1;

            }

        }


    }


}

输出


0 2

3 4


查看完整回答
反对 回复 2021-08-13
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

  String serialIndex = "";

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

        {serialIndex += "("+Integer.toString(o-1);

            while(i<zeroIndex.size() && zeroIndex.get(o-1)+1 == zeroIndex.get(o))

            {  i++;

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

            }

         serialIndex = serialIndex+Integer.toString(i-1)+"),";

     }

     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)

我没有你的完整代码来测试,所以这是我能做的最好的。如果你遇到任何错误,请告诉我。


查看完整回答
反对 回复 2021-08-13
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

这是有关如何执行此操作的快速示例。首先,创建我们的列表。


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   

int indexStart = 0; //the index of the beginning of the current sequence

int previous = a.get(0); //the previous value


int 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){

    //get the current value

    current = a.get(i); 


    //if the sequence is broken, print the index and print also the sublist using `List.subList`.

    if(current != previous + 1){ 

        System.out.format("Sequence from %d to %d%n", indexStart, i - 1);

        System.out.println(a.subList(indexStart, i));


        //reset the start of the current sequence

        indexStart = i; 

    }


    //update the previous value with the current for the next iteration.

    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.


查看完整回答
反对 回复 2021-08-13
  • 3 回答
  • 0 关注
  • 272 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信