练习 修改文档

This commit is contained in:
游由 2021-07-20 22:40:00 +08:00
parent 466c6e1702
commit 7ff51b11ed
5 changed files with 100 additions and 44 deletions

View File

@ -1,9 +1,3 @@
# 路漫漫其修远兮,吾将上下而求索
## 路漫漫其修远兮,吾将上下而求索
# 工欲善其事,必先利其器
# 刷题中...
# 分类
## 位运算
## 工欲善其事,必先利其器

View File

@ -68,3 +68,5 @@ mod q0704;
mod q0035;
mod q0152;
mod q0977;
mod q0283;
mod q0344;

View File

@ -0,0 +1,46 @@
use crate::Solution;
impl Solution {
/// 283.移动零
///
/// [原题链接](https://leetcode-cn.com/problems/move-zeroes/)
///
/// 2021-07-20 22:21:24
///
/// 给定一个数组 `nums`,编写一个函数将所有 `0` 移动到数组的末尾,同时保持非零元素的相对顺序。
///
/// + 示例:
/// - 输入: `[0,1,0,3,12]`
/// - 输出: `[1,3,12,0,0]`
/// + 说明:
/// - 必须在原数组上操作,不能拷贝额外的数组。
/// - 尽量减少操作次数。
///
/// Related Topics 数组 双指针
///
/// 👍 1131 👎 0
pub fn move_zeroes(nums: &mut Vec<i32>) {
let (mut l, mut r) = (0usize, 0usize);
while r < nums.len() {
if nums[r] != 0 {
nums.swap(l, r);
l += 1;
}
r += 1;
}
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q0283() {
let mut nums = vec![0, 1, 0, 3, 12];
Solution::move_zeroes(&mut nums);
assert_eq!(nums, vec![1, 3, 12, 0, 0]);
}
}

View File

@ -0,0 +1,50 @@
use crate::Solution;
impl Solution {
/// 344.反转字符串
///
/// [原题链接](https://leetcode-cn.com/problems/reverse-string/)
///
/// 2021-07-20 22:32:53
///
/// 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 `char[]` 的形式给出。
///
/// 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 `O(1)` 的额外空间解决这一问题。
///
/// 你可以假设数组中的所有字符都是 `ASCII` 码表中的可打印字符。
///
/// + 示例 1
/// - 输入:`["h","e","l","l","o"]`
/// - 输出:`["o","l","l","e","h"]`
/// + 示例 2
/// - 输入:`["H","a","n","n","a","h"]`
/// - 输出:`["h","a","n","n","a","H"]`
///
/// Related Topics 递归 双指针 字符串
///
/// 👍 425 👎 0
pub fn reverse_string(s: &mut Vec<char>) {
let n = s.len();
let mut idx = 0;
for i in 0..n / 2 {
idx = n - i - 1;
s[i] = (s[i] as u8 ^ s[idx] as u8) as char;
s[idx] = (s[i] as u8 ^ s[idx] as u8) as char;
s[i] = (s[i] as u8 ^ s[idx] as u8) as char;
}
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q0344() {
let mut s = vec!['h', 'e', 'l', 'l', 'o'];
Solution::reverse_string(&mut s);
assert_eq!(s, vec!['o', 'l', 'l', 'e', 'h']);
}
}

View File

@ -1037,20 +1037,6 @@ impl Solution {
}
}
/// 344.反转字符串
///
/// [原题链接](https://leetcode-cn.com/problems/reverse-string/)
pub fn reverse_string(s: &mut Vec<char>) {
let n = s.len();
let mut idx = 0;
for i in 0..n / 2 {
idx = n - i - 1;
s[i] = (s[i] as u8 ^ s[idx] as u8) as char;
s[idx] = (s[i] as u8 ^ s[idx] as u8) as char;
s[i] = (s[i] as u8 ^ s[idx] as u8) as char;
}
}
/// 125.验证回文串
///
/// [原题链接](https://leetcode-cn.com/problems/valid-palindrome/)
@ -2484,20 +2470,6 @@ impl Solution {
(stones.len() - count) as i32
}
/// 283.移动零
///
/// [原题链接](https://leetcode-cn.com/problems/move-zeroes/)
pub fn move_zeroes(nums: &mut Vec<i32>) {
let (mut left, mut right) = (0usize, 0usize);
while right < nums.len() {
if nums[right] != 0 {
nums.swap(left, right);
left += 1;
}
right += 1;
}
}
/// 4.寻找两个正序数组的中位数
///
/// [原题链接](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/)
@ -4318,14 +4290,6 @@ mod tests {
assert_eq!(i, 5);
}
#[test]
fn test_move_zeroes() {
let mut nums = vec![0, 1, 0, 3, 12];
Solution::move_zeroes(&mut nums);
//println!("{:?}", nums);
assert_eq!(nums, vec![1, 3, 12, 0, 0]);
}
#[test]
fn test_find_duplicate() {
let nums = vec![8, 7, 1, 10, 17, 15, 18, 11, 16, 9, 19, 12, 5, 14, 3, 4, 2, 13, 18, 18];