Java-查找连续元素最长递增序列的程序(逻辑错误)
我目前正在创建一个程序,用户在其中输入一个整数数组。程序必须找到连续元素的最长递增序列。因此,如果用户输入“3,2,1,2,4,6,7,8,1,2”,程序将输出“1,2,4,5,6,7,8”。但是,我一直遇到两个错误 第一个错误是当xs=1000977774878274972837时。程序将输出“100097777”而不是“4878274972837”。逻辑上这是错误的,因为第一个输出不是连续元素的“最长”递增序列 第二个错误是当xs=2,7时。它似乎输出一个空数组,而不是“2,7”。我想可能是因为元素不够Java-查找连续元素最长递增序列的程序(逻辑错误),java,arrays,arraylist,Java,Arrays,Arraylist,我目前正在创建一个程序,用户在其中输入一个整数数组。程序必须找到连续元素的最长递增序列。因此,如果用户输入“3,2,1,2,4,6,7,8,1,2”,程序将输出“1,2,4,5,6,7,8”。但是,我一直遇到两个错误 第一个错误是当xs=1000977774878274972837时。程序将输出“100097777”而不是“4878274972837”。逻辑上这是错误的,因为第一个输出不是连续元素的“最长”递增序列 第二个错误是当xs=2,7时。它似乎输出一个空数组,而不是“2,7”。我想可能是
static int[] increasing(int[] xs){
ArrayList<Integer> current_array = new ArrayList<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
int c_counter = 0;
for (int i = 0; i<(xs.length); i++){
if (i==0){
if (xs[i+1] > xs[i]){
current_array.add(xs[i]);
c_counter++; //keeps track of how many elements have been added
}
}
else if ((xs[i] > xs[i-1])){
if (c_counter==0){
current_array.add(xs[i-1]); //makes sure the smaller number gets added too
current_array.add(xs[i]);
c_counter = c_counter + 2;
} else{
current_array.add(xs[i]);
c_counter++;
}
} else {
if (current_array.size()>list.size()){ //compares sizes to find the longest sequence
list.clear();
for (int k=0; k<(current_array.size()); k++){
if (current_array.get(k) != 0){ //removes any null values
list.add(current_array.get(k));
}
}
current_array.clear(); //clears it to restart and find any longer sequences
c_counter = 0;
}
}
}
int[] out_array = list.stream().mapToInt(i->i).toArray(); //converts from arraylist to int[] as that's the format it must output
out_array = list.stream().filter(i->i != null).mapToInt(i->i).toArray();
return out_array;
}
static int[]递增(int[]xs){
ArrayList当前_数组=新ArrayList();
ArrayList=新建ArrayList();
int c_计数器=0;
for(int i=0;i xs[i]){
当前_array.add(xs[i]);
c_counter++;//跟踪添加了多少元素
}
}
else if((xs[i]>xs[i-1])){
如果(c_计数器==0){
当前_array.add(xs[i-1]);//确保也添加较小的数字
当前_array.add(xs[i]);
c_计数器=c_计数器+2;
}否则{
当前_array.add(xs[i]);
c_计数器++;
}
}否则{
if(current_array.size()>list.size()){//比较大小以找到最长的序列
list.clear();
for(int k=0;ki).toArray();//从arraylist转换为int[],因为这是它必须输出的格式
out_array=list.stream().filter(i->i!=null).mapToInt(i->i).toArray();
返回_数组;
}
理解代码的时间太长。请尝试以下方法:
List<Integer> test = new ArrayList<Integer>();
test.add(3);
test.add(2);
test.add(1);
test.add(2);
// test.add(4);
// test.add(6);
// test.add(7);
// test.add(8);
test.add(1);
test.add(2);
test.add(1000);
test.add(97777);
test.add(487);
test.add(8274);
test.add(972837);
List<Integer> output = new ArrayList<Integer>();
List<Integer> temp = new ArrayList<Integer>();
for(int i = 0; i < test.size(); i++) {
int current = test.get(i);
int next = Integer.MIN_VALUE;
if(i + 1 < test.size()) next = test.get(i + 1);
if(current > next) {
if(output.size() <= temp.size()) {
temp.add(current);
output = new ArrayList<Integer>(temp);
}
temp.clear();
} else {
temp.add(current);
}
}
output.forEach(i -> System.out.print(i + ", "));
List test=newarraylist();
测试。添加(3);
测试。添加(2);
测试。添加(1);
测试。添加(2);
//试验。添加(4);
//试验。添加(6);
//试验。添加(7);
//试验。添加(8);
测试。添加(1);
测试。添加(2);
测试。添加(1000);
测试。添加(97777);
测试。添加(487);
测试。添加(8274);
测试。添加(972837);
列表输出=新的ArrayList();
List temp=new ArrayList();
对于(int i=0;i下一个){
if(output.size()System.out.print(i+“,”));
如果您想要最长的递减输出,请将
int next=Integer.MAX_VALUE;
和If(current>next)
更改为If(current
理解您的代码需要很长时间。请尝试以下方法:
List<Integer> test = new ArrayList<Integer>();
test.add(3);
test.add(2);
test.add(1);
test.add(2);
// test.add(4);
// test.add(6);
// test.add(7);
// test.add(8);
test.add(1);
test.add(2);
test.add(1000);
test.add(97777);
test.add(487);
test.add(8274);
test.add(972837);
List<Integer> output = new ArrayList<Integer>();
List<Integer> temp = new ArrayList<Integer>();
for(int i = 0; i < test.size(); i++) {
int current = test.get(i);
int next = Integer.MIN_VALUE;
if(i + 1 < test.size()) next = test.get(i + 1);
if(current > next) {
if(output.size() <= temp.size()) {
temp.add(current);
output = new ArrayList<Integer>(temp);
}
temp.clear();
} else {
temp.add(current);
}
}
output.forEach(i -> System.out.print(i + ", "));
List test=newarraylist();
测试。添加(3);
测试。添加(2);
测试。添加(1);
测试。添加(2);
//试验。添加(4);
//试验。添加(6);
//试验。添加(7);
//试验。添加(8);
测试。添加(1);
测试。添加(2);
测试。添加(1000);
测试。添加(97777);
测试。添加(487);
测试。添加(8274);
测试。添加(972837);
列表输出=新的ArrayList();
List temp=new ArrayList();
对于(int i=0;i下一个){
if(output.size()System.out.print(i+“,”));
如果您想要最长的递减输出,请将int next=Integer.MAX_VALUE;
和If(current>next)
更改为If(current
公共静态int[]递增(int[]xs){
int start=0;
int end=0;
内部温度=0;
for(int i=0;i
公共静态int[]递增(int[]xs){
int start=0;
int end=0;
内部温度=0;
for(int i=0;i
你应该重新考虑你的策略。在任何时候,你只需要存储一个开始位置和当前最长序列的长度。当你在序列中前进时,一旦递增序列结束,就将其与之前的最长序列(初始化为0[位置],0[长度])进行比较.如果当前序列较长,则将其存储为最长序列的详细信息…我已理解您的意思,并删除了“c_计数器”变量。这次我使用以下输入测试代码:1,2,1,2,3,1,2,3,4,5。这次它输出“1,2,3”因此,它似乎能够比较序列的长度,只是不能比较最后一个序列。知道为什么吗?提示:正如你和其他人已经指出的那样:你的代码过于复杂。你想了解“单层抽象”原则。也许还有“干净的代码”罗伯特·马丁也是。你应该重新考虑你的策略。在任何时候,你只需要存储一个开始位置和当前最长序列的长度。当你在序列中前进时,一旦递增序列结束,就将其与之前的最长序列(初始化为0[position],0[length])进行比较.如果当前序列较长,则将其存储为最长序列的详细信息…我已理解您的意思,并删除了“c_计数器”变量。这次我使用以下输入测试代码:1,2,1,2,3,1,2,3,4,5。这次它输出“1,2,3”因此,它似乎能够比较序列的长度,只是不能比较最后一个序列。知道为什么吗?提示:正如您和其他人已经指出的那样:您的代码过于复杂。您想了解“单层抽象”吗