我对编程很陌生,这是我的第一篇文章。
我尝试创建一个采用数组(来自文本文件)的方法,比较序列中的值以找到最长的非递减序列(值而不是索引)。
例如,在阵列[2 3 4 1 50 2 3 3 4 5 1 4]中应该返回 2 3 3 4 5 (不是 5 )。
我一直试图关注这篇文章Longest Increasing Sequence,但它使用的是ArrayLists(根据作业我不允许使用)
这是我到目前为止的代码:
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();
}
}
}
我非常感谢任何关于我做错的方向。
答案 0 :(得分:0)
由于这是一项任务,我不会详细说出你的全部答案,但提供一些指示。
将问题分成子问题可能是个好主意。这也可以让您更轻松地了解正在发生的事情。
一旦你的循环结束,你可以使用System.arraycopy()(或你自己的循环,如果这是被禁止的)将所有值从最长的序列复制到一个新的数组中。毕竟,您现在知道最长序列的起点,并且您知道该序列有多长。这意味着您甚至可以知道目标阵列的初始大小。
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:0)
这里有:
public static int[] longestNonDecreasingSequence(int[] fullSequence) {
int[] maxSequence = new int[0];
int[] tmpSequence = new int[fullSequence.length];
int tmpSequenceLength = 0;
for (int i=0;i<fullSequence.length;i++) {
if (i==0 || fullSequence[i] >= fullSequence[i-1]) {
tmpSequence[tmpSequenceLength] = fullSequence[i];
tmpSequenceLength++;
} else {
if (tmpSequenceLength>maxSequence.length) {
maxSequence = new int[tmpSequenceLength];
System.arraycopy(tmpSequence,0,maxSequence,0,maxSequence.length);
}
tmpSequence[0] = fullSequence[i];
tmpSequenceLength=1;
}
}
return maxSequence;
}
答案 3 :(得分:0)
您可以简单地遍历数字列表两次。首先找出最长序列的长度,然后找出最长的序列。这应该可以解决问题:
int[] nums = new int[] {2, 3, 4, 1, 50, 2, 3, 3, 4, 5, 1, 4};
int prevNum = Integer.MIN_VALUE;
int longest = 0;
int seq = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] >= prevNum) {
seq += 1;
if(seq > longest) {
longest = seq;
}
} else {
seq = 0;
}
prevNum = nums[i];
}
System.out.println("Longest: " + index);
int index = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] >= prevNum) {
seq += 1;
if(seq == longest) {
index = i - longest;
longest = seq;
}
} else {
seq = 0;
}
prevNum = nums[i];
}
int[] longestSequenceValues = Arrays.copyOfRange(nums, index, index + longest + 1);
System.out.println(Arrays.toString(longestSequenceValues));
// Output: [2, 3, 3, 4, 5]