举例: array = {7,6,3,-10,2,-8,6}
和最大的子串为: 7 + 6 + 3 = 16
函数返回: 16
建议测试用例: {7,6,3,-10,200,-8,6} -> return 7 + 6 + 3 + (-10) + 200 = 206,
{7,6,3,-100,2,-8,6} -> return 2,
{100,-200,100,-10,100} -> return 100 + (-10) + 100 = 190.
其他情况可另行测试.
答案如下:
static int GetMaxString(int[] arr)
{
int curMax = 0;
int i = 0;
int j = 0;
int length = arr.Length;
int[] arrMax = new int[length];
for (; i < arr.Length; i++)
{
if (arr[i] < 0)
continue;
else
break;
}
if (i == arr.Length && arr[i - 1] < 0)
{
int tmpMax2 = arr[0];
for (int m = 0; m < arr.Length; m++)
{
if (tmpMax2 < arr[m])
{
tmpMax2 = arr[m];
}
}
return tmpMax2;
}
for (; i < arr.Length; i++)
{
if (arr[i] >= 0)
{
curMax += arr[i];
if (i == arr.Length - 1)
{
arrMax[j++] = curMax;
}
}
else
{
arrMax[j++] = curMax;
if (curMax + arr[i] >= 0)
{
curMax += arr[i];
}
else
{
curMax = 0;
}
}
}
int tmpMax = arrMax[0];
for (int k = 1; k < j; k++)
{
if (tmpMax < arrMax[k])
{
tmpMax = arrMax[k];
}
}
return tmpMax;
}
{
int curMax = 0;
int i = 0;
int j = 0;
int length = arr.Length;
int[] arrMax = new int[length];
for (; i < arr.Length; i++)
{
if (arr[i] < 0)
continue;
else
break;
}
if (i == arr.Length && arr[i - 1] < 0)
{
int tmpMax2 = arr[0];
for (int m = 0; m < arr.Length; m++)
{
if (tmpMax2 < arr[m])
{
tmpMax2 = arr[m];
}
}
return tmpMax2;
}
for (; i < arr.Length; i++)
{
if (arr[i] >= 0)
{
curMax += arr[i];
if (i == arr.Length - 1)
{
arrMax[j++] = curMax;
}
}
else
{
arrMax[j++] = curMax;
if (curMax + arr[i] >= 0)
{
curMax += arr[i];
}
else
{
curMax = 0;
}
}
}
int tmpMax = arrMax[0];
for (int k = 1; k < j; k++)
{
if (tmpMax < arrMax[k])
{
tmpMax = arrMax[k];
}
}
return tmpMax;
}
2.一个没有排序的链表,比如list={a,l,x,b,e,f,f,e,a,g,h,b,m},请去掉"重复项,并保留原顺序",以上链表去掉重复项后为newlist={a,l,x,b,e,f,g,h,m},请写出一个高效算法(时间比空间更重要)
提示:如果采用先排序,在去掉重复项的办法,复杂度为n+nlogn(采用快排),显然不是最优解,而且破还了"原顺序".还有其他解法.
答案如下:
static void Main(string[] args)
{
Link link1 = new Link('a');
Link link2 = new Link('l');
Link link3 = new Link('x');
Link link4 = new Link('b');
Link link5 = new Link('e');
Link link6 = new Link('f');
Link link7 = new Link('f');
Link link8 = new Link('e');
Link link9 = new Link('a');
Link link10 = new Link('g');
Link link11 = new Link('h');
Link link12 = new Link('b');
Link link13 = new Link('m');
link1.Next = link2;
link2.Next = link3;
link3.Next = link4;
link4.Next = link5;
link5.Next = link6;
link6.Next = link7;
link7.Next = link8;
link8.Next = link9;
link9.Next = link10;
link10.Next = link11;
link11.Next = link12;
link12.Next = link13;
link13.Next = null;
link1.Prev = null;
link2.Prev = link1;
link3.Prev = link2;
link4.Prev = link3;
link5.Prev = link4;
link6.Prev = link5;
link7.Prev = link6;
link8.Prev = link7;
link9.Prev = link8;
link10.Prev = link9;
link11.Prev = link10;
link12.Prev = link11;
link13.Prev = link12;
Link link = link1;
int[] tmp = new int[256];
Link tmpLink = link;
while (tmpLink != null)
{
tmp[Convert.ToInt32(tmpLink.Chr)]++;
tmpLink = tmpLink.Next;
}
Link tmpLink2 = link;
int count = 0;
while (tmpLink2 != null)
{
count++;
if (tmp[Convert.ToInt32(tmpLink2.Chr)] == 2)
{
tmp[Convert.ToInt32(tmpLink2.Chr)]++;
}
else if (tmp[Convert.ToInt32(tmpLink2.Chr)] == 3)
{
tmpLink2.Prev.Next = tmpLink2.Next;
}
tmpLink2 = tmpLink2.Next;
}
Link tmpLink3 = link;
while (tmpLink3 != null)
{
Console.WriteLine(tmpLink3.Chr.ToString());
tmpLink3 = tmpLink3.Next;
}
Console.Read();
}
{
Link link1 = new Link('a');
Link link2 = new Link('l');
Link link3 = new Link('x');
Link link4 = new Link('b');
Link link5 = new Link('e');
Link link6 = new Link('f');
Link link7 = new Link('f');
Link link8 = new Link('e');
Link link9 = new Link('a');
Link link10 = new Link('g');
Link link11 = new Link('h');
Link link12 = new Link('b');
Link link13 = new Link('m');
link1.Next = link2;
link2.Next = link3;
link3.Next = link4;
link4.Next = link5;
link5.Next = link6;
link6.Next = link7;
link7.Next = link8;
link8.Next = link9;
link9.Next = link10;
link10.Next = link11;
link11.Next = link12;
link12.Next = link13;
link13.Next = null;
link1.Prev = null;
link2.Prev = link1;
link3.Prev = link2;
link4.Prev = link3;
link5.Prev = link4;
link6.Prev = link5;
link7.Prev = link6;
link8.Prev = link7;
link9.Prev = link8;
link10.Prev = link9;
link11.Prev = link10;
link12.Prev = link11;
link13.Prev = link12;
Link link = link1;
int[] tmp = new int[256];
Link tmpLink = link;
while (tmpLink != null)
{
tmp[Convert.ToInt32(tmpLink.Chr)]++;
tmpLink = tmpLink.Next;
}
Link tmpLink2 = link;
int count = 0;
while (tmpLink2 != null)
{
count++;
if (tmp[Convert.ToInt32(tmpLink2.Chr)] == 2)
{
tmp[Convert.ToInt32(tmpLink2.Chr)]++;
}
else if (tmp[Convert.ToInt32(tmpLink2.Chr)] == 3)
{
tmpLink2.Prev.Next = tmpLink2.Next;
}
tmpLink2 = tmpLink2.Next;
}
Link tmpLink3 = link;
while (tmpLink3 != null)
{
Console.WriteLine(tmpLink3.Chr.ToString());
tmpLink3 = tmpLink3.Next;
}
Console.Read();
}