我想開發C#如何在C#中實現MaxSubArray?
最大子排列問題我的代碼是
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 { }
我想知道這是否是解決這個算法我想最好的辦法儘量減少時間複雜度
謝謝大家的時間
這可能會更好張貼在代碼審查你的問題 - http://codereview.stackexchange.com/ – 2013-03-15 07:29:44
我不在你的代碼中看不到任何高級操作..你有什麼理由把它放在try塊中嗎? – Default 2013-03-15 07:32:25