找到最长的递减序列

发布于 2024-09-30 12:52:22 字数 1515 浏览 6 评论 0原文

我试图找到数组中最长的递减数字序列。我不确定我在下面的代码中做错了什么。

public static int getDecSeq(double[] data) {
  int currentSeq = 1;
  int currentIndex = 1;

  int longestSeq = 0;
  int longestIndex = 0;
  for (int i = currentIndex; i < data.length; i++) {
    if (data[i] < data[i - 1]) {
      currentSeq++;
    } else {
      currentSeq = 1;
      currentIndex = i;
    }
    if (currentSeq > longestSeq) {
      longestSeq = currentSeq;
      longestIndex = currentIndex;
    }
    //double[] sequence = new double[longestSeq];
    //for (int j = longestIndex; j < longestSeq; j++) {
      //sequence[j]
    //}
  }
  return longestSeq;
}//close getDecSeq 

看起来现在真正的问题是如何正确设置数据以便我可以在方法中使用它。

getData(input) 从文件中返回一堆数字并将它们存储在数组中。

我写道:

double[] data = getData(input);
System.out.println("longest sequence is" + getDecSeq(data));

我这样做是错误的。我的方法有效。当我将变量数据声明为:

double[] data = {119.1, 186.4, 46.3, 89.0 ...};

一切正常。 那么如何重写我调用数据的方式来工作呢?

获取数据是

public static double[] getData(Scanner input) {
   double[] list = new double[70]; //Construct an array, length 70, to hold values from file
   int count = 0;
   while (input.hasNextDouble()) {
      double n = input.nextDouble();
      list[count] = n;
      count++;
   }
   double[] newList = new double[count];
   for (int i = 0; i < newList.length; i++ ) {
      newList[i] = list[i];

   }
   return newList;
}//close getData

I am trying to find the longest sequence of decreasing numbers in an array. I am not sure what I am doing incorrectly in the following code.

public static int getDecSeq(double[] data) {
  int currentSeq = 1;
  int currentIndex = 1;

  int longestSeq = 0;
  int longestIndex = 0;
  for (int i = currentIndex; i < data.length; i++) {
    if (data[i] < data[i - 1]) {
      currentSeq++;
    } else {
      currentSeq = 1;
      currentIndex = i;
    }
    if (currentSeq > longestSeq) {
      longestSeq = currentSeq;
      longestIndex = currentIndex;
    }
    //double[] sequence = new double[longestSeq];
    //for (int j = longestIndex; j < longestSeq; j++) {
      //sequence[j]
    //}
  }
  return longestSeq;
}//close getDecSeq 

Looks like now the real problem is how to set up data correctly so that I can use it in the method.

getData(input) returns a bunch of numbers from a file and stores them in an array.

I wrote:

double[] data = getData(input);
System.out.println("longest sequence is" + getDecSeq(data));

I am doing this incorrectly. My methods work. When I declared the variable data as:

double[] data = {119.1, 186.4, 46.3, 89.0 ...};

Everything worked just fine.
So how do rewrite the way I call data to work?

getData is

public static double[] getData(Scanner input) {
   double[] list = new double[70]; //Construct an array, length 70, to hold values from file
   int count = 0;
   while (input.hasNextDouble()) {
      double n = input.nextDouble();
      list[count] = n;
      count++;
   }
   double[] newList = new double[count];
   for (int i = 0; i < newList.length; i++ ) {
      newList[i] = list[i];

   }
   return newList;
}//close getData

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦中楼上月下 2024-10-07 12:52:22

4444444 是否被视为正在减少?如果不是,那么您需要严格检查 < 而不是 <=

您能否提供更多信息,例如您的答案失败的地方?

Is 4444444 considered to be decreasing? If not then you want to check for strictly < rather than <=.

Could you provide a bit more, such as where your answer fails?

俏︾媚 2024-10-07 12:52:22

我看到很多冗余逻辑..我认为这会做..

public static int getDecSeq(int[] data) {
        int currentSeq = 1, longestSeq = 1;

        for (int i = 1; i < data.length; i++) {
            currentSeq = (data[i] < data[i - 1]) ? currentSeq + 1 : 1;
            if (currentSeq > longestSeq)
                longestSeq = currentSeq;
        }
        return longestSeq;
    }

我考虑了@Reese Moore 的建议。

I see lot of redundant logic..i think this would do..

public static int getDecSeq(int[] data) {
        int currentSeq = 1, longestSeq = 1;

        for (int i = 1; i < data.length; i++) {
            currentSeq = (data[i] < data[i - 1]) ? currentSeq + 1 : 1;
            if (currentSeq > longestSeq)
                longestSeq = currentSeq;
        }
        return longestSeq;
    }

I considered @Reese Moore's suggestion.

锦欢 2024-10-07 12:52:22

This looks strikingly like my pseudocode posted here. One edge case you haven't covered is what to return if the loop is never run. I would treat these as special cases.

e.g.

 if ( data.length < 2 ) return data.length;
紫竹語嫣☆ 2024-10-07 12:52:22

据我所知,代码将正确计算最长序列的长度,只有一个错误导致它在某些情况下错误地计算该序列的起始位置。改成这样:

int currentSeq = 1;
int currentIndex = 0;

int longestSeq = 0;
int longestIndex = 0;
for (int i = 1; i < data.length; i++) {
...

As far as I can tell, the code will correctly calculate the length of the longest sequence, there's only one bug that makes it incorrectly calculate the starting position of that sequence in some cases. Change it to this:

int currentSeq = 1;
int currentIndex = 0;

int longestSeq = 0;
int longestIndex = 0;
for (int i = 1; i < data.length; i++) {
...
弱骨蛰伏 2024-10-07 12:52:22

看看你什么时候分配longestSeq和longestIndex。如果您首先在纸上勾勒出算法对给定测试输入执行的操作,然后运行算法,同时观察(打印)每次迭代的 Seq 和 Index 值,这将会有所帮助。

Take a look at when you assign longestSeq and longestIndex. It will help if you sketch out on paper first what you intend the algorithm to do for a given test input, and then run the algorithm while watching (printing) the Seq and Index values on each iteration.

强辩 2024-10-07 12:52:22

我认为这可以更简单一点。
如果不需要,实际上不需要保留索引。

public int getDecSeq(double[] data)
{

    int curSequence = 1;
    int maxSequence = 1;

    for (int i = 1; i <= data.Length - 1; i++) {
        if (data[i] > data[i - 1]) {
            if (curSequence > maxSequence) {
                maxSequence = curSequence;
            }

            curSequence = 1;
        } else {
            curSequence += 1;
        }
    }

    if (curSequence > maxSequence) {
        maxSequence = curSequence;
    }

    return maxSequence;
}

I think this could be a little bit simpler.
You don't actually need to keep the index if you don't need it.

public int getDecSeq(double[] data)
{

    int curSequence = 1;
    int maxSequence = 1;

    for (int i = 1; i <= data.Length - 1; i++) {
        if (data[i] > data[i - 1]) {
            if (curSequence > maxSequence) {
                maxSequence = curSequence;
            }

            curSequence = 1;
        } else {
            curSequence += 1;
        }
    }

    if (curSequence > maxSequence) {
        maxSequence = curSequence;
    }

    return maxSequence;
}
静赏你的温柔 2024-10-07 12:52:22

看起来现在真正的问题是如何正确设置数据以便我可以在方法中使用它。

我没有正确声明数组以便在方法中使用它。但这个方法效果很好。

Looks like now the real problem is how to set up data correctly so that I can use it in the method.

I hadn't declared the array correctly in order to use it in the method. But the method works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文