-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path面试题31之连续子数组的最大和_GreatestSumOfSubarrays.cpp
88 lines (71 loc) · 1.72 KB
/
面试题31之连续子数组的最大和_GreatestSumOfSubarrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// GreatestSumOfSubarrays.cpp : Defines the entry point for the console application.
//
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdafx.h"
bool g_InvalidInput = false;
int FindGreatestSumOfSubArray(int *pData, int nLength)
{
if((pData == NULL) || (nLength <= 0))
{
g_InvalidInput = true;
return 0;
}
g_InvalidInput = false;
int nCurSum = 0;
int nGreatestSum = 0x80000000;
for(int i = 0; i < nLength; ++i)
{
if(nCurSum <= 0)
nCurSum = pData[i];
else
nCurSum += pData[i];
if(nCurSum > nGreatestSum)
nGreatestSum = nCurSum;
}
return nGreatestSum;
}
// ====================测试代码====================
void Test(char* testName, int* pData, int nLength, int expected, bool expectedFlag)
{
if(testName != NULL)
printf("%s begins: \n", testName);
int result = FindGreatestSumOfSubArray(pData, nLength);
if(result == expected && expectedFlag == g_InvalidInput)
printf("Passed.\n");
else
printf("Failed.\n");
}
// 1, -2, 3, 10, -4, 7, 2, -5
void Test1()
{
int data[] = {1, -2, 3, 10, -4, 7, 2, -5};
Test("Test1", data, sizeof(data) / sizeof(int), 18, false);
}
// 所有数字都是负数
// -2, -8, -1, -5, -9
void Test2()
{
int data[] = {-2, -8, -1, -5, -9};
Test("Test2", data, sizeof(data) / sizeof(int), -1, false);
}
// 所有数字都是正数
// 2, 8, 1, 5, 9
void Test3()
{
int data[] = {2, 8, 1, 5, 9};
Test("Test3", data, sizeof(data) / sizeof(int), 25, false);
}
// 无效输入
void Test4()
{
Test("Test4", NULL, 0, 0, true);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test1();
Test2();
Test3();
Test4();
return 0;
}