JZ-C-31

剑指offer第三十一题:连续子数组的最大和

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
//============================================================================
// Name        : JZ-C-31.cpp
// Author      : Laughing_Lz
// Version     :
// Copyright   : All Right Reserved
// Description : 连续子数组的最大和
//============================================================================
 
#include <iostream>
#include <stdio.h>
using namespace std;
 
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;//最小的整数。int 4个字节,最小数:-2^31:0x80000000,最大数:2^32-1:0x7fffffff。(均为补码)
    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 main(int argc, char** argv) {
    Test1();
    Test2();
    Test3();
    Test4();
 
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注