mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #668 from andywang0607/209_rust
增加0209.长度最小的子数组Rust語言實現
This commit is contained in:
@ -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>) -> 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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user