给定一个由N个整数构成的数组A和一个整数K,
从所有长度为K的A的连续子数组中返回最大的连续子数组。
如果两个数组中的第一个不相等元素在A中的值大于B中的值,则我们定义子数组A大于子数组B。
例如,A=[1,2,4,3],B=[1,2,3,5].
A大于B,因为A [2]> B [2]。
样例
例 1:
输入:
[1,4,3,2,5]
4
输出:
[4,3,2,5]
解释:
该数组有两个长度为4的连续子数组,分别为:
[1,4,3,2] 以及 [4,3,2,5].
所以最大的子数组为 [4,3,2,5].
例 2:
输入:
[7,1,2,7,9,2,3,1,2,5]
4
输出:
[9,2,3,1]
注意事项
1<=K<=N<=100
1<=A[i]<=1000
class Solution {
public:
/**
* @param A: the array
* @param K: the length
* @return: the largest subarray
*/
vector<int> largestSubarray(vector<int> &A, int K) {
// Write your code here.
vector<int> res;
if(A.size()<K) return res;
int max=0;
for (int i = 1; i <= A.size()-K; i++) {
for (int j = 0; j <= K; j++) {
/* code */
if(A[max+j]<A[i+j]){max=i;break;}
else if(A[max+j]>A[i+j])break;
else continue;
}
}
for (int i = max; i < max+K; i++) {
/* code */
res.push_back(A[i]);
}
return res;
}
};