From a934f2d2d94d724c8ef29609ce23e72c47cb86cf Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 20 May 2025 16:17:35 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0581 No.0581.Shortest Unsorted Continuous Subarray --- .../README.md | 71 ++++++++++++------- .../README_EN.md | 71 ++++++++++++------- .../Solution.rs | 36 ++++------ .../Solution2.rs | 30 ++++++++ 4 files changed, 136 insertions(+), 72 deletions(-) create mode 100644 solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution2.rs diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README.md b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README.md index 6d3765e61f6cb..2cf6f98195019 100644 --- a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README.md +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README.md @@ -173,32 +173,20 @@ function findUnsortedSubarray(nums: number[]): number { ```rust impl Solution { pub fn find_unsorted_subarray(nums: Vec) -> i32 { - let inf = 1 << 30; - let n = nums.len(); - let mut l = -1; - let mut r = -1; - let mut mi = inf; - let mut mx = -inf; - - for i in 0..n { - if mx > nums[i] { - r = i as i32; - } else { - mx = nums[i]; - } - - if mi < nums[n - i - 1] { - l = (n - i - 1) as i32; - } else { - mi = nums[n - i - 1]; - } + let mut arr = nums.clone(); + arr.sort(); + let mut l = 0usize; + while l < nums.len() && nums[l] == arr[l] { + l += 1; } - - if r == -1 { - 0 - } else { - r - l + 1 + if l == nums.len() { + return 0; + } + let mut r = nums.len() - 1; + while r > l && nums[r] == arr[r] { + r -= 1; } + (r - l + 1) as i32 } } ``` @@ -340,6 +328,41 @@ function findUnsortedSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_unsorted_subarray(nums: Vec) -> i32 { + let inf = 1 << 30; + let n = nums.len(); + let mut l = -1; + let mut r = -1; + let mut mi = inf; + let mut mx = -inf; + + for i in 0..n { + if mx > nums[i] { + r = i as i32; + } else { + mx = nums[i]; + } + + if mi < nums[n - i - 1] { + l = (n - i - 1) as i32; + } else { + mi = nums[n - i - 1]; + } + } + + if r == -1 { + 0 + } else { + r - l + 1 + } + } +} +``` + diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md index 75d9d3a8fd1b0..d05b0d8b85aed 100644 --- a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md @@ -166,32 +166,20 @@ function findUnsortedSubarray(nums: number[]): number { ```rust impl Solution { pub fn find_unsorted_subarray(nums: Vec) -> i32 { - let inf = 1 << 30; - let n = nums.len(); - let mut l = -1; - let mut r = -1; - let mut mi = inf; - let mut mx = -inf; - - for i in 0..n { - if mx > nums[i] { - r = i as i32; - } else { - mx = nums[i]; - } - - if mi < nums[n - i - 1] { - l = (n - i - 1) as i32; - } else { - mi = nums[n - i - 1]; - } + let mut arr = nums.clone(); + arr.sort(); + let mut l = 0usize; + while l < nums.len() && nums[l] == arr[l] { + l += 1; } - - if r == -1 { - 0 - } else { - r - l + 1 + if l == nums.len() { + return 0; + } + let mut r = nums.len() - 1; + while r > l && nums[r] == arr[r] { + r -= 1; } + (r - l + 1) as i32 } } ``` @@ -333,6 +321,41 @@ function findUnsortedSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_unsorted_subarray(nums: Vec) -> i32 { + let inf = 1 << 30; + let n = nums.len(); + let mut l = -1; + let mut r = -1; + let mut mi = inf; + let mut mx = -inf; + + for i in 0..n { + if mx > nums[i] { + r = i as i32; + } else { + mx = nums[i]; + } + + if mi < nums[n - i - 1] { + l = (n - i - 1) as i32; + } else { + mi = nums[n - i - 1]; + } + } + + if r == -1 { + 0 + } else { + r - l + 1 + } + } +} +``` + diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs index bc3aa08117ca2..8e6ec9ada8767 100644 --- a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs @@ -1,30 +1,18 @@ impl Solution { pub fn find_unsorted_subarray(nums: Vec) -> i32 { - let inf = 1 << 30; - let n = nums.len(); - let mut l = -1; - let mut r = -1; - let mut mi = inf; - let mut mx = -inf; - - for i in 0..n { - if mx > nums[i] { - r = i as i32; - } else { - mx = nums[i]; - } - - if mi < nums[n - i - 1] { - l = (n - i - 1) as i32; - } else { - mi = nums[n - i - 1]; - } + let mut arr = nums.clone(); + arr.sort(); + let mut l = 0usize; + while l < nums.len() && nums[l] == arr[l] { + l += 1; } - - if r == -1 { - 0 - } else { - r - l + 1 + if l == nums.len() { + return 0; } + let mut r = nums.len() - 1; + while r > l && nums[r] == arr[r] { + r -= 1; + } + (r - l + 1) as i32 } } diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution2.rs b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution2.rs new file mode 100644 index 0000000000000..bc3aa08117ca2 --- /dev/null +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution2.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn find_unsorted_subarray(nums: Vec) -> i32 { + let inf = 1 << 30; + let n = nums.len(); + let mut l = -1; + let mut r = -1; + let mut mi = inf; + let mut mx = -inf; + + for i in 0..n { + if mx > nums[i] { + r = i as i32; + } else { + mx = nums[i]; + } + + if mi < nums[n - i - 1] { + l = (n - i - 1) as i32; + } else { + mi = nums[n - i - 1]; + } + } + + if r == -1 { + 0 + } else { + r - l + 1 + } + } +}