forked from 617076674/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1.java
More file actions
20 lines (19 loc) · 583 Bytes
/
Solution1.java
File metadata and controls
20 lines (19 loc) · 583 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package question0053_maximum_subarray;
/**
* 时间复杂度是O(n ^ 2),其中n是nums数组的长度。空间复杂度是O(1)。
*
* 执行用时:182ms,击败5.11%。消耗内存:43.1MB,击败41.80%。
*/
public class Solution1 {
public int maxSubArray(int[] nums) {
int n = nums.length, result = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += nums[j];
result = Math.max(result, sum);
}
}
return result;
}
}