首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >比较数组中的索引值并返回新数组

比较数组中的索引值并返回新数组
EN

Stack Overflow用户
提问于 2017-08-01 16:21:36
回答 4查看 90关注 0票数 1

我对编程非常陌生,这是我的第一篇文章。

我正在尝试创建一个方法,它接受一个数组(从文本文件中),比较序列中的值,以找到最长的不递减序列(值,,而不是索引)。

例如,在数组2 3 4 1 5 0 2 3 4 4 1 4 4 2 3 3 4 5 5中(而不是5)。

我一直试图遵循这篇文章最长增长序列,但它使用ArrayLists (我不允许在分配中使用)

下面是我到目前为止掌握的代码:

代码语言:javascript
运行
复制
public double brightSequenceNonDec()throws FileNotFoundException{

    double[] numbersInSequence = new double[numbersOfLines];
    for (int i = 0; i < listElements.length; i++) {
        for (int k = i + 1; k < listElements.length; k++) {
            if (listElements[i].brightness()<=listElements[k].brightness()) {
                numbersInSequence[0] = listElements[i].brightness();

            }
        }
    }

任何关于我做错了什么的指示都会受到极大的感谢。

EN

Stack Overflow用户

发布于 2017-08-01 16:45:20

代码语言:javascript
运行
复制
public class LongestIncreasingSequence {

double[] longestSequence(double input[]) {

    // variable start and end will store the starting index and ending index
    // of array
    // variable max will store the length of sequence and compare it to the
    // older length
    // length will store the maximum length
    int start = 0, end = 0, length = 0, index, max = 0;
    int arrayLength = input.length;

    for (index = 1; index < arrayLength; index++) {
        if (input[index] >= input[index - 1]) {
            max++;
        }

        else {
            if (max > length) {
                length = max;
                end = index;
                start = end - length - 1;
            }
            max = 0;
        }
    }

    // this condition will work when the last element is also the part of
    // the longest sequence
    if (max > length) {
        length = max;
        end = index;
        start = end - length - 1;
    }

    int resultLength = end - start;

    double result[] = new double[resultLength];

    for (index = 0; index < resultLength; index++, start++) {
        result[index] = input[start];
    }

    return result;
}

}

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45442983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档