比较数组中的索引值并返回一个新数组
问题描述:
我是编程的新手,这是我的第一篇文章。比较数组中的索引值并返回一个新数组
我试图创建一个方法,该方法需要一个数组(从文本文件),比较序列中的值以查找最长的非递减序列(值非索引)。
例如,阵列[2 3 4 1 50 2 3 3 4 5 1 4] 中应返回(未)。
我一直在试图按照本文Longest Increasing Sequence,但它使用的ArrayList(这我不能每次分配使用)
这里是我的代码至今:
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();
}
}
}
任何方向,我不正确的行为将不胜感激。
答
由于这是一项任务,所以我不会详细解释你的完整答案,而是提供一些指导。
将您的问题划分为子问题可能是一个好主意。这也可以让你更容易地了解正在发生的事情。
- 创建一个帮助器方法,该方法将开始索引作为参数,并返回该索引向前的非递减值的数量。
- 声明两个局部变量,一个存储起始索引,另一个存储迄今为止找到的最长序列的长度。
- 在循环中,遍历每个索引并使用助手方法检查从该索引开始的序列的长度。如果它比您当前存储的长度更长,请记住该索引和长度作为新的最佳选择。
一旦你的循环完成后,您可以使用System.arraycopy()(或你自己的循环,如果这是禁止的)所有值从最长的序列复制到一个新的数组。毕竟,您现在知道最长序列的起点,并且您知道该序列有多长。这意味着你甚至知道你的目标数组的初始大小。
答
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;
}
}
答
在这里你有:
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;
}
答
你可以简单地通过号码列表循环两次。首先要找出最长序列的长度,然后找出最长的序列。这应该做的伎俩:
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]
这个工程。谢谢! – TomassinaJ