找到最长的递减序列
我试图找到数组中最长的递减数字序列。我不确定我在下面的代码中做错了什么。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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?
我看到很多冗余逻辑..我认为这会做..
我考虑了@Reese Moore 的建议。
I see lot of redundant logic..i think this would do..
I considered @Reese Moore's suggestion.
这看起来非常像我在这里发布的伪代码。您没有涉及的一种边缘情况是如果循环从未运行则返回什么。我会将这些视为特殊情况。
例如
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.
据我所知,代码将正确计算最长序列的长度,只有一个错误导致它在某些情况下错误地计算该序列的起始位置。改成这样:
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:
看看你什么时候分配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.
我认为这可以更简单一点。
如果不需要,实际上不需要保留索引。
I think this could be a little bit simpler.
You don't actually need to keep the index if you don't need it.
看起来现在真正的问题是如何正确设置数据以便我可以在方法中使用它。
我没有正确声明数组以便在方法中使用它。但这个方法效果很好。
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.