FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SwordForOfferJava/problem31/FindGreatestSumOfSubArray.java at master · lsp12138/SwordForOfferJava · GitHub
lsp12138
/
SwordForOfferJava
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
9
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SwordForOfferJava
/
problem31
/
FindGreatestSumOfSubArray.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (33 loc) · 1.21 KB
Breadcrumbs
SwordForOfferJava
/
problem31
/
FindGreatestSumOfSubArray.java
Copy path
File metadata and controls
33 lines (33 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
problem31
;
/*
* 面试题31:连续子数组的最大和
* 输入一个整数型数组,有正有负,数组中的一个或多个数字组成子数组,求所有子数组的和的最大值,要求时间复杂度为O(n)。
* 例如输入的数组为{1,-2,3,10,-4,7,2,-5},和最大的子数组为{3,10,-4,7,2},因此输出为该子数组的和18。
* 思路:从头到尾逐个累加数组中的数字作为当前和,初始化为0,如果当前和小于等于0,那么和下一个要累加的数相加后肯定比这个数小,所以当前和记为这个数才是最大的,之前的抛弃。如果当前和大于0,更新当前和为它加上下一个数。如果这个当前和比最大和还大,就更新最大和。
*/
public
class
FindGreatestSumOfSubArray
{
public
static
Integer
findGreatestSumOfSubArray
(
int
[]
arr
){
if
(
arr
==
null
||
arr
.
length
<=
0
){
System
.
out
.
println
(
"Invalid Input"
);
return
0
;
}
int
curSum
=
0
;
//当前和
int
greatestSum
=
Integer
.
MIN_VALUE
;
//最大和,初始值设为int的最小值,适用于数组全是负数的情况。
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++){
if
(
curSum
<=
0
){
curSum
=
arr
[
i
];
}
else
{
curSum
+=
arr
[
i
];
}
if
(
curSum
>
greatestSum
){
greatestSum
=
curSum
;
}
}
return
greatestSum
;
}
public
static
void
main
(
String
[]
args
) {
int
[]
arr
= {
1
,-
2
,
3
,
10
,-
4
,
7
,
2
,-
5
};
int
sum
=
findGreatestSumOfSubArray
(
arr
);
System
.
out
.
println
(
sum
);
}
}
Back
|
FazBrowse Home
|
New Git URL