Skip to content

Commit 1d4eed0

Browse files
author
chufan
committed
docs: 文档更新
1 parent 28e37da commit 1d4eed0

File tree

23 files changed

+169
-194
lines changed

23 files changed

+169
-194
lines changed

code/algorithm/find.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
*
3-
* @param target
4-
* @param array
5-
*/
61
function Find(target, array) {
72
if (!array) {
83
return false

code/algorithm/leftRotateString.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* @param str
3-
* @param n
4-
* @returns {*}
5-
* @constructor
6-
*/
71
function LeftRotateString(str, n) {
82
if (!str) {
93
return str
File renamed without changes.

code/algorithm/剑指/JZ30_FindGreatestSumOfSubArray.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

code/algorithm/剑指/动态规划/findGreatestSumOfSubArray.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* @returns {number|*}
66
* @constructor
77
*/
8-
function FindGreatestSumOfSubArray(array) {
8+
9+
function FindGreatestSumOfSubArray01(array) {
910
// 标记指针
1011
let index = 0
1112
// 子串的累计结果
@@ -18,9 +19,37 @@ function FindGreatestSumOfSubArray(array) {
1819
sum += array[index]
1920
max = sum > max ? sum : max
2021

21-
// 注意,重新计数,累计求和,从0开始
22+
// 注意,重新计数
2223
sum = sum > 0 ? sum : 0
2324
index++
2425
}
2526
return max
2627
}
28+
29+
function FindGreatestSumOfSubArray(array) {
30+
// 首位指针
31+
let i = 0
32+
// 从第一个元素开始,假设最大
33+
let max = array[0]
34+
let sum = 0
35+
36+
while (i < array.length) {
37+
// 替换
38+
39+
// 和下一个元素求和
40+
sum += array[i]
41+
// 获取最大值
42+
max = sum > max ? sum : max
43+
44+
// 小于sum值,则说明累加和变小了,下一个模块重新计数
45+
sum = sum < 0 ? 0 : sum
46+
47+
// 标记指针后移
48+
i++
49+
}
50+
51+
return max
52+
}
53+
console.log(FindGreatestSumOfSubArray01([1, -2, 3, 10, -4, 7, 2, -5]))
54+
console.log(FindGreatestSumOfSubArray([-2, -8, -1, -5, -9]))
55+

0 commit comments

Comments
 (0)