Skip to content

Commit bc6d2da

Browse files
committed
commit solution 53
1 parent 608604d commit bc6d2da

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed

index-tags.md

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
1414
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> ||
1515
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
16+
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
1617
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
1718
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
1819
| [80](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii) | [删除排序数组中的重复项 ii](/solution/1-99/0080.remove-duplicates-from-sorted-array-ii/) | `数组`,`双指针` | <font color=blue>中等</font> ||
@@ -188,10 +189,15 @@
188189

189190
#### **动态规划**
190191

192+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
193+
| --- | --- | --- | --- | --- |
194+
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
195+
191196
#### **分治算法**
192197

193198
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
194199
| --- | --- | --- | --- | --- |
200+
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
195201
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
196202

197203
#### **回溯算法**

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
2020
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |
22-
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> |
22+
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
2323
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> ||
2424
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
2525
| [67](https://leetcode-cn.com/problems/add-binary) | [二进制求和](/solution/1-99/0067.add-binary/) | `数学`,`字符串` | <font color=green>简单</font> |

solution/1-99/0053.maximum-subarray/README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [53. 最大子序和](https://leetcode-cn.com/problems/maximum-subarray)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个整数数组 <code>nums</code>&nbsp;,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p>
66

77
<p><strong>示例:</strong></p>
@@ -22,12 +22,29 @@
2222

2323
### 具体解法
2424

25-
<!-- tabs:start -->
25+
2626

2727
#### **Golang**
2828
```go
29-
29+
func maxSubArray(nums []int) int {
30+
l := len(nums)
31+
if l == 0 {
32+
return 0
33+
}
34+
max := nums[l-1]
35+
sum := nums[l-1]
36+
for i := l - 2; i >= 0; i-- {
37+
if sum > 0 {
38+
sum += nums[i]
39+
} else {
40+
sum = nums[i]
41+
}
42+
if sum > max {
43+
max = sum
44+
}
45+
}
46+
return max
47+
}
3048
```
3149

32-
<!-- tabs:end -->
3350

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=53 lang=golang
5+
*
6+
* [53] 最大子序和
7+
*/
8+
9+
// @lc code=start
10+
func maxSubArray(nums []int) int {
11+
l := len(nums)
12+
if l == 0 {
13+
return 0
14+
}
15+
max := nums[l-1]
16+
sum := nums[l-1]
17+
for i := l - 2; i >= 0; i-- {
18+
if sum > 0 {
19+
sum += nums[i]
20+
} else {
21+
sum = nums[i]
22+
}
23+
if sum > max {
24+
max = sum
25+
}
26+
}
27+
return max
28+
}
29+
30+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMaxSubArray(t *testing.T) {
8+
var nums []int
9+
var ret int
10+
11+
ret = 10
12+
nums = []int{1, 3, 6, -5, 4, -3, 2}
13+
if ret != maxSubArray(nums) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
17+
ret = 0
18+
nums = []int{}
19+
if ret != maxSubArray(nums) {
20+
t.Fatalf("case fails %v\n", ret)
21+
}
22+
}

solution/1-99/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
- [50. pow(x, n) ✅](solution/1-99/0050.powx-c-n/)
5959
- [51. n皇后](solution/1-99/0051.n-queens/)
6060
- [52. n皇后 ii](solution/1-99/0052.n-queens-ii/)
61-
- [53. 最大子序和](solution/1-99/0053.maximum-subarray/)
61+
- [53. 最大子序和](solution/1-99/0053.maximum-subarray/)
6262
- [54. 螺旋矩阵](solution/1-99/0054.spiral-matrix/)
6363
- [55. 跳跃游戏](solution/1-99/0055.jump-game/)
6464
- [56. 合并区间](solution/1-99/0056.merge-intervals/)

0 commit comments

Comments
 (0)