Skip to content

chore: format rust code with rustfmt tool #3140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 2 additions & 8 deletions basic/sorting/HeapSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,13 @@ fn sink(nums: &mut Vec<i32>, mut i: usize, n: usize) {
fn main() -> io::Result<()> {
let mut s = String::new();
io::stdin().read_line(&mut s)?;
let s: Vec<usize> = s
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();
let s: Vec<usize> = s.split(' ').map(|s| s.trim().parse().unwrap()).collect();
// let n = s[0];
let m = s[1];

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();

heap_sort(&mut nums);
for num in nums.iter().take(m) {
Expand Down
5 changes: 1 addition & 4 deletions basic/sorting/MergeSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ fn main() -> io::Result<()> {

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();

merge_sort(&mut nums, 0, n - 1);
for num in nums.iter() {
Expand Down
6 changes: 5 additions & 1 deletion lcci/01.03.String to URL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ impl Solution {
s.chars()
.take(length as usize)
.map(|c| {
if c == ' ' { "%20".to_string() } else { c.to_string() }
if c == ' ' {
"%20".to_string()
} else {
c.to_string()
}
})
.collect()
}
Expand Down
6 changes: 5 additions & 1 deletion lcci/01.03.String to URL/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ impl Solution {
s.chars()
.take(length as usize)
.map(|c| {
if c == ' ' { "%20".to_string() } else { c.to_string() }
if c == ' ' {
"%20".to_string()
} else {
c.to_string()
}
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.05.Sum Lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul
impl Solution {
pub fn add_two_numbers(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>
mut l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut cur = dummy.as_mut();
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.05.Sum Lists/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul
impl Solution {
pub fn add_two_numbers(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>
mut l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut cur = dummy.as_mut();
Expand Down
5 changes: 4 additions & 1 deletion lcci/03.02.Min Stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ struct MinStack {
impl MinStack {
/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
Self {
stack: VecDeque::new(),
min_stack: VecDeque::new(),
}
}

fn push(&mut self, x: i32) {
Expand Down
5 changes: 4 additions & 1 deletion lcci/03.02.Min Stack/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ struct MinStack {
impl MinStack {
/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
Self {
stack: VecDeque::new(),
min_stack: VecDeque::new(),
}
}

fn push(&mut self, x: i32) {
Expand Down
6 changes: 5 additions & 1 deletion lcci/03.05.Sort of Stacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ impl SortedStack {
}

fn peek(&self) -> i32 {
if self.is_empty() { -1 } else { *self.stk.back().unwrap() }
if self.is_empty() {
-1
} else {
*self.stk.back().unwrap()
}
}

fn is_empty(&self) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion lcci/03.05.Sort of Stacks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ impl SortedStack {
}

fn peek(&self) -> i32 {
if self.is_empty() { -1 } else { *self.stk.back().unwrap() }
if self.is_empty() {
-1
} else {
*self.stk.back().unwrap()
}
}

fn is_empty(&self) -> bool {
Expand Down
5 changes: 2 additions & 3 deletions lcci/03.06.Animal Shelter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ impl AnimalShelf {
}

fn dequeue_any(&mut self) -> Vec<i32> {
if
self.q[0].is_empty() ||
(!self.q[1].is_empty() && self.q[1].front().unwrap() < self.q[0].front().unwrap())
if self.q[0].is_empty()
|| (!self.q[1].is_empty() && self.q[1].front().unwrap() < self.q[0].front().unwrap())
{
self.dequeue_dog()
} else {
Expand Down
5 changes: 2 additions & 3 deletions lcci/03.06.Animal Shelter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ impl AnimalShelf {
}

fn dequeue_any(&mut self) -> Vec<i32> {
if
self.q[0].is_empty() ||
(!self.q[1].is_empty() && self.q[1].front().unwrap() < self.q[0].front().unwrap())
if self.q[0].is_empty()
|| (!self.q[1].is_empty() && self.q[1].front().unwrap() < self.q[0].front().unwrap())
{
self.dequeue_dog()
} else {
Expand Down
16 changes: 6 additions & 10 deletions lcci/04.02.Minimum Height Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,19 @@ function sortedArrayToBST(nums: number[]): TreeNode | null {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(nums: &Vec<i32>, l: usize, r: usize) -> Option<Rc<RefCell<TreeNode>>> {
if l >= r {
return None;
}
let mid = (l + r) >> 1;
Some(
Rc::new(
RefCell::new(TreeNode {
val: nums[mid],
left: Self::dfs(nums, l, mid),
right: Self::dfs(nums, mid + 1, r),
})
)
)
Some(Rc::new(RefCell::new(TreeNode {
val: nums[mid],
left: Self::dfs(nums, l, mid),
right: Self::dfs(nums, mid + 1, r),
})))
}
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
Self::dfs(&nums, 0, nums.len())
Expand Down
16 changes: 6 additions & 10 deletions lcci/04.02.Minimum Height Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,19 @@ function sortedArrayToBST(nums: number[]): TreeNode | null {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(nums: &Vec<i32>, l: usize, r: usize) -> Option<Rc<RefCell<TreeNode>>> {
if l >= r {
return None;
}
let mid = (l + r) >> 1;
Some(
Rc::new(
RefCell::new(TreeNode {
val: nums[mid],
left: Self::dfs(nums, l, mid),
right: Self::dfs(nums, mid + 1, r),
})
)
)
Some(Rc::new(RefCell::new(TreeNode {
val: nums[mid],
left: Self::dfs(nums, l, mid),
right: Self::dfs(nums, mid + 1, r),
})))
}
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
Self::dfs(&nums, 0, nums.len())
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.03.List of Depth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ function listOfDepth(tree: TreeNode | null): Array<ListNode | null> {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
impl Solution {
pub fn list_of_depth(tree: Option<Rc<RefCell<TreeNode>>>) -> Vec<Option<Box<ListNode>>> {
let mut res = vec![];
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.03.List of Depth/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ function listOfDepth(tree: TreeNode | null): Array<ListNode | null> {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
impl Solution {
pub fn list_of_depth(tree: Option<Rc<RefCell<TreeNode>>>) -> Vec<Option<Box<ListNode>>> {
let mut res = vec![];
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.05.Legal Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ function isValidBST(root: TreeNode | null): boolean {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, prev: &mut Option<i32>) -> bool {
if root.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.05.Legal Binary Search Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ function isValidBST(root: TreeNode | null): boolean {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, prev: &mut Option<i32>) -> bool {
if root.is_none() {
Expand Down
16 changes: 8 additions & 8 deletions lcci/04.10.Check SubTree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ function checkSubTree(t1: TreeNode | null, t2: TreeNode | null): boolean {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(t1: &Option<Rc<RefCell<TreeNode>>>, t2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
match (t1, t2) {
(Some(node1), Some(node2)) => {
let n1 = node1.borrow();
let n2 = node2.borrow();
n1.val == n2.val &&
Solution::dfs(&n1.left, &n2.left) &&
Solution::dfs(&n1.right, &n2.right)
n1.val == n2.val
&& Solution::dfs(&n1.left, &n2.left)
&& Solution::dfs(&n1.right, &n2.right)
}
(None, Some(_)) => false,
(Some(_), None) => false,
Expand All @@ -276,15 +276,15 @@ impl Solution {

pub fn check_sub_tree(
t1: Option<Rc<RefCell<TreeNode>>>,
t2: Option<Rc<RefCell<TreeNode>>>
t2: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (t1, t2) {
(Some(node1), Some(node2)) => {
let n1 = node1.borrow();
let n2 = node2.borrow();
Solution::dfs(&Some(Rc::clone(&node1)), &Some(Rc::clone(&node2))) ||
Solution::check_sub_tree(n1.left.clone(), Some(Rc::clone(&node2))) ||
Solution::check_sub_tree(n1.right.clone(), Some(Rc::clone(&node2)))
Solution::dfs(&Some(Rc::clone(&node1)), &Some(Rc::clone(&node2)))
|| Solution::check_sub_tree(n1.left.clone(), Some(Rc::clone(&node2)))
|| Solution::check_sub_tree(n1.right.clone(), Some(Rc::clone(&node2)))
}
(Some(_), None) => true,
(None, Some(_)) => false,
Expand Down
16 changes: 8 additions & 8 deletions lcci/04.10.Check SubTree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,17 @@ function checkSubTree(t1: TreeNode | null, t2: TreeNode | null): boolean {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(t1: &Option<Rc<RefCell<TreeNode>>>, t2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
match (t1, t2) {
(Some(node1), Some(node2)) => {
let n1 = node1.borrow();
let n2 = node2.borrow();
n1.val == n2.val &&
Solution::dfs(&n1.left, &n2.left) &&
Solution::dfs(&n1.right, &n2.right)
n1.val == n2.val
&& Solution::dfs(&n1.left, &n2.left)
&& Solution::dfs(&n1.right, &n2.right)
}
(None, Some(_)) => false,
(Some(_), None) => false,
Expand All @@ -284,15 +284,15 @@ impl Solution {

pub fn check_sub_tree(
t1: Option<Rc<RefCell<TreeNode>>>,
t2: Option<Rc<RefCell<TreeNode>>>
t2: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (t1, t2) {
(Some(node1), Some(node2)) => {
let n1 = node1.borrow();
let n2 = node2.borrow();
Solution::dfs(&Some(Rc::clone(&node1)), &Some(Rc::clone(&node2))) ||
Solution::check_sub_tree(n1.left.clone(), Some(Rc::clone(&node2))) ||
Solution::check_sub_tree(n1.right.clone(), Some(Rc::clone(&node2)))
Solution::dfs(&Some(Rc::clone(&node1)), &Some(Rc::clone(&node2)))
|| Solution::check_sub_tree(n1.left.clone(), Some(Rc::clone(&node2)))
|| Solution::check_sub_tree(n1.right.clone(), Some(Rc::clone(&node2)))
}
(Some(_), None) => true,
(None, Some(_)) => false,
Expand Down
4 changes: 2 additions & 2 deletions lcci/04.12.Paths with Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ function pathSum(root: TreeNode | null, sum: number): number {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
impl Solution {
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, sum: i32) -> i32 {
let mut cnt = HashMap::new();
Expand All @@ -265,7 +265,7 @@ impl Solution {
root: Option<Rc<RefCell<TreeNode>>>,
sum: i32,
s: i32,
cnt: &mut HashMap<i32, i32>
cnt: &mut HashMap<i32, i32>,
) -> i32 {
if let Some(node) = root {
let node = node.borrow();
Expand Down
4 changes: 2 additions & 2 deletions lcci/04.12.Paths with Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ function pathSum(root: TreeNode | null, sum: number): number {
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
impl Solution {
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, sum: i32) -> i32 {
let mut cnt = HashMap::new();
Expand All @@ -278,7 +278,7 @@ impl Solution {
root: Option<Rc<RefCell<TreeNode>>>,
sum: i32,
s: i32,
cnt: &mut HashMap<i32, i32>
cnt: &mut HashMap<i32, i32>,
) -> i32 {
if let Some(node) = root {
let node = node.borrow();
Expand Down
7 changes: 3 additions & 4 deletions lcci/08.02.Robot in a Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ impl Solution {
}
path.push(vec![i as i32, j as i32]);
grid[i as usize][j as usize] = 1;
if
(i + 1 == grid.len() && j + 1 == grid[0].len()) ||
Self::dfs(grid, path, i + 1, j) ||
Self::dfs(grid, path, i, j + 1)
if (i + 1 == grid.len() && j + 1 == grid[0].len())
|| Self::dfs(grid, path, i + 1, j)
|| Self::dfs(grid, path, i, j + 1)
{
return true;
}
Expand Down
Loading
Loading