Skip to main content

Number of Sub-arrays With Odd Sum

数学题,要注意的是子数组是连续取的。
做完看题解发现就是个前缀和,但是我因为一开始没注意到子数组是连续的,最后写出来的逻辑稍微复杂了一点点。On & O1

impl Solution {    pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {        let mut j_total = 0;        let mut o_total = 0;        let mut res = 0;        let mut o_num = 0;        let MOD = 1000000007;        for i in arr {            if i % 2 == 1 {                res += j_total * o_num + o_total + o_num + 1;                res %= MOD;                let j_total_buf = j_total;                j_total = o_total + o_num + 1;                o_total = j_total_buf;                o_num = 0;            } else {                o_num += 1;            }        }        res + j_total * o_num    }}