Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f244be0

Browse files
author
guangsheng.li01
committed
Solved 0053
1 parent de3707c commit f244be0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

‎src/a0053_maximum_subarray.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* [0053] maximum-subarray
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
11+
let mut sum = 0;
12+
let mut res = nums[0];
13+
14+
for i in nums {
15+
if sum > 0 {
16+
sum += i;
17+
} else {
18+
sum = i;
19+
}
20+
res = std::cmp::max(res, sum);
21+
}
22+
res
23+
}
24+
}
25+
26+
// solution impl ends here
27+
28+
// solution tests starts here
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
34+
#[test]
35+
fn test_case0() {
36+
assert_eq!(
37+
Solution::max_sub_array(vec![-2, 1, -3, 4, -1, 2, 1, -5, 4]),
38+
6
39+
);
40+
}
41+
}
42+
43+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod a0009_palindrome_number;
1010
mod a0026_remove_duplicates_from_sorted_array;
1111
mod a0027_remove_element;
1212
mod a0035_search_insert_position;
13+
mod a0053_maximum_subarray;
1314
mod a0145_binary_tree_postorder_traversal;
1415
mod a0172_factorial_trailing_zeroes;
1516
mod a0400_nth_digit;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /