From f69122eb052248c413ca9ffc3ff39a9282e91b00 Mon Sep 17 00:00:00 2001 From: ben1009 <liuhe1009@gmail.com> Date: Thu, 27 Mar 2025 11:27:28 +0800 Subject: [PATCH] chore: upgrade toolchain to nightly-2025-02-01 --- .github/workflows/check.yml | 2 +- .github/workflows/test.yml | 2 +- rust-toolchain | 2 +- src/problem/p0001_two_sum.rs | 8 ++--- src/problem/p0002_add_two_numbers.rs | 28 ++++++++--------- ..._substring_without_repeating_characters.rs | 18 +++++------ .../p0005_longest_palindromic_substring.rs | 30 ++++++++----------- src/problem/p0007_reverse_integer.rs | 16 ++++------ .../p0011_container_with_most_water.rs | 11 +++---- ...7_letter_combinations_of_a_phone_number.rs | 14 +++++---- src/problem/p0022_generate_parentheses.rs | 7 +++-- src/problem/p0056_merge_intervals.rs | 7 +++-- .../p0094_binary_tree_inorder_traversal.rs | 7 +++-- .../p0095_unique_binary_search_trees_ii.rs | 17 ++++++----- ...0114_flatten_binary_tree_to_linked_list.rs | 7 +++-- .../p0144_binary_tree_preorder_traversal.rs | 7 +++-- .../p0145_binary_tree_postorder_traversal.rs | 7 +++-- src/problem/p0503_next_greater_element_ii.rs | 14 +++++---- src/problem/p0739_daily_temperatures.rs | 14 +++++---- src/problem/p0912_sort_an_array.rs | 21 +++++++------ src/problem/p1054_distant_barcodes.rs | 7 +++-- src/problem/p2766_relocate_marbles.rs | 15 ++++++---- ...3142_check_if_grid_satisfies_conditions.rs | 14 +++++---- 23 files changed, 144 insertions(+), 131 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 71ef1016..c70a3774 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -12,7 +12,7 @@ concurrency: env: CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse - RUST_TOOLCHAIN: nightly-2024-12-01 + RUST_TOOLCHAIN: nightly-2025-02-01 name: Check jobs: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5763006b..605eb5a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ concurrency: env: CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse - RUST_TOOLCHAIN: nightly-2024-12-01 + RUST_TOOLCHAIN: nightly-2025-02-01 name: Test jobs: diff --git a/rust-toolchain b/rust-toolchain index a9c14142..bcc3629e 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,2 +1,2 @@ [toolchain] -channel = "nightly-2024-12-01" \ No newline at end of file +channel = "nightly-2025-02-01" \ No newline at end of file diff --git a/src/problem/p0001_two_sum.rs b/src/problem/p0001_two_sum.rs index 3e83aeaf..22eaf2cb 100644 --- a/src/problem/p0001_two_sum.rs +++ b/src/problem/p0001_two_sum.rs @@ -43,11 +43,11 @@ pub struct Solution {} impl Solution { pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { let mut map = HashMap::new(); - for (i, item) in nums.iter().enumerate() { - if map.contains_key(&(target - item)) { - return vec![map[&(target - item)], i as i32]; + for (i, &n) in nums.iter().enumerate() { + if let Some(&j) = map.get(&(target - n)) { + return vec![j, i as i32]; } - map.insert(item, i as i32); + map.insert(n, i as i32); } vec![] diff --git a/src/problem/p0002_add_two_numbers.rs b/src/problem/p0002_add_two_numbers.rs index 452b81f1..7da77a43 100644 --- a/src/problem/p0002_add_two_numbers.rs +++ b/src/problem/p0002_add_two_numbers.rs @@ -64,31 +64,29 @@ impl Solution { return l1; } + let mut root_pointer = ListNode::new(0); + let mut current = &mut root_pointer; + let mut carry = 0; let mut l1 = l1; let mut l2 = l2; - let mut ret = ListNode::new(0); - let mut current = &mut ret; - let mut carry = 0; - while l1.is_some() || l2.is_some() || carry != 0 { let mut v1 = 0; let mut v2 = 0; - if let Some(l) = l1 { - v1 = l.val; - l1 = l.next; + if let Some(mut n1) = l1 { + v1 = n1.val; + l1 = n1.next.take(); } - if let Some(l) = l2 { - v2 = l.val; - l2 = l.next; + if let Some(mut n2) = l2 { + v2 = n2.val; + l2 = n2.next.take(); } - let num = v1 + v2 + carry; - carry = num / 10; - let n = num % 10; - current.next = Some(Box::new(ListNode::new(n))); + let v = (v1 + v2 + carry) % 10; + carry = (v1 + v2 + carry) / 10; + current.next = Some(Box::new(ListNode::new(v))); current = current.next.as_mut().unwrap(); } - ret.next + root_pointer.next } } diff --git a/src/problem/p0003_longest_substring_without_repeating_characters.rs b/src/problem/p0003_longest_substring_without_repeating_characters.rs index 3fb33b46..7c843eb5 100644 --- a/src/problem/p0003_longest_substring_without_repeating_characters.rs +++ b/src/problem/p0003_longest_substring_without_repeating_characters.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, hash_map::Entry}; +use std::collections::HashMap; /// [3] Longest Substring Without Repeating Characters /// @@ -41,20 +41,20 @@ impl Solution { return s.len() as i32; } + let mut map = HashMap::new(); let mut ret = 0; let mut pre = 0; - let mut dic = HashMap::new(); - for (i, b) in s.as_bytes().iter().enumerate() { - match dic.entry(b) { - Entry::Occupied(o) => { - if pre <= *o.get() { + for (i, c) in s.bytes().enumerate() { + match map.entry(c) { + std::collections::hash_map::Entry::Occupied(o) => { + if o.get() >= &pre { pre = o.get() + 1; } } - Entry::Vacant(_) => {} + std::collections::hash_map::Entry::Vacant(_) => {} } - ret = std::cmp::max(ret, i - pre + 1); - dic.insert(b, i); + ret = ret.max(i - pre + 1); + map.insert(c, i); } ret as i32 diff --git a/src/problem/p0005_longest_palindromic_substring.rs b/src/problem/p0005_longest_palindromic_substring.rs index 072bd356..76e4908b 100644 --- a/src/problem/p0005_longest_palindromic_substring.rs +++ b/src/problem/p0005_longest_palindromic_substring.rs @@ -27,19 +27,19 @@ pub struct Solution {} impl Solution { pub fn longest_palindrome(s: String) -> String { - if s.len() < 2 { + if s.len() == 1 { return s; } - let a = s.as_bytes(); - let mut ret = &a[0..1]; - for i in 0..a.len() - 1 { - let s1 = Self::pali(a, i as i32, i as i32 + 1); - let s2 = Self::pali(a, i as i32, i as i32); - if ret.len() < s1.len() { + let s = s.as_bytes(); + let mut ret: &[u8] = &[]; + for i in 0..s.len() - 1 { + let s1 = Solution::pali(s, i as i32, i); + let s2 = Solution::pali(s, i as i32, i + 1); + if s1.len() > ret.len() { ret = s1; } - if ret.len() < s2.len() { + if s2.len() > ret.len() { ret = s2; } } @@ -47,17 +47,13 @@ impl Solution { String::from_utf8_lossy(ret).to_string() } - fn pali(a: &[u8], mut i: i32, mut j: i32) -> &[u8] { - if a[i as usize] != a[j as usize] { - return &[]; + fn pali(s: &[u8], mut l: i32, mut r: usize) -> &[u8] { + while l >= 0 && r < s.len() && s[l as usize] == s[r] { + l -= 1; + r += 1; } - while i >= 0 && j < a.len() as i32 && a[i as usize] == a[j as usize] { - i -= 1; - j += 1; - } - - &a[(i + 1) as usize..j as usize] + &s[(l + 1) as usize..r] } } diff --git a/src/problem/p0007_reverse_integer.rs b/src/problem/p0007_reverse_integer.rs index a2b3f0ce..bc216d26 100644 --- a/src/problem/p0007_reverse_integer.rs +++ b/src/problem/p0007_reverse_integer.rs @@ -35,23 +35,17 @@ impl Solution { return x; } - let mut sign: i64 = 1; - let mut x = x as i64; - if x < 0 { - sign = -1; - } - x *= sign; - - let mut ret = 0; + let mut ret: i64 = 0; + let mut x = x; while x != 0 { - ret = ret * 10 + x % 10; - if ret * sign > i32::MAX as i64 || ret * sign < i32::MIN as i64 { + ret = x as i64 % 10 + ret * 10; + if ret > i32::MAX as i64 || ret < i32::MIN as i64 { return 0; } x /= 10; } - (ret * sign) as i32 + ret as i32 } } diff --git a/src/problem/p0011_container_with_most_water.rs b/src/problem/p0011_container_with_most_water.rs index d3aeaf07..6a9c70f0 100644 --- a/src/problem/p0011_container_with_most_water.rs +++ b/src/problem/p0011_container_with_most_water.rs @@ -1,5 +1,3 @@ -use std::cmp::{max, min}; - /// [11] Container With Most Water /// /// You are given an integer array height of length n. There are n vertical lines drawn such that @@ -37,14 +35,17 @@ impl Solution { let mut ret = 0; let mut i = 0; let mut j = height.len() - 1; - while i < j { - let t = min(height[i], height[j]) * (j - i) as i32; - ret = max(t, ret); + let mut t; + while i != j { + t = (j - i) as i32; if height[i] < height[j] { + t *= height[i]; i += 1; } else { + t *= height[j]; j -= 1; } + ret = ret.max(t); } ret diff --git a/src/problem/p0017_letter_combinations_of_a_phone_number.rs b/src/problem/p0017_letter_combinations_of_a_phone_number.rs index 5f79f523..216b755d 100644 --- a/src/problem/p0017_letter_combinations_of_a_phone_number.rs +++ b/src/problem/p0017_letter_combinations_of_a_phone_number.rs @@ -84,12 +84,14 @@ mod tests { #[test] fn test_17() { - assert_eq!(Solution::letter_combinations("23".to_owned()), vec![ - "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" - ]); - assert_eq!(Solution::letter_combinations("2".to_owned()), vec![ - "a", "b", "c" - ]); + assert_eq!( + Solution::letter_combinations("23".to_owned()), + vec!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] + ); + assert_eq!( + Solution::letter_combinations("2".to_owned()), + vec!["a", "b", "c"] + ); assert_eq!( Solution::letter_combinations("".to_owned()), Vec::<String>::new() diff --git a/src/problem/p0022_generate_parentheses.rs b/src/problem/p0022_generate_parentheses.rs index db4ad3ad..433a339d 100644 --- a/src/problem/p0022_generate_parentheses.rs +++ b/src/problem/p0022_generate_parentheses.rs @@ -54,9 +54,10 @@ mod tests { #[test] fn test_22() { - assert_eq!(Solution::generate_parenthesis(3), vec![ - "((()))", "(()())", "(())()", "()(())", "()()()" - ]); + assert_eq!( + Solution::generate_parenthesis(3), + vec!["((()))", "(()())", "(())()", "()(())", "()()()"] + ); assert_eq!(Solution::generate_parenthesis(1), vec!["()"]); assert_eq!(Solution::generate_parenthesis(2), vec!["(())", "()()"]); } diff --git a/src/problem/p0056_merge_intervals.rs b/src/problem/p0056_merge_intervals.rs index 535ab9bf..d2cf0453 100644 --- a/src/problem/p0056_merge_intervals.rs +++ b/src/problem/p0056_merge_intervals.rs @@ -65,8 +65,9 @@ mod tests { Solution::merge(vec![vec![2, 6], vec![1, 3], vec![8, 10], vec![15, 18]]), vec![vec![1, 6], vec![8, 10], vec![15, 18]] ); - assert_eq!(Solution::merge(vec![vec![1, 4], vec![4, 5]]), vec![vec![ - 1, 5 - ]]); + assert_eq!( + Solution::merge(vec![vec![1, 4], vec![4, 5]]), + vec![vec![1, 5]] + ); } } diff --git a/src/problem/p0094_binary_tree_inorder_traversal.rs b/src/problem/p0094_binary_tree_inorder_traversal.rs index f91e8d9d..dce67a84 100644 --- a/src/problem/p0094_binary_tree_inorder_traversal.rs +++ b/src/problem/p0094_binary_tree_inorder_traversal.rs @@ -88,9 +88,10 @@ mod tests { #[test] fn test_94() { - assert_eq!(Solution::inorder_traversal(tree![1, null, 2, 3]), vec![ - 1, 3, 2 - ]); + assert_eq!( + Solution::inorder_traversal(tree![1, null, 2, 3]), + vec![1, 3, 2] + ); assert_eq!(Solution::inorder_traversal(tree![1]), vec![1]); assert_eq!(Solution::inorder_traversal(tree![]), vec![]); } diff --git a/src/problem/p0095_unique_binary_search_trees_ii.rs b/src/problem/p0095_unique_binary_search_trees_ii.rs index 28863e49..e3541e10 100644 --- a/src/problem/p0095_unique_binary_search_trees_ii.rs +++ b/src/problem/p0095_unique_binary_search_trees_ii.rs @@ -80,12 +80,15 @@ mod tests { #[test] fn test_95() { - assert_eq!(Solution::generate_trees(3), vec![ - tree![1, null, 2, null, 3], - tree![1, null, 3, 2], - tree![2, 1, 3], - tree![3, 1, null, null, 2], - tree![3, 2, null, 1] - ]); + assert_eq!( + Solution::generate_trees(3), + vec![ + tree![1, null, 2, null, 3], + tree![1, null, 3, 2], + tree![2, 1, 3], + tree![3, 1, null, null, 2], + tree![3, 2, null, 1] + ] + ); } } diff --git a/src/problem/p0114_flatten_binary_tree_to_linked_list.rs b/src/problem/p0114_flatten_binary_tree_to_linked_list.rs index c327c55a..d85c4c5b 100644 --- a/src/problem/p0114_flatten_binary_tree_to_linked_list.rs +++ b/src/problem/p0114_flatten_binary_tree_to_linked_list.rs @@ -105,8 +105,9 @@ mod tests { fn test_114() { let tree_node = tree!(1, 2, 5, 3, 4, null, 6); Solution::flatten(&tree_node); - assert_eq!(tree_node, tree![ - 1, null, 2, null, 3, null, 4, null, 5, null, 6 - ]); + assert_eq!( + tree_node, + tree![1, null, 2, null, 3, null, 4, null, 5, null, 6] + ); } } diff --git a/src/problem/p0144_binary_tree_preorder_traversal.rs b/src/problem/p0144_binary_tree_preorder_traversal.rs index 1a1e8c20..38091521 100644 --- a/src/problem/p0144_binary_tree_preorder_traversal.rs +++ b/src/problem/p0144_binary_tree_preorder_traversal.rs @@ -89,9 +89,10 @@ mod tests { #[test] fn test_144() { - assert_eq!(Solution::preorder_traversal(tree![1, null, 2, 3]), vec![ - 1, 2, 3 - ]); + assert_eq!( + Solution::preorder_traversal(tree![1, null, 2, 3]), + vec![1, 2, 3] + ); assert_eq!(Solution::preorder_traversal(tree![1]), vec![1]); assert_eq!(Solution::preorder_traversal(tree![]), vec![]); } diff --git a/src/problem/p0145_binary_tree_postorder_traversal.rs b/src/problem/p0145_binary_tree_postorder_traversal.rs index 9d42b79b..9054a25b 100644 --- a/src/problem/p0145_binary_tree_postorder_traversal.rs +++ b/src/problem/p0145_binary_tree_postorder_traversal.rs @@ -98,9 +98,10 @@ mod tests { #[test] fn test_145() { - assert_eq!(Solution::postorder_traversal(tree![1, null, 2, 3]), vec![ - 3, 2, 1 - ]); + assert_eq!( + Solution::postorder_traversal(tree![1, null, 2, 3]), + vec![3, 2, 1] + ); assert_eq!(Solution::postorder_traversal(tree![1]), vec![1]); assert_eq!(Solution::postorder_traversal(tree![1, 2]), vec![2, 1]); } diff --git a/src/problem/p0503_next_greater_element_ii.rs b/src/problem/p0503_next_greater_element_ii.rs index d8cb484c..0432b3a5 100644 --- a/src/problem/p0503_next_greater_element_ii.rs +++ b/src/problem/p0503_next_greater_element_ii.rs @@ -48,11 +48,13 @@ mod tests { #[test] fn test_0503() { - assert_eq!(Solution::next_greater_elements(vec![1, 2, 1]), vec![ - 2, -1, 2 - ]); - assert_eq!(Solution::next_greater_elements(vec![1, 2, 3, 4, 3]), vec![ - 2, 3, 4, -1, 4 - ]); + assert_eq!( + Solution::next_greater_elements(vec![1, 2, 1]), + vec![2, -1, 2] + ); + assert_eq!( + Solution::next_greater_elements(vec![1, 2, 3, 4, 3]), + vec![2, 3, 4, -1, 4] + ); } } diff --git a/src/problem/p0739_daily_temperatures.rs b/src/problem/p0739_daily_temperatures.rs index 88e9c9b5..7bf9910d 100644 --- a/src/problem/p0739_daily_temperatures.rs +++ b/src/problem/p0739_daily_temperatures.rs @@ -59,11 +59,13 @@ mod tests { Solution::daily_temperatures(vec![73, 74, 75, 71, 69, 72, 76, 73]), vec![1, 1, 4, 2, 1, 1, 0, 0] ); - assert_eq!(Solution::daily_temperatures(vec![30, 40, 50, 60]), vec![ - 1, 1, 1, 0 - ]); - assert_eq!(Solution::daily_temperatures(vec![30, 60, 90]), vec![ - 1, 1, 0 - ]); + assert_eq!( + Solution::daily_temperatures(vec![30, 40, 50, 60]), + vec![1, 1, 1, 0] + ); + assert_eq!( + Solution::daily_temperatures(vec![30, 60, 90]), + vec![1, 1, 0] + ); } } diff --git a/src/problem/p0912_sort_an_array.rs b/src/problem/p0912_sort_an_array.rs index 66ab0853..77847a29 100644 --- a/src/problem/p0912_sort_an_array.rs +++ b/src/problem/p0912_sort_an_array.rs @@ -80,16 +80,19 @@ mod tests { fn test_912() { assert_eq!(Solution::sort_array(vec![5, 2, 3, 1]), vec![1, 2, 3, 5]); assert_eq!(Solution::sort_array(vec![-5, 2, 3, 1]), vec![-5, 1, 2, 3]); - assert_eq!(Solution::sort_array(vec![511, 2, -3, 11]), vec![ - -3, 2, 11, 511 - ]); - assert_eq!(Solution::sort_array(vec![5, 1, 1, 2, 0, 0]), vec![ - 0, 0, 1, 1, 2, 5 - ]); + assert_eq!( + Solution::sort_array(vec![511, 2, -3, 11]), + vec![-3, 2, 11, 511] + ); + assert_eq!( + Solution::sort_array(vec![5, 1, 1, 2, 0, 0]), + vec![0, 0, 1, 1, 2, 5] + ); assert_eq!(Solution::sort_array(vec![2, 1]), vec![1, 2]); assert_eq!(Solution::sort_array(vec![1, 1, 2]), vec![1, 1, 2]); - assert_eq!(Solution::sort_array(vec![-1, 2, -8, -10]), vec![ - -10, -8, -1, 2 - ]); + assert_eq!( + Solution::sort_array(vec![-1, 2, -8, -10]), + vec![-10, -8, -1, 2] + ); } } diff --git a/src/problem/p1054_distant_barcodes.rs b/src/problem/p1054_distant_barcodes.rs index 5ac72668..0ea3c2d8 100644 --- a/src/problem/p1054_distant_barcodes.rs +++ b/src/problem/p1054_distant_barcodes.rs @@ -72,9 +72,10 @@ mod tests { fn test_1054() { let a = Solution::rearrange_barcodes(vec![1, 1, 1, 1, 2, 2, 2, 3, 3]); assert!( - [vec![1, 2, 1, 2, 1, 3, 1, 3, 2], vec![ - 1, 3, 1, 2, 1, 2, 1, 2, 3 - ]] + [ + vec![1, 2, 1, 2, 1, 3, 1, 3, 2], + vec![1, 3, 1, 2, 1, 2, 1, 2, 3] + ] .contains(&a) ); diff --git a/src/problem/p2766_relocate_marbles.rs b/src/problem/p2766_relocate_marbles.rs index 8723153d..3076a1b2 100644 --- a/src/problem/p2766_relocate_marbles.rs +++ b/src/problem/p2766_relocate_marbles.rs @@ -68,9 +68,10 @@ mod tests { #[test] fn test_2766() { - assert_eq!(Solution::relocate_marbles(vec![1], vec![1], vec![2]), vec![ - 2 - ]); + assert_eq!( + Solution::relocate_marbles(vec![1], vec![1], vec![2]), + vec![2] + ); assert_eq!( Solution::relocate_marbles(vec![1, 6, 7, 8], vec![1, 7, 2], vec![2, 9, 5]), vec![5, 6, 8, 9] @@ -80,9 +81,11 @@ mod tests { vec![2] ); assert_eq!( - Solution::relocate_marbles(vec![3, 4], vec![4, 3, 1, 2, 2, 3, 2, 4, 1], vec![ - 3, 1, 2, 2, 3, 2, 4, 1, 1 - ]), + Solution::relocate_marbles( + vec![3, 4], + vec![4, 3, 1, 2, 2, 3, 2, 4, 1], + vec![3, 1, 2, 2, 3, 2, 4, 1, 1] + ), vec![1] ); } diff --git a/src/problem/p3142_check_if_grid_satisfies_conditions.rs b/src/problem/p3142_check_if_grid_satisfies_conditions.rs index db4f51b5..b8471c40 100644 --- a/src/problem/p3142_check_if_grid_satisfies_conditions.rs +++ b/src/problem/p3142_check_if_grid_satisfies_conditions.rs @@ -73,12 +73,14 @@ mod tests { #[test] fn test_3142() { - assert!(Solution::satisfies_conditions(vec![vec![1, 0, 2], vec![ - 1, 0, 2 - ]])); - assert!(!Solution::satisfies_conditions(vec![vec![1, 1, 1], vec![ - 0, 0, 0 - ]])); + assert!(Solution::satisfies_conditions(vec![ + vec![1, 0, 2], + vec![1, 0, 2] + ])); + assert!(!Solution::satisfies_conditions(vec![ + vec![1, 1, 1], + vec![0, 0, 0] + ])); assert!(!Solution::satisfies_conditions(vec![ vec![1], vec![2],