From 26ca2d107344999ce69f356a511feec0df1aac0d Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 29 Apr 2024 00:21:38 +0800 Subject: [PATCH] rust: Add 0977 --- src/SUMMARY.md | 2 + .../0088.merge-sorted-array/src/main.rs | 17 ++- .../0234.palindrome-linked-list/index.md | 1 + .../0977.squares-of-a-sorted-array/Cargo.toml | 7 ++ .../0977.squares-of-a-sorted-array/index.md | 5 + .../src/main.rs | 107 ++++++++++++++++++ src/leetcode/tags/two-pointers.md | 3 +- 7 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 src/leetcode/0977.squares-of-a-sorted-array/Cargo.toml create mode 100644 src/leetcode/0977.squares-of-a-sorted-array/index.md create mode 100644 src/leetcode/0977.squares-of-a-sorted-array/src/main.rs diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 29766c47..57d76fc1 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -101,6 +101,7 @@ - [0015. 三数之和 3Sum](leetcode/0015.3sum/index.md) - [0026. 删除有序数组中的重复项 Remove Duplicates from Sorted Array](leetcode/0026.remove-duplicates-from-sorted-array/index.md) - [0067. 二进制求和 Add Binary](leetcode/0067.add-binary/index.md) + - [0088. 合并两个有序数组 Merge Sorted Array](leetcode/0088.merge-sorted-array/index.md) - [0101-0200](leetcode/orders/0101-0200.md) - [0125. 验证回文串 Valid Palindrome](leetcode/0125.valid-palindrome/index.md) - [0136. 只出现一次的数字 Single Number](leetcode/0136.single-number/index.md) @@ -125,6 +126,7 @@ - [0701-0800](leetcode/orders/0701-0800.md) - [0801-0900](leetcode/orders/0801-0900.md) - [0901-1000](leetcode/orders/0901-1000.md) + - [0977. 有序数组的平方 Squares of a Sorted Array](leetcode/0977.squares-of-a-sorted-array/index.md) - [1001-1100](leetcode/orders/1001-1100.md) - [1101-1200](leetcode/orders/1101-1200.md) - [1201-1300](leetcode/orders/1201-1300.md) diff --git a/src/leetcode/0088.merge-sorted-array/src/main.rs b/src/leetcode/0088.merge-sorted-array/src/main.rs index 35a1e74b..d9ed2787 100644 --- a/src/leetcode/0088.merge-sorted-array/src/main.rs +++ b/src/leetcode/0088.merge-sorted-array/src/main.rs @@ -2,14 +2,16 @@ // Use of this source is governed by General Public License that can be // found in the LICENSE file. +// Brute force // 直接调用数组的方法 pub fn merge1(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { let m = m as usize; let n = n as usize; assert_eq!(nums2.len(), n); - nums1.resize(m, 0); + for i in 0..n { + nums1[m + i] = nums2[i]; + } // 合并两个数组, 然后排序 - nums1.extend_from_slice(&nums2); nums1.sort(); } @@ -18,10 +20,12 @@ pub fn merge1(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { pub fn merge2(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { let m = m as usize; let n = n as usize; + + // 两个指针分别指向两个数组的尾部元素. let mut index1 = m - 1; let mut index2 = n - 1; // 这个指针指当前向合并后的数组的最低位. - let mut new_index = nums1.len() - 1; + let mut new_index = m + n - 1; // 从数组的尾部向头部合并, 即从高位开始, 直到有一个数组合并完成中止. while index1 >= 0 && index2 >= 0 { @@ -44,10 +48,11 @@ pub fn merge2(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { // 如果 nums2 还没有合并完, 就把剩下的元素都合并过来 while index2 >= 0 { nums1[new_index] = nums2[index2]; - index2 -= 1; - if new_index > 0 { - new_index -= 1; + if index2 == 0 { + break; } + index2 -= 1; + new_index -= 1; } } diff --git a/src/leetcode/0234.palindrome-linked-list/index.md b/src/leetcode/0234.palindrome-linked-list/index.md index 09b882cf..cfdbe7c1 100644 --- a/src/leetcode/0234.palindrome-linked-list/index.md +++ b/src/leetcode/0234.palindrome-linked-list/index.md @@ -1,2 +1,3 @@ # 0234. 回文链表 Palindrome Linked List +TODO(shaohua) \ No newline at end of file diff --git a/src/leetcode/0977.squares-of-a-sorted-array/Cargo.toml b/src/leetcode/0977.squares-of-a-sorted-array/Cargo.toml new file mode 100644 index 00000000..6408a7e4 --- /dev/null +++ b/src/leetcode/0977.squares-of-a-sorted-array/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0977-squares-of-a-sorted-array" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0977.squares-of-a-sorted-array/index.md b/src/leetcode/0977.squares-of-a-sorted-array/index.md new file mode 100644 index 00000000..a30bc6b1 --- /dev/null +++ b/src/leetcode/0977.squares-of-a-sorted-array/index.md @@ -0,0 +1,5 @@ +# 0977. 有序数组的平方 Squares of a Sorted Array + +[问题描述](../problems/0977.squares-of-a-sorted-array/content.html) + +TODO(Shaohua): \ No newline at end of file diff --git a/src/leetcode/0977.squares-of-a-sorted-array/src/main.rs b/src/leetcode/0977.squares-of-a-sorted-array/src/main.rs new file mode 100644 index 00000000..bb6d1f12 --- /dev/null +++ b/src/leetcode/0977.squares-of-a-sorted-array/src/main.rs @@ -0,0 +1,107 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +// Brute force +pub fn sorted_squares1(nums: Vec) -> Vec { + let mut list = nums.iter().map(|n| n.pow(2)).collect::>(); + list.sort(); + list +} + +// 原地处理 +pub fn sorted_squares2(nums: Vec) -> Vec { + let mut nums = nums; + for num in nums.iter_mut() { + *num = num.pow(2); + } + nums.sort(); + nums +} + +// 靠拢型双指针 +pub fn sorted_squares3(nums: Vec) -> Vec { + let mut left = 0; + let mut right = nums.len() - 1; + let mut out = Vec::with_capacity(nums.len()); + + // 遍历数组中所有的元素, 注意这里是 `<=` 符号. + while left <= right { + let left_val = nums[left].abs(); + let right_val = nums[right].abs(); + // 只移动其中的一个指针 + if left_val < right_val { + out.push(right_val * right_val); + right -= 1; + } else { + out.push(left_val * left_val); + left += 1; + } + } + // 以上是从大到小排列的, 现在进行反转 + out.into_iter().rev().collect() +} + +// 优化靠拢型双指针 +pub fn sorted_squares4(nums: Vec) -> Vec { + let mut left = 0; + let len = nums.len(); + let mut right = len - 1; + let mut out = vec![0; len]; + + // 遍历数组中所有的元素 + for i in (0..len).rev() { + let left_val = nums[left].abs(); + let right_val = nums[right].abs(); + // 只移动其中的一个指针 + if left_val < right_val { + out[i] = right_val.pow(2); + right -= 1; + } else { + out[i] = left_val.pow(2); + left += 1; + } + } + + out +} + +pub type SolutionFn = fn(Vec) -> Vec; + +fn check_solution(func: SolutionFn) { + let nums = vec![-4, -1, 0, 3, 10]; + assert_eq!(func(nums), vec![0, 1, 9, 16, 100]); + + let nums = vec![-7, -3, 2, 3, 11]; + assert_eq!(func(nums), vec![4, 9, 9, 49, 121]); + + let nums = vec![1]; + assert_eq!(func(nums), vec![1]); +} + +fn main() { + check_solution(sorted_squares1); + check_solution(sorted_squares2); + check_solution(sorted_squares3); + check_solution(sorted_squares4); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, sorted_squares1, sorted_squares2, sorted_squares3}; + + #[test] + fn test_sorted_squares1() { + check_solution(sorted_squares1); + } + + #[test] + fn test_sorted_squares2() { + check_solution(sorted_squares2); + } + + #[test] + fn test_sorted_squares3() { + check_solution(sorted_squares3); + } +} diff --git a/src/leetcode/tags/two-pointers.md b/src/leetcode/tags/two-pointers.md index 1233b6b9..5536887e 100644 --- a/src/leetcode/tags/two-pointers.md +++ b/src/leetcode/tags/two-pointers.md @@ -3,13 +3,14 @@ ## 容易 - [0026. 删除有序数组中的重复项 Remove Duplicates from Sorted Array](../0026.remove-duplicates-from-sorted-array/index.md) +- [0088. 合并两个有序数组 Merge Sorted Array](../0088.merge-sorted-array/index.md) - [0125. 验证回文串 Valid Palindrome](../0125.valid-palindrome/index.md) - [0349. 两个数组的交集 Intersection of Two Arrays](../0349.intersection-of-two-arrays/index.md) - [0350. 两个数组的交集 II Intersection of Two Arrays II](../0350.intersection-of-two-arrays-ii/index.md) - [0680. 验证回文串 II Valid Palindrome II](../0680.valid-palindrome-ii/index.md) +- [0977. 有序数组的平方 Squares of a Sorted Array](../0977.squares-of-a-sorted-array/index.md) - [2108. 找出数组中的第一个回文字符串 Find First Palindromic String in the Array](../2108.find-first-palindromic-string-in-the-array/index.md) -- [88. 合并两个有序数组](https://leetcode.com/problems/merge-sorted-array) - [160. 相交链表](https://leetcode.com/problems/intersection-of-two-linked-lists) - [234. 回文链表](https://leetcode.com/problems/palindrome-linked-list) - [925.长按键入](https://leetcode.com/problems/long-pressed-name)