Skip to content

Commit 8061774

Browse files
committed
感觉leetcode里面有这道题目。。看错题目,闹出笑话,product是乘积!!
1 parent b7d3488 commit 8061774

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Sorted-Array/MaxSum

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 把题目读错了,人家求连续的(contiguous)的乘积,我TMD的算成了求和,一个array里面subarray和最大的,O(n)的思路如下:
2+
3+
class Solution {
4+
public:
5+
int maxProduct(int A[], int n) {
6+
int sum = 0;
7+
int curMax = 0, temMax = 0;
8+
bool flag = true;
9+
for(int i = 0;i < n;i++)
10+
{
11+
if(flag)
12+
{
13+
if(temMax + A[i] >= temMax)
14+
temMax = temMax + A[i];
15+
else
16+
{
17+
flag = false;
18+
curMax = max(temMax,curMax);
19+
}
20+
}
21+
else
22+
{
23+
flag = true;
24+
temMax = A[i];
25+
}
26+
}
27+
return curMax;
28+
29+
}
30+
};

0 commit comments

Comments
 (0)