我正在尝试在 C#中开发最大子数组问题
我的代码是
try
{
int[] Values = { 9, 1, 4, 15, -5, -41, -8, 78, 145, 14 };//Will be executed once '1'
int StartIndex = 0;//Will be executed once '1'
double Sum = 0;//Will be executed once '1'
double Temp = 0;//Will be executed once '1'
double Max = 0;//Will be executed once '1'
do
{
for (int i = 0; i < Values.Length; i++)//1+(N+1)+N
{
Sum = Values[StartIndex];
if (StartIndex < i)
{
for (int j = StartIndex+1; j <= i; j++)
{
Sum += Values[j];
}
if (Sum > Temp)
{
Max = Sum;
Temp = Sum;
}
}
}
StartIndex++;
} while (StartIndex<Values.Length);
MessageBox.Show("The Max Value is " + Max);
}
catch { }
我想知道这是否是解决该算法的最佳方法,因为我试图最小化时间复杂度
谢谢大家的时间