-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cf0469
commit 39c268b
Showing
11 changed files
with
62 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 连续数列 | ||
|
||
> 难度:简单 | ||
> | ||
> https://leetcode.cn/problems/contiguous-sequence-lcci/ | ||
## 题目 | ||
|
||
给定一个整数数组,找出总和最大的连续数列,并返回总和。 | ||
|
||
### 示例: | ||
``` | ||
输入: [-2,1,-3,4,-1,2,1,-5,4] | ||
输出: 6 | ||
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { maxSubArray } from '.' | ||
|
||
describe('连续数列', () => { | ||
describe('', () => { | ||
testCase(maxSubArray) | ||
}) | ||
}) | ||
|
||
function testCase(fn: (nums: number[]) => number) { | ||
it.each([ | ||
// test cases | ||
[[-2, 1, -3, 4, -1, 2, 1, -5, 4], 6], | ||
])('示例%#', (nums, expected) => { | ||
expect(fn(nums)).toBe(expected) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function maxSubArray(nums: number[]): number { | ||
// [-2, 1, -3, 4, -1, 2, 1, -5, 4] | ||
let pre = 0; let maxAns = nums[0]; | ||
nums.forEach((x) => { | ||
pre = Math.max(pre + x, x); | ||
maxAns = Math.max(maxAns, pre); | ||
}); | ||
return maxAns; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.