简体   繁体   English

无法在数组中打印最后增加的序列

[英]Unable to print last increasing sequence in array

I am writing an algorithm for printing increasing sequences in an array, but my current solution doesn't print only the last sequence. 我正在编写一个算法来打印数组中增加的序列,但我目前的解决方案不会只打印最后一个序列。 This is because the if conditions doesn't eval at the last index of the array. 这是因为if条件在数组的最后一个索引处没有eval。 The algorithm below should output [-10, 4] [1, 120, 150] [1, 5, 7] but instead it skips the [1, 5, 7] sequence. 下面的算法应输出[-10, 4] [1, 120, 150] [1, 5, 7] 1,5,7 [-10, 4] [1, 120, 150] [1, 5, 7]但它会跳过[1, 5, 7] 1,5,7 [1, 5, 7]序列。 Can someone please help me here! 有人可以帮我这里!

public class Sequence {
    public static void main(String[] args) {
        int[] test = {1000, -10, 4, 1, 120, 150, 1, 5, 7};
        Outputpattern(test);
    }

    public static void printList(int[] arr, int l, int u) {
        System.out.println(Arrays.toString(Arrays.copyOfRange(arr, l, u)));
    }

    public static void Outputpattern(int[] arr) {
        int i = 0;
        int l = 0;
        while (i < arr.length - 1) {
            if (arr[i] > arr[i + 1]) {
                if (i != l) {
                    printList(arr, l, i + 1);
                }
                l = i + 1;
            }
            i++;
        }
    }
}

You only print a sequence when you find a number that is lower than the previous one. 只有在找到低于前一个数字的数字时才打印序列。 But you also need to print a sequence when you reach the end. 但是当你到达终点时,你还需要打印一个序列。 This solution will only print the sequence at the end if it has length more than 1. 如果长度大于1,此解决方案将仅在末尾打印序列。

int i = 0;
int l = 0;
while (i < arr.length) {                                // I changed the bound
    if (i == arr.length - 1 || arr[i] > arr[i + 1]) {   // I added a 2nd condition
        if (i != l) {
            printList(arr, l, i + 1);
        }
        l = i + 1;
    }
    i++;
}

The while loop recognizes when a sequence ends and prints out that sequence. while循环识别序列何时结束并打印出该序列。 But after the loop has ended you simply need to print the last sequence. 但是在循环结束后,您只需要打印最后一个序列。

public static void Outputpattern(int[] arr) {
    int i = 0;
    int l = 0;
    while (i < arr.length - 1) {
        ...
    }

    // the last sequence
    printList(arr, l, i + 1);
}

To generalize your question: It is about to find sublists in a list while looping over the list. 概括您的问题:在循环列表时,它将在列表中查找子列表。 You can detect sublist boundaries within the loop and flush each sublist. 您可以检测循环中的子列表边界并刷新每个子列表。 But in the end you also need to flush the last list. 但最后你还需要刷新最后一个列表。

That said there seem to be implementation issues with your current boundary detection and the variables which remember that state. 也就是说,您当前的边界检测和记住该状态的变量似乎存在实施问题。

Take care on border conditions. 注意边境条件。

Try this: 尝试这个:

public static void outputPattern(int[] arr) {
        int l = 0;
        for (int i = 0; i < arr.length; i++) {
            if ((i < arr.length - 1) && (arr[i] <= arr[i + 1])) continue;
            if (i != l) printList(arr, l, i + 1);
            l = i + 1;
        }
    }

And please follow java naming conventions. 请遵循java命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM