【发布时间】:2013-03-15 07:24:56
【问题描述】:
我正在尝试用 C# 开发 Max Sub Array Problem
我的代码是
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 { }
我想知道这是否是解决此算法的最佳方法,因为我正在尝试最小化时间复杂度
感谢大家的宝贵时间
【问题讨论】:
-
最好在代码审查上发布您的问题 - codereview.stackexchange.com
-
我没有在你的代码中看到任何高级操作。你有什么理由把它放在 try 块中吗?