#include<iostream> using namespace std; void GetLong(int *arry, const int &len, int &start, int &end); void main() { int start = 0; int end = 0; int arry[] = {1,2,4,3,4,7,5,6,5,7,9,10}; GetLong(arry, sizeof(arry)/sizeof(arry[0]), start, end); while(start<=end) { cout<<arry[start]<<" "; start++; } cout<<endl; } void GetLong(int *arry, const int &len, int &start, int &end) { if(arry == NULL || len<=0) return; int i = 1; int *tempspace = new int[len]; tempspace[0] = 1; while(i<len) { if(arry[i] == (arry[i-1]+1)) tempspace[i] = tempspace[i-1]+1; else tempspace[i] = 1; i++; } int max = 0; int maxpos = 0; i=0; while(i<len) { if(tempspace[i]>max) { max = tempspace[i]; maxpos = i; } i++; } end = maxpos; start = end-max+1; } 这是第二题最长连续子数组的答案,时间复杂度为O(n),空间复杂度为O(n)
点赞 评论

相关推荐

轻絵梨花泪沾衣:南泵,大少爷驾到通通闪开
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务