diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 45d2a229..ceca8c87 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -237,6 +237,32 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { } ``` +Rust: + +```rust +impl Solution { + pub fn min_sub_array_len(target: i32, nums: Vec) -> i32 { + let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0); + let (mut sum, mut i) = (0, 0); + + for (pos, val) in nums.iter().enumerate() { + sum += val; + while sum >= target { + subLength = (pos - i + 1) as i32; + if result > subLength { + result = subLength; + } + sum -= nums[i]; + i += 1; + } + } + if result == i32::MAX { + return 0; + } + result + } +} +``` -----------------------