Skip to content

Commit

Permalink
rust: Add 0977
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Apr 28, 2024
1 parent 12e4c1f commit 26ca2d1
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions src/leetcode/0088.merge-sorted-array/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>, m: i32, nums2: &mut Vec<i32>, 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();
}

Expand All @@ -18,10 +20,12 @@ pub fn merge1(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
pub fn merge2(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, 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 {
Expand All @@ -44,10 +48,11 @@ pub fn merge2(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, 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;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/leetcode/0234.palindrome-linked-list/index.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 0234. 回文链表 Palindrome Linked List

TODO(shaohua)
7 changes: 7 additions & 0 deletions src/leetcode/0977.squares-of-a-sorted-array/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0977-squares-of-a-sorted-array"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
5 changes: 5 additions & 0 deletions src/leetcode/0977.squares-of-a-sorted-array/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 0977. 有序数组的平方 Squares of a Sorted Array

[问题描述](../problems/0977.squares-of-a-sorted-array/content.html)

TODO(Shaohua):
107 changes: 107 additions & 0 deletions src/leetcode/0977.squares-of-a-sorted-array/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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<i32>) -> Vec<i32> {
let mut list = nums.iter().map(|n| n.pow(2)).collect::<Vec<_>>();
list.sort();
list
}

// 原地处理
pub fn sorted_squares2(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
for num in nums.iter_mut() {
*num = num.pow(2);
}
nums.sort();
nums
}

// 靠拢型双指针
pub fn sorted_squares3(nums: Vec<i32>) -> Vec<i32> {
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<i32>) -> Vec<i32> {
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<i32>) -> Vec<i32>;

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);
}
}
3 changes: 2 additions & 1 deletion src/leetcode/tags/two-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 26ca2d1

Please sign in to comment.