Skip to content

Commit 9deb732

Browse files
committedMay 3, 2021
feat: resolve running_sum_of_1d_array
1 parent 077c504 commit 9deb732

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
 

‎running_sum_of_1d_array/src/lib.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* [Running Sum of 1d Array](https://leetcode.com/explore/featured/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3730/)
3+
*/
4+
5+
pub struct Solution {}
6+
7+
impl Solution {
8+
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
9+
let mut n = nums;
10+
for i in 1..n.len() {
11+
n[i] += n[i - 1];
12+
}
13+
n
14+
}
15+
}
16+
17+
#[cfg(test)]
18+
mod test {
19+
use super::*;
20+
21+
#[test]
22+
fn test_example_1() {
23+
let input = vec![1, 2, 3, 4];
24+
let output = vec![1, 3, 6, 10];
25+
assert_eq!(Solution::running_sum(input), output);
26+
}
27+
28+
#[test]
29+
fn test_example_2() {
30+
let input = vec![1, 1, 1, 1, 1];
31+
let output = vec![1, 2, 3, 4, 5];
32+
assert_eq!(Solution::running_sum(input), output);
33+
}
34+
35+
#[test]
36+
fn test_example_3() {
37+
let input = vec![3, 1, 2, 10, 1];
38+
let output = vec![3, 4, 6, 16, 17];
39+
assert_eq!(Solution::running_sum(input), output);
40+
}
41+
}

‎running_sum_of_1d_array/src/main.rs

-3
This file was deleted.

0 commit comments

Comments
 (0)