问题03 连续子数组的最大和

``````int maxSubArray(int A[], int n) {

int curSum = 0;
int maxSum = A[0];

for(int j = 0; j < n; j++) {
if(curSum >= 0)  {
curSum += A[j];
}
else {
curSum = A[j];
}
if(curSum > maxSum) {
maxSum = curSum;
}
}
return maxSum;
}``````

问题05 把数组排成最小的数

``````#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

bool cmp(int a, int b)
{
string ab = to_string(a) + to_string(b);
string ba = to_string(b) + to_string(a);
return ab < ba;
}
class Solution
{
public:
string PrintMinNumber(vector<int> n)
{
sort(n.begin(), n.end(), cmp);

string res = "";
for (int i : n)
res += to_string(i);
return res;
}
};``````

问题08 数组中的逆序对

``````#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

class Solution
{
public:
int MMD = 1000000007;
int mergeSort(vector<int>& a, vector<int>& tmp, int beg, int end)
{
if (beg >= end)
return 0;
else
{
int mid = (end - beg) / 2 + beg;
long long left = (mergeSort(a, tmp, beg, mid)) % MMD;
long long right = (mergeSort(a, tmp, mid + 1, end)) % MMD;
long long count = 0;
int i = beg, j = mid + 1, k = beg;
while (i <= mid && j <= end)
{
if (a[i] <= a[j])
tmp[k++] = a[i++];
else
{
tmp[k++] = a[j++];
count += (mid - i + 1);
}
}

while (i <= mid)
tmp[k++] = a[i++];
while (j <= end)
tmp[k++] = a[j++];

for (int i = beg; i <= end; i++)
a[i] = tmp[i];

long long res = (left + right + count) % MMD;
return (int)res;

}
}

int InversePairs(vector<int> a)
{
vector<int> tmp(a);
return mergeSort(a, tmp, 0, a.size() - 1);
}
};``````