如果这个问题真的很愚蠢,我想道歉 但我一直试图找到一种方法来迭代第二个元素
的基于for循环的范围int max_subarray(){
std::vector<int> vec;
vec.push_back( 10 );
vec.push_back( 20 );
vec.push_back( -20 );
vec.push_back( -20 );
vec.push_back( -20 );
std::vector<int> vec1;
vec1.push_back( 20 );
vec1.push_back( -20 );
vec1.push_back( -20 );
vec1.push_back( -20 );
int max_ending_here ,max_so_far;
max_ending_here = max_so_far = 10;
for(int x:vec1){
max_ending_here = std::max(x,max_ending_here + x);
std::cout<<"max_ending_here = "<<max_ending_here<<"\n";
max_so_far = std::max(max_so_far, max_ending_here);
std::cout<<"max_so_far = "<<max_so_far<<"\n";
}
std::cout<<"Max So Far = "<<max_so_far<<"\n";
return 1;
}
我通过复制除第一个
之外的所有元素创建了一个全新的数组我找到了一种方法,使用基于范围的循环
从第二个元素访问的python方式
for x in A[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
由于 Ť