題目描述:
給定一個整數數組,找到其中最長的遞增子數組(注意不是子序列,即結果是連續的)。如果有多個一樣長的子數組,那麼都需要查找出來。語言不限。
輸入描述:
輸入爲標準輸入流。輸入數組元素以空格分隔。
輸出描述:
輸出爲標準輸出流。輸出數組元素以空格分隔,輸出最後不帶額外空格,多個輸出以換行分隔。
示例1
輸入
1 7 2 4 5 3
輸出
2 4 5
解法:
使用Map來存儲相關的值,保證了多個。
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Datatest {
public static void main(String args[])
{ int [] nums= {1,2,3,1,2,3,4,2,3,5,6};
Max_ASC(nums);
}
public static void Max_ASC(int[] nums)
{
int index=0,maxRes=0,numsSize=nums.length;
ArrayList<Integer> arrayList=new ArrayList<>();
Map<Integer, ArrayList<Integer>>aMap = new HashMap<Integer, ArrayList<Integer>>();
while(index<numsSize)
{
int teIndex=index+1;
while(teIndex<numsSize &&nums[teIndex]>nums[teIndex-1])
{
teIndex++;
}
if(teIndex-index>=maxRes)
{ if(teIndex-index!=maxRes)
aMap.clear();
arrayList=new ArrayList<>();
for(int i=index;i<teIndex;i++)
arrayList.add(nums[i]);
maxRes=teIndex-index;
for(int i=0;i<arrayList.size();i++)
System.out.println(arrayList.get(i));
aMap.put((Integer)index,arrayList);
}
index=teIndex;
}
//輸出相關的數據
System.out.println(aMap.size());
for(ArrayList<Integer> value : aMap.values()){
System.out.println(value);
}
}
}
希望會有二面,跪求!!!