diff --git a/.github/workflows/pr-checker.yml b/.github/workflows/pr-checker.yml index 967a8062897b3..77e95524bc2a5 100644 --- a/.github/workflows/pr-checker.yml +++ b/.github/workflows/pr-checker.yml @@ -19,7 +19,7 @@ jobs: 我们项目中各种编程语言代码(包括文档)所采用的格式化工具不同,提交 pr 之前必须确保代码、文档正确格式化。 - - .{md,js,ts,php,sql} 采用 prettier + - .{md,js,ts,php,sql,rs} 采用 prettier - .{c,cpp,java} 采用 clang-format - .{py} 采用 black - .{go} 采用 gofmt @@ -47,7 +47,7 @@ jobs: We use different formatting tools for various programming languages (including documentation) in our project. You must ensure that the code and documentation are correctly formatted before submitting a pr. - - .{md,js,ts,php,sql} use prettier + - .{md,js,ts,php,sql,rs} use prettier - .{c,cpp,java} use clang-format - .{py} use black - .{go} use gofmt diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml index 273497c2a8aae..b017bc5ca86b7 100644 --- a/.github/workflows/prettier.yml +++ b/.github/workflows/prettier.yml @@ -25,7 +25,7 @@ jobs: - name: Run prettier run: | git config --global core.quotepath off - changed_files=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" | grep -E '\.md$|\.js$|\.ts$|\.php|\.sql$' || true) + changed_files=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" | grep -E '\.js$|\.ts$|\.php$|\.sql$|\.rs$|\.md$' || true) if [ -n "$changed_files" ]; then echo "Running prettier on the changed files" echo "$changed_files" | xargs -d '\n' npx prettier --write diff --git a/basic/sorting/HeapSort/README.md b/basic/sorting/HeapSort/README.md index f35e52567a9be..ba368648050ba 100644 --- a/basic/sorting/HeapSort/README.md +++ b/basic/sorting/HeapSort/README.md @@ -207,13 +207,19 @@ fn sink(nums: &mut Vec, mut i: usize, n: usize) { fn main() -> io::Result<()> { let mut s = String::new(); io::stdin().read_line(&mut s)?; - let s: Vec = s.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let s: Vec = 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 = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let mut nums: Vec = nums + .split(' ') + .map(|s| s.trim().parse().unwrap()) + .collect(); heap_sort(&mut nums); for num in nums.iter().take(m) { diff --git a/basic/sorting/InsertionSort/InsertionSort.rs b/basic/sorting/InsertionSort/InsertionSort.rs index 0db36a808fb8b..842f5f8bfd2d2 100644 --- a/basic/sorting/InsertionSort/InsertionSort.rs +++ b/basic/sorting/InsertionSort/InsertionSort.rs @@ -3,7 +3,7 @@ fn insertion_sort(nums: &mut Vec) { for i in 1..n { let mut j = i - 1; let temp = nums[i]; - while j >= 0 as usize && nums[j] > temp { + while j >= (0 as usize) && nums[j] > temp { nums[j + 1] = nums[j]; j -= 1; } diff --git a/basic/sorting/InsertionSort/README.md b/basic/sorting/InsertionSort/README.md index c7c88334a3101..6f0ead5860e15 100644 --- a/basic/sorting/InsertionSort/README.md +++ b/basic/sorting/InsertionSort/README.md @@ -136,7 +136,7 @@ fn insertion_sort(nums: &mut Vec) { for i in 1..n { let mut j = i - 1; let temp = nums[i]; - while j >= 0 as usize && nums[j] > temp { + while j >= (0 as usize) && nums[j] > temp { nums[j + 1] = nums[j]; j -= 1; } diff --git a/basic/sorting/MergeSort/Main.rs b/basic/sorting/MergeSort/Main.rs index a9fcb04189690..9165da9ec0dd9 100644 --- a/basic/sorting/MergeSort/Main.rs +++ b/basic/sorting/MergeSort/Main.rs @@ -43,7 +43,10 @@ fn main() -> io::Result<()> { let mut nums = String::new(); io::stdin().read_line(&mut nums)?; - let mut nums: Vec = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let mut nums: Vec = nums + .split(' ') + .map(|s| s.trim().parse().unwrap()) + .collect(); merge_sort(&mut nums, 0, n - 1); for num in nums.iter() { diff --git a/basic/sorting/MergeSort/README.md b/basic/sorting/MergeSort/README.md index 6e603c1baf6a9..160df0adee76d 100644 --- a/basic/sorting/MergeSort/README.md +++ b/basic/sorting/MergeSort/README.md @@ -359,7 +359,10 @@ fn main() -> io::Result<()> { let mut nums = String::new(); io::stdin().read_line(&mut nums)?; - let mut nums: Vec = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let mut nums: Vec = nums + .split(' ') + .map(|s| s.trim().parse().unwrap()) + .collect(); merge_sort(&mut nums, 0, n - 1); for num in nums.iter() { diff --git a/basic/sorting/QuickSort/Main.rs b/basic/sorting/QuickSort/Main.rs index ea152734e1e42..766e3579ae083 100644 --- a/basic/sorting/QuickSort/Main.rs +++ b/basic/sorting/QuickSort/Main.rs @@ -37,7 +37,10 @@ fn main() -> io::Result<()> { let mut nums = String::new(); io::stdin().read_line(&mut nums)?; - let mut nums: Vec = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let mut nums: Vec = nums + .split(' ') + .map(|s| s.trim().parse().unwrap()) + .collect(); quick_sort(&mut nums, 0, n - 1); for num in nums.iter() { diff --git a/basic/sorting/QuickSort/README.md b/basic/sorting/QuickSort/README.md index 05548708241bc..bf09625d5e6b0 100644 --- a/basic/sorting/QuickSort/README.md +++ b/basic/sorting/QuickSort/README.md @@ -280,7 +280,10 @@ fn main() -> io::Result<()> { let mut nums = String::new(); io::stdin().read_line(&mut nums)?; - let mut nums: Vec = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect(); + let mut nums: Vec = nums + .split(' ') + .map(|s| s.trim().parse().unwrap()) + .collect(); quick_sort(&mut nums, 0, n - 1); for num in nums.iter() { diff --git a/basic/sorting/ShellSort/README.md b/basic/sorting/ShellSort/README.md index 1baa827f26581..80695c4934567 100644 --- a/basic/sorting/ShellSort/README.md +++ b/basic/sorting/ShellSort/README.md @@ -104,7 +104,7 @@ fn shell_sort(nums: &mut Vec) { for i in gap..n { let mut j = i - gap; let temp = nums[i]; - while j >= 0 as usize && nums[j] > temp { + while j >= (0 as usize) && nums[j] > temp { nums[j + gap] = nums[j]; j -= gap; } diff --git a/basic/sorting/ShellSort/ShellSort.rs b/basic/sorting/ShellSort/ShellSort.rs index 5ed3bb428f6a4..5c0fc19806394 100644 --- a/basic/sorting/ShellSort/ShellSort.rs +++ b/basic/sorting/ShellSort/ShellSort.rs @@ -5,7 +5,7 @@ fn shell_sort(nums: &mut Vec) { for i in gap..n { let mut j = i - gap; let temp = nums[i]; - while j >= 0 as usize && nums[j] > temp { + while j >= (0 as usize) && nums[j] > temp { nums[j + gap] = nums[j]; j -= gap; } diff --git a/lcci/01.03.String to URL/README.md b/lcci/01.03.String to URL/README.md index 47348330e4285..e407f2671a2ed 100644 --- a/lcci/01.03.String to URL/README.md +++ b/lcci/01.03.String to URL/README.md @@ -139,11 +139,7 @@ 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() } diff --git a/lcci/01.03.String to URL/README_EN.md b/lcci/01.03.String to URL/README_EN.md index c731195558349..51d49d679fffb 100644 --- a/lcci/01.03.String to URL/README_EN.md +++ b/lcci/01.03.String to URL/README_EN.md @@ -136,11 +136,7 @@ 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() } diff --git a/lcci/01.09.String Rotation/README.md b/lcci/01.09.String Rotation/README.md index 59d534ce98563..d6483f6561de4 100644 --- a/lcci/01.09.String Rotation/README.md +++ b/lcci/01.09.String Rotation/README.md @@ -142,7 +142,7 @@ impl Solution { } if is_pass { return true; - }; + } } false } diff --git a/lcci/01.09.String Rotation/README_EN.md b/lcci/01.09.String Rotation/README_EN.md index 7edd99e072013..e35c37081b08e 100644 --- a/lcci/01.09.String Rotation/README_EN.md +++ b/lcci/01.09.String Rotation/README_EN.md @@ -115,7 +115,7 @@ impl Solution { } if is_pass { return true; - }; + } } false } diff --git a/lcci/02.05.Sum Lists/README.md b/lcci/02.05.Sum Lists/README.md index d1c1d64bbb12e..7c59d3e478ab8 100644 --- a/lcci/02.05.Sum Lists/README.md +++ b/lcci/02.05.Sum Lists/README.md @@ -236,7 +236,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = dummy.as_mut(); diff --git a/lcci/02.05.Sum Lists/README_EN.md b/lcci/02.05.Sum Lists/README_EN.md index 607d8051253be..646b4051c90bc 100644 --- a/lcci/02.05.Sum Lists/README_EN.md +++ b/lcci/02.05.Sum Lists/README_EN.md @@ -231,7 +231,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = dummy.as_mut(); diff --git a/lcci/02.05.Sum Lists/Solution.rs b/lcci/02.05.Sum Lists/Solution.rs index f396419173da0..4ee4affa8b99f 100644 --- a/lcci/02.05.Sum Lists/Solution.rs +++ b/lcci/02.05.Sum Lists/Solution.rs @@ -1,7 +1,7 @@ impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = dummy.as_mut(); diff --git a/lcci/03.02.Min Stack/README.md b/lcci/03.02.Min Stack/README.md index 77922eea5a7fb..36f7dee4a37b1 100644 --- a/lcci/03.02.Min Stack/README.md +++ b/lcci/03.02.Min Stack/README.md @@ -236,13 +236,11 @@ struct MinStack { min_stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - /** initialize your data structure here. */ fn new() -> Self { Self { stack: VecDeque::new(), min_stack: VecDeque::new() } @@ -269,9 +267,7 @@ impl MinStack { fn get_min(&self) -> i32 { *self.min_stack.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); diff --git a/lcci/03.02.Min Stack/README_EN.md b/lcci/03.02.Min Stack/README_EN.md index a8a9fa8128e06..6c5dbfd9dd068 100644 --- a/lcci/03.02.Min Stack/README_EN.md +++ b/lcci/03.02.Min Stack/README_EN.md @@ -236,13 +236,11 @@ struct MinStack { min_stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - /** initialize your data structure here. */ fn new() -> Self { Self { stack: VecDeque::new(), min_stack: VecDeque::new() } @@ -269,9 +267,7 @@ impl MinStack { fn get_min(&self) -> i32 { *self.min_stack.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); diff --git a/lcci/03.02.Min Stack/Solution.rs b/lcci/03.02.Min Stack/Solution.rs index bb2d1b0d9eb9d..fcbc64f5abc63 100644 --- a/lcci/03.02.Min Stack/Solution.rs +++ b/lcci/03.02.Min Stack/Solution.rs @@ -4,13 +4,11 @@ struct MinStack { min_stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - /** initialize your data structure here. */ fn new() -> Self { Self { stack: VecDeque::new(), min_stack: VecDeque::new() } @@ -37,13 +35,11 @@ impl MinStack { fn get_min(&self) -> i32 { *self.min_stack.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); * obj.pop(); * let ret_3: i32 = obj.top(); * let ret_4: i32 = obj.get_min(); - */ \ No newline at end of file + */ diff --git a/lcci/03.05.Sort of Stacks/README.md b/lcci/03.05.Sort of Stacks/README.md index aadc75cad1da7..2fcecc00f8242 100644 --- a/lcci/03.05.Sort of Stacks/README.md +++ b/lcci/03.05.Sort of Stacks/README.md @@ -246,16 +246,14 @@ func (s *SortedStack) IsEmpty() bool { ```rust use std::collections::VecDeque; struct SortedStack { - stack: VecDeque + stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SortedStack { - fn new() -> Self { Self { stack: VecDeque::new() } } @@ -281,9 +279,7 @@ impl SortedStack { fn is_empty(&self) -> bool { self.stack.is_empty() } -} - -/** +}/** * Your SortedStack object will be instantiated and called as such: * let obj = SortedStack::new(); * obj.push(val); diff --git a/lcci/03.05.Sort of Stacks/README_EN.md b/lcci/03.05.Sort of Stacks/README_EN.md index fd364094ca29a..560fede6bb67b 100644 --- a/lcci/03.05.Sort of Stacks/README_EN.md +++ b/lcci/03.05.Sort of Stacks/README_EN.md @@ -251,16 +251,14 @@ func (s *SortedStack) IsEmpty() bool { ```rust use std::collections::VecDeque; struct SortedStack { - stack: VecDeque + stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SortedStack { - fn new() -> Self { Self { stack: VecDeque::new() } } @@ -286,9 +284,7 @@ impl SortedStack { fn is_empty(&self) -> bool { self.stack.is_empty() } -} - -/** +}/** * Your SortedStack object will be instantiated and called as such: * let obj = SortedStack::new(); * obj.push(val); diff --git a/lcci/03.05.Sort of Stacks/Solution.rs b/lcci/03.05.Sort of Stacks/Solution.rs index f54b48af92dcd..e3f9ae5bd62d5 100644 --- a/lcci/03.05.Sort of Stacks/Solution.rs +++ b/lcci/03.05.Sort of Stacks/Solution.rs @@ -1,15 +1,13 @@ use std::collections::VecDeque; struct SortedStack { - stack: VecDeque + stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SortedStack { - fn new() -> Self { Self { stack: VecDeque::new() } } @@ -35,13 +33,11 @@ impl SortedStack { fn is_empty(&self) -> bool { self.stack.is_empty() } -} - -/** +}/** * Your SortedStack object will be instantiated and called as such: * let obj = SortedStack::new(); * obj.push(val); * obj.pop(); * let ret_3: i32 = obj.peek(); * let ret_4: bool = obj.is_empty(); - */ \ No newline at end of file + */ diff --git a/lcci/03.06.Animal Shelter/README.md b/lcci/03.06.Animal Shelter/README.md index fe24b24140944..5d4a155d6025f 100644 --- a/lcci/03.06.Animal Shelter/README.md +++ b/lcci/03.06.Animal Shelter/README.md @@ -219,11 +219,7 @@ impl AnimalShelf { (true, false) => self.dequeue_dog(), (false, true) => self.dequeue_cat(), (false, false) => { - if self.dogs[0] < self.cats[0] { - self.dequeue_dog() - } else { - self.dequeue_cat() - } + if self.dogs[0] < self.cats[0] { self.dequeue_dog() } else { self.dequeue_cat() } } } } @@ -241,9 +237,7 @@ impl AnimalShelf { } vec![self.cats.pop_front().unwrap(), 0] } -} - -/** +}/** * Your AnimalShelf object will be instantiated and called as such: * let obj = AnimalShelf::new(); * obj.enqueue(animal); diff --git a/lcci/03.06.Animal Shelter/README_EN.md b/lcci/03.06.Animal Shelter/README_EN.md index 54ee832ccdd13..3f53f4c513682 100644 --- a/lcci/03.06.Animal Shelter/README_EN.md +++ b/lcci/03.06.Animal Shelter/README_EN.md @@ -224,11 +224,7 @@ impl AnimalShelf { (true, false) => self.dequeue_dog(), (false, true) => self.dequeue_cat(), (false, false) => { - if self.dogs[0] < self.cats[0] { - self.dequeue_dog() - } else { - self.dequeue_cat() - } + if self.dogs[0] < self.cats[0] { self.dequeue_dog() } else { self.dequeue_cat() } } } } @@ -246,9 +242,7 @@ impl AnimalShelf { } vec![self.cats.pop_front().unwrap(), 0] } -} - -/** +}/** * Your AnimalShelf object will be instantiated and called as such: * let obj = AnimalShelf::new(); * obj.enqueue(animal); diff --git a/lcci/03.06.Animal Shelter/Solution.rs b/lcci/03.06.Animal Shelter/Solution.rs index c02a3e4fc7636..8c325e39ddf93 100644 --- a/lcci/03.06.Animal Shelter/Solution.rs +++ b/lcci/03.06.Animal Shelter/Solution.rs @@ -31,11 +31,7 @@ impl AnimalShelf { (true, false) => self.dequeue_dog(), (false, true) => self.dequeue_cat(), (false, false) => { - if self.dogs[0] < self.cats[0] { - self.dequeue_dog() - } else { - self.dequeue_cat() - } + if self.dogs[0] < self.cats[0] { self.dequeue_dog() } else { self.dequeue_cat() } } } } @@ -53,13 +49,11 @@ impl AnimalShelf { } vec![self.cats.pop_front().unwrap(), 0] } -} - -/** +}/** * Your AnimalShelf object will be instantiated and called as such: * let obj = AnimalShelf::new(); * obj.enqueue(animal); * let ret_2: Vec = obj.dequeue_any(); * let ret_3: Vec = obj.dequeue_dog(); * let ret_4: Vec = obj.dequeue_cat(); - */ \ No newline at end of file + */ diff --git a/lcci/04.02.Minimum Height Tree/README.md b/lcci/04.02.Minimum Height Tree/README.md index 3c46700f7ddf7..9f6f9110fe962 100644 --- a/lcci/04.02.Minimum Height Tree/README.md +++ b/lcci/04.02.Minimum Height Tree/README.md @@ -183,11 +183,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::dfs(nums, start, mid), - right: Self::dfs(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::dfs(nums, start, mid), + right: Self::dfs(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { let end = nums.len(); diff --git a/lcci/04.02.Minimum Height Tree/README_EN.md b/lcci/04.02.Minimum Height Tree/README_EN.md index cabbcda182a91..0efe11ae86e63 100644 --- a/lcci/04.02.Minimum Height Tree/README_EN.md +++ b/lcci/04.02.Minimum Height Tree/README_EN.md @@ -194,11 +194,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::dfs(nums, start, mid), - right: Self::dfs(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::dfs(nums, start, mid), + right: Self::dfs(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { let end = nums.len(); diff --git a/lcci/04.02.Minimum Height Tree/Solution.rs b/lcci/04.02.Minimum Height Tree/Solution.rs index f4be88de9a376..f538beab6b774 100644 --- a/lcci/04.02.Minimum Height Tree/Solution.rs +++ b/lcci/04.02.Minimum Height Tree/Solution.rs @@ -24,11 +24,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::dfs(nums, start, mid), - right: Self::dfs(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::dfs(nums, start, mid), + right: Self::dfs(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { let end = nums.len(); diff --git a/lcci/04.10.Check SubTree/README.md b/lcci/04.10.Check SubTree/README.md index 33214c6bc29ab..5cee3c2b1cd5b 100644 --- a/lcci/04.10.Check SubTree/README.md +++ b/lcci/04.10.Check SubTree/README.md @@ -210,7 +210,7 @@ impl Solution { pub fn check_sub_tree( t1: Option>>, - t2: Option>>, + t2: Option>> ) -> bool { Self::dfs(&t1, &t2) } diff --git a/lcci/04.10.Check SubTree/README_EN.md b/lcci/04.10.Check SubTree/README_EN.md index f4b0a92aef896..c698de234fc93 100644 --- a/lcci/04.10.Check SubTree/README_EN.md +++ b/lcci/04.10.Check SubTree/README_EN.md @@ -213,7 +213,7 @@ impl Solution { pub fn check_sub_tree( t1: Option>>, - t2: Option>>, + t2: Option>> ) -> bool { Self::dfs(&t1, &t2) } diff --git a/lcci/04.10.Check SubTree/Solution.rs b/lcci/04.10.Check SubTree/Solution.rs index bd15ab67e5714..b83fededf890b 100644 --- a/lcci/04.10.Check SubTree/Solution.rs +++ b/lcci/04.10.Check SubTree/Solution.rs @@ -36,7 +36,7 @@ impl Solution { pub fn check_sub_tree( t1: Option>>, - t2: Option>>, + t2: Option>> ) -> bool { Self::dfs(&t1, &t2) } diff --git a/lcci/04.12.Paths with Sum/README.md b/lcci/04.12.Paths with Sum/README.md index 961e0a4d17121..25e66de267188 100644 --- a/lcci/04.12.Paths with Sum/README.md +++ b/lcci/04.12.Paths with Sum/README.md @@ -254,7 +254,12 @@ impl Solution { return Self::dfs(root, sum, 0, &mut cnt); } - fn dfs(root: Option>>, sum: i32, s: i32, cnt: &mut HashMap) -> i32 { + fn dfs( + root: Option>>, + sum: i32, + s: i32, + cnt: &mut HashMap + ) -> i32 { if let Some(node) = root { let node = node.borrow(); let s = s + node.val; diff --git a/lcci/04.12.Paths with Sum/README_EN.md b/lcci/04.12.Paths with Sum/README_EN.md index ef9a19b3295a6..781d9a781af82 100644 --- a/lcci/04.12.Paths with Sum/README_EN.md +++ b/lcci/04.12.Paths with Sum/README_EN.md @@ -238,7 +238,12 @@ impl Solution { return Self::dfs(root, sum, 0, &mut cnt); } - fn dfs(root: Option>>, sum: i32, s: i32, cnt: &mut HashMap) -> i32 { + fn dfs( + root: Option>>, + sum: i32, + s: i32, + cnt: &mut HashMap + ) -> i32 { if let Some(node) = root { let node = node.borrow(); let s = s + node.val; diff --git a/lcci/04.12.Paths with Sum/Solution.rs b/lcci/04.12.Paths with Sum/Solution.rs index f8f4810af351a..714389a72ba4b 100644 --- a/lcci/04.12.Paths with Sum/Solution.rs +++ b/lcci/04.12.Paths with Sum/Solution.rs @@ -1,42 +1,47 @@ -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::rc::Rc; -use std::cell::RefCell; -use std::collections::HashMap; -impl Solution { - pub fn path_sum(root: Option>>, sum: i32) -> i32 { - let mut cnt = HashMap::new(); - cnt.insert(0, 1); - return Self::dfs(root, sum, 0, &mut cnt); - } - - fn dfs(root: Option>>, sum: i32, s: i32, cnt: &mut HashMap) -> i32 { - if let Some(node) = root { - let node = node.borrow(); - let s = s + node.val; - let mut ans = *cnt.get(&(s - sum)).unwrap_or(&0); - *cnt.entry(s).or_insert(0) += 1; - ans += Self::dfs(node.left.clone(), sum, s, cnt); - ans += Self::dfs(node.right.clone(), sum, s, cnt); - *cnt.entry(s).or_insert(0) -= 1; - return ans; - } - return 0; - } -} \ No newline at end of file +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +use std::collections::HashMap; +impl Solution { + pub fn path_sum(root: Option>>, sum: i32) -> i32 { + let mut cnt = HashMap::new(); + cnt.insert(0, 1); + return Self::dfs(root, sum, 0, &mut cnt); + } + + fn dfs( + root: Option>>, + sum: i32, + s: i32, + cnt: &mut HashMap + ) -> i32 { + if let Some(node) = root { + let node = node.borrow(); + let s = s + node.val; + let mut ans = *cnt.get(&(s - sum)).unwrap_or(&0); + *cnt.entry(s).or_insert(0) += 1; + ans += Self::dfs(node.left.clone(), sum, s, cnt); + ans += Self::dfs(node.right.clone(), sum, s, cnt); + *cnt.entry(s).or_insert(0) -= 1; + return ans; + } + return 0; + } +} diff --git a/lcci/05.07.Exchange/README.md b/lcci/05.07.Exchange/README.md index 2c9ae8296de20..ad8bf3a24fa39 100644 --- a/lcci/05.07.Exchange/README.md +++ b/lcci/05.07.Exchange/README.md @@ -67,7 +67,7 @@ impl Solution { num >>= 1; let b = num & 1; num >>= 1; - res |= a << i + 1; + res |= a << (i + 1); res |= b << i; i += 2; } diff --git a/lcci/05.07.Exchange/README_EN.md b/lcci/05.07.Exchange/README_EN.md index 4f0e66b769c36..4706724a6f439 100644 --- a/lcci/05.07.Exchange/README_EN.md +++ b/lcci/05.07.Exchange/README_EN.md @@ -67,7 +67,7 @@ impl Solution { num >>= 1; let b = num & 1; num >>= 1; - res |= a << i + 1; + res |= a << (i + 1); res |= b << i; i += 2; } diff --git a/lcci/05.07.Exchange/Solution.rs b/lcci/05.07.Exchange/Solution.rs index 5a7748fd6e5f6..88859be3457b5 100644 --- a/lcci/05.07.Exchange/Solution.rs +++ b/lcci/05.07.Exchange/Solution.rs @@ -7,7 +7,7 @@ impl Solution { num >>= 1; let b = num & 1; num >>= 1; - res |= a << i + 1; + res |= a << (i + 1); res |= b << i; i += 2; } diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md index 50e4a64ab8acd..914d0069b4604 100644 --- a/lcci/08.01.Three Steps Problem/README.md +++ b/lcci/08.01.Three Steps Problem/README.md @@ -415,7 +415,7 @@ impl Solution { let t = a; a = b; b = c; - c = ((a + b) % m + t) % m; + c = (((a + b) % m) + t) % m; } a } diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md index 922cd97729704..6f7f95261d5f3 100644 --- a/lcci/08.01.Three Steps Problem/README_EN.md +++ b/lcci/08.01.Three Steps Problem/README_EN.md @@ -375,7 +375,7 @@ impl Solution { let t = a; a = b; b = c; - c = ((a + b) % m + t) % m; + c = (((a + b) % m) + t) % m; } a } diff --git a/lcci/08.01.Three Steps Problem/Solution.rs b/lcci/08.01.Three Steps Problem/Solution.rs index 5f97ce6f7e98a..134cb7a7eb9a0 100644 --- a/lcci/08.01.Three Steps Problem/Solution.rs +++ b/lcci/08.01.Three Steps Problem/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn ways_to_step(n: i32) -> i32 { - let (mut a, mut b, mut c) = (1, 2, 4); - let m = 1000000007; - for _ in 1..n { - let t = a; - a = b; - b = c; - c = ((a + b) % m + t) % m; - } - a - } -} \ No newline at end of file +impl Solution { + pub fn ways_to_step(n: i32) -> i32 { + let (mut a, mut b, mut c) = (1, 2, 4); + let m = 1000000007; + for _ in 1..n { + let t = a; + a = b; + b = c; + c = (((a + b) % m) + t) % m; + } + a + } +} diff --git a/lcci/08.02.Robot in a Grid/README.md b/lcci/08.02.Robot in a Grid/README.md index 4646cc1fc4b17..0adb67854d256 100644 --- a/lcci/08.02.Robot in a Grid/README.md +++ b/lcci/08.02.Robot in a Grid/README.md @@ -179,9 +179,10 @@ 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; } diff --git a/lcci/08.02.Robot in a Grid/README_EN.md b/lcci/08.02.Robot in a Grid/README_EN.md index 4fcb510174177..784aaedf309d1 100644 --- a/lcci/08.02.Robot in a Grid/README_EN.md +++ b/lcci/08.02.Robot in a Grid/README_EN.md @@ -173,9 +173,10 @@ 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; } diff --git a/lcci/08.02.Robot in a Grid/Solution.rs b/lcci/08.02.Robot in a Grid/Solution.rs index 3f121643bd9ce..6c6e7a5d7adad 100644 --- a/lcci/08.02.Robot in a Grid/Solution.rs +++ b/lcci/08.02.Robot in a Grid/Solution.rs @@ -5,9 +5,10 @@ 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; } diff --git a/lcci/08.03.Magic Index/README.md b/lcci/08.03.Magic Index/README.md index 2aa0059118708..76f01adf62756 100644 --- a/lcci/08.03.Magic Index/README.md +++ b/lcci/08.03.Magic Index/README.md @@ -213,13 +213,13 @@ impl Solution { return -1; } let mid = l + (r - l) / 2; - if nums[mid] >= l as i32 { + if nums[mid] >= (l as i32) { let res = Self::find(nums, l, mid); if res != -1 { return res; } } - if nums[mid] == mid as i32 { + if nums[mid] == (mid as i32) { return mid as i32; } Self::find(nums, mid + 1, r) diff --git a/lcci/08.03.Magic Index/README_EN.md b/lcci/08.03.Magic Index/README_EN.md index 3c27bd7574b19..a47b9e35baebe 100644 --- a/lcci/08.03.Magic Index/README_EN.md +++ b/lcci/08.03.Magic Index/README_EN.md @@ -199,13 +199,13 @@ impl Solution { return -1; } let mid = l + (r - l) / 2; - if nums[mid] >= l as i32 { + if nums[mid] >= (l as i32) { let res = Self::find(nums, l, mid); if res != -1 { return res; } } - if nums[mid] == mid as i32 { + if nums[mid] == (mid as i32) { return mid as i32; } Self::find(nums, mid + 1, r) diff --git a/lcci/08.05.Recursive Mulitply/README.md b/lcci/08.05.Recursive Mulitply/README.md index 4025c35a11d16..0d9d81c58a3b2 100644 --- a/lcci/08.05.Recursive Mulitply/README.md +++ b/lcci/08.05.Recursive Mulitply/README.md @@ -120,7 +120,7 @@ impl Solution { if b == 1 { return a; } - if b & 1 == 1 { + if (b & 1) == 1 { return (Self::multiply(a, b >> 1) << 1) + a; } Self::multiply(a, b >> 1) << 1 diff --git a/lcci/08.05.Recursive Mulitply/README_EN.md b/lcci/08.05.Recursive Mulitply/README_EN.md index 8bf772ea82af4..4dcbc044f9423 100644 --- a/lcci/08.05.Recursive Mulitply/README_EN.md +++ b/lcci/08.05.Recursive Mulitply/README_EN.md @@ -111,7 +111,7 @@ impl Solution { if b == 1 { return a; } - if b & 1 == 1 { + if (b & 1) == 1 { return (Self::multiply(a, b >> 1) << 1) + a; } Self::multiply(a, b >> 1) << 1 diff --git a/lcci/08.05.Recursive Mulitply/Solution.rs b/lcci/08.05.Recursive Mulitply/Solution.rs index 6302bf5e16c94..c1d318132d096 100644 --- a/lcci/08.05.Recursive Mulitply/Solution.rs +++ b/lcci/08.05.Recursive Mulitply/Solution.rs @@ -1,11 +1,11 @@ -impl Solution { - pub fn multiply(a: i32, b: i32) -> i32 { - if b == 1 { - return a; - } - if b & 1 == 1 { - return (Self::multiply(a, b >> 1) << 1) + a; - } - Self::multiply(a, b >> 1) << 1 - } -} +impl Solution { + pub fn multiply(a: i32, b: i32) -> i32 { + if b == 1 { + return a; + } + if (b & 1) == 1 { + return (Self::multiply(a, b >> 1) << 1) + a; + } + Self::multiply(a, b >> 1) << 1 + } +} diff --git a/lcci/10.02.Group Anagrams/README.md b/lcci/10.02.Group Anagrams/README.md index 68c4494e38081..ddebbb79b0dc3 100644 --- a/lcci/10.02.Group Anagrams/README.md +++ b/lcci/10.02.Group Anagrams/README.md @@ -143,7 +143,9 @@ impl Solution { cs.sort(); cs.iter().collect::() }; - map.entry(key).or_insert(vec![]).push(s); + map.entry(key) + .or_insert(vec![]) + .push(s); } map.into_values().collect() } diff --git a/lcci/10.02.Group Anagrams/README_EN.md b/lcci/10.02.Group Anagrams/README_EN.md index bd549e5464e5f..6c4aa02bce865 100644 --- a/lcci/10.02.Group Anagrams/README_EN.md +++ b/lcci/10.02.Group Anagrams/README_EN.md @@ -140,7 +140,9 @@ impl Solution { cs.sort(); cs.iter().collect::() }; - map.entry(key).or_insert(vec![]).push(s); + map.entry(key) + .or_insert(vec![]) + .push(s); } map.into_values().collect() } diff --git a/lcci/10.02.Group Anagrams/Solution.rs b/lcci/10.02.Group Anagrams/Solution.rs index 4773defd8a6c6..85ae009c61d49 100644 --- a/lcci/10.02.Group Anagrams/Solution.rs +++ b/lcci/10.02.Group Anagrams/Solution.rs @@ -9,7 +9,9 @@ impl Solution { cs.sort(); cs.iter().collect::() }; - map.entry(key).or_insert(vec![]).push(s); + map.entry(key) + .or_insert(vec![]) + .push(s); } map.into_values().collect() } diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index b4113b6e2277e..51328dcea8eaa 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -197,16 +197,14 @@ class WordsFrequency { ```rust use std::collections::HashMap; struct WordsFrequency { - cnt: HashMap + cnt: HashMap, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl WordsFrequency { - fn new(book: Vec) -> Self { let mut cnt = HashMap::new(); for word in book.into_iter() { @@ -218,9 +216,7 @@ impl WordsFrequency { fn get(&self, word: String) -> i32 { *self.cnt.get(&word).unwrap_or(&0) } -} - -/** +}/** * Your WordsFrequency object will be instantiated and called as such: * let obj = WordsFrequency::new(book); * let ret_1: i32 = obj.get(word); diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md index 3aa9b2fe56581..b40a13ee08478 100644 --- a/lcci/16.02.Words Frequency/README_EN.md +++ b/lcci/16.02.Words Frequency/README_EN.md @@ -203,16 +203,14 @@ class WordsFrequency { ```rust use std::collections::HashMap; struct WordsFrequency { - cnt: HashMap + cnt: HashMap, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl WordsFrequency { - fn new(book: Vec) -> Self { let mut cnt = HashMap::new(); for word in book.into_iter() { @@ -224,9 +222,7 @@ impl WordsFrequency { fn get(&self, word: String) -> i32 { *self.cnt.get(&word).unwrap_or(&0) } -} - -/** +}/** * Your WordsFrequency object will be instantiated and called as such: * let obj = WordsFrequency::new(book); * let ret_1: i32 = obj.get(word); diff --git a/lcci/16.02.Words Frequency/Solution.rs b/lcci/16.02.Words Frequency/Solution.rs index d19e54d4bfd52..bbab4feff97a9 100644 --- a/lcci/16.02.Words Frequency/Solution.rs +++ b/lcci/16.02.Words Frequency/Solution.rs @@ -1,30 +1,26 @@ -use std::collections::HashMap; -struct WordsFrequency { - cnt: HashMap -} - - -/** - * `&self` means the method takes an immutable reference. - * If you need a mutable reference, change it to `&mut self` instead. - */ -impl WordsFrequency { - - fn new(book: Vec) -> Self { - let mut cnt = HashMap::new(); - for word in book.into_iter() { - *cnt.entry(word).or_insert(0) += 1; - } - Self { cnt } - } - - fn get(&self, word: String) -> i32 { - *self.cnt.get(&word).unwrap_or(&0) - } -} - -/** - * Your WordsFrequency object will be instantiated and called as such: - * let obj = WordsFrequency::new(book); - * let ret_1: i32 = obj.get(word); - */ \ No newline at end of file +use std::collections::HashMap; +struct WordsFrequency { + cnt: HashMap, +} + +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl WordsFrequency { + fn new(book: Vec) -> Self { + let mut cnt = HashMap::new(); + for word in book.into_iter() { + *cnt.entry(word).or_insert(0) += 1; + } + Self { cnt } + } + + fn get(&self, word: String) -> i32 { + *self.cnt.get(&word).unwrap_or(&0) + } +}/** + * Your WordsFrequency object will be instantiated and called as such: + * let obj = WordsFrequency::new(book); + * let ret_1: i32 = obj.get(word); + */ diff --git a/lcci/16.10.Living People/README.md b/lcci/16.10.Living People/README.md index b2c1bc119ae7a..caab2ba2c13c6 100644 --- a/lcci/16.10.Living People/README.md +++ b/lcci/16.10.Living People/README.md @@ -183,7 +183,7 @@ impl Solution { s += d[i]; if mx < s { mx = s; - ans = base + i as i32; + ans = base + (i as i32); } } ans diff --git a/lcci/16.10.Living People/README_EN.md b/lcci/16.10.Living People/README_EN.md index 6523c8b611343..83c6e001e16f1 100644 --- a/lcci/16.10.Living People/README_EN.md +++ b/lcci/16.10.Living People/README_EN.md @@ -185,7 +185,7 @@ impl Solution { s += d[i]; if mx < s { mx = s; - ans = base + i as i32; + ans = base + (i as i32); } } ans diff --git a/lcci/16.10.Living People/Solution.rs b/lcci/16.10.Living People/Solution.rs index e9023a5fd3496..feba5adba9a6f 100644 --- a/lcci/16.10.Living People/Solution.rs +++ b/lcci/16.10.Living People/Solution.rs @@ -1,22 +1,22 @@ -impl Solution { - pub fn max_alive_year(birth: Vec, death: Vec) -> i32 { - let n = birth.len(); - let mut d = vec![0; 102]; - let base = 1900; - for i in 0..n { - d[(birth[i] - base) as usize] += 1; - d[(death[i] - base + 1) as usize] -= 1; - } - let mut ans = 0; - let mut mx = 0; - let mut s = 0; - for i in 0..102 { - s += d[i]; - if mx < s { - mx = s; - ans = base + i as i32; - } - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn max_alive_year(birth: Vec, death: Vec) -> i32 { + let n = birth.len(); + let mut d = vec![0; 102]; + let base = 1900; + for i in 0..n { + d[(birth[i] - base) as usize] += 1; + d[(death[i] - base + 1) as usize] -= 1; + } + let mut ans = 0; + let mut mx = 0; + let mut s = 0; + for i in 0..102 { + s += d[i]; + if mx < s { + mx = s; + ans = base + (i as i32); + } + } + ans + } +} diff --git a/lcci/17.04.Missing Number/README.md b/lcci/17.04.Missing Number/README.md index 698bf718d4c1d..5f6b05adf6f39 100644 --- a/lcci/17.04.Missing Number/README.md +++ b/lcci/17.04.Missing Number/README.md @@ -275,7 +275,7 @@ impl Solution { max = max.max(num); } if max == n { - ((1 + max) * max / 2) - sum + ((1 + max) * max) / 2 - sum } else { n } @@ -289,7 +289,7 @@ impl Solution { let mut res = 0; let n = nums.len(); for i in 0..n { - res ^= nums[i] ^ (i + 1) as i32; + res ^= nums[i] ^ ((i + 1) as i32); } res } diff --git a/lcci/17.04.Missing Number/README_EN.md b/lcci/17.04.Missing Number/README_EN.md index b7e7264a75393..8eebd0daaf96f 100644 --- a/lcci/17.04.Missing Number/README_EN.md +++ b/lcci/17.04.Missing Number/README_EN.md @@ -255,7 +255,7 @@ impl Solution { max = max.max(num); } if max == n { - ((1 + max) * max / 2) - sum + ((1 + max) * max) / 2 - sum } else { n } @@ -269,7 +269,7 @@ impl Solution { let mut res = 0; let n = nums.len(); for i in 0..n { - res ^= nums[i] ^ (i + 1) as i32; + res ^= nums[i] ^ ((i + 1) as i32); } res } diff --git a/lcci/17.04.Missing Number/Solution.rs b/lcci/17.04.Missing Number/Solution.rs index 8fbcac891da60..255bded97307c 100644 --- a/lcci/17.04.Missing Number/Solution.rs +++ b/lcci/17.04.Missing Number/Solution.rs @@ -3,7 +3,7 @@ impl Solution { let mut res = 0; let n = nums.len(); for i in 0..n { - res ^= nums[i] ^ (i + 1) as i32; + res ^= nums[i] ^ ((i + 1) as i32); } res } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" index 9b12ca4fc8b35..329014e3f4bd5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" @@ -248,7 +248,7 @@ function findRepeatNumber(nums: number[]): number { impl Solution { pub fn find_repeat_number(mut nums: Vec) -> i32 { for i in 0..nums.len() { - while i as i32 != nums[i] { + while (i as i32) != nums[i] { let j = nums[i] as usize; if nums[i] == nums[j] { return nums[i]; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.rs" index 5f29b5f8a8844..3da426f3c95e8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.rs" @@ -1,7 +1,7 @@ impl Solution { pub fn find_repeat_number(mut nums: Vec) -> i32 { for i in 0..nums.len() { - while i as i32 != nums[i] { + while (i as i32) != nums[i] { let j = nums[i] as usize; if nums[i] == nums[j] { return nums[i]; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" index 6e601a4cc5154..7266ea868d6c4 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" @@ -265,9 +265,15 @@ impl Solution { let (mut i, mut j) = (0, n); while i < m && j > 0 { match target.cmp(&matrix[i][j - 1]) { - Ordering::Less => j -= 1, - Ordering::Greater => i += 1, - Ordering::Equal => return true, + Ordering::Less => { + j -= 1; + } + Ordering::Greater => { + i += 1; + } + Ordering::Equal => { + return true; + } } } false diff --git "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.rs" index c4efac9e4a9b9..7aa1fc0bdf4e3 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.rs" @@ -8,9 +8,15 @@ impl Solution { let (mut i, mut j) = (0, n); while i < m && j > 0 { match target.cmp(&matrix[i][j - 1]) { - Ordering::Less => j -= 1, - Ordering::Greater => i += 1, - Ordering::Equal => return true, + Ordering::Less => { + j -= 1; + } + Ordering::Greater => { + i += 1; + } + Ordering::Equal => { + return true; + } } } false diff --git "a/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.rs" index 3103d82813ba5..7340f399fed1f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.rs" @@ -2,4 +2,4 @@ impl Solution { pub fn replace_space(s: String) -> String { s.replace(' ', "%20") } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" index 4c27f060ac10b..27c0d6fa934ea 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" @@ -318,12 +318,19 @@ impl Solution { return None; } let val = preorder[0]; - let i = inorder.iter().position(|num| *num == val).unwrap(); - Some(Rc::new(RefCell::new(TreeNode { - val, - left: Self::help(&preorder[1..i + 1], &inorder[..i]), - right: Self::help(&preorder[i + 1..], &inorder[i + 1..]), - }))) + let i = inorder + .iter() + .position(|num| *num == val) + .unwrap(); + Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: Self::help(&preorder[1..i + 1], &inorder[..i]), + right: Self::help(&preorder[i + 1..], &inorder[i + 1..]), + }) + ) + ) } pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.rs" index fd4e7f2f81958..3aba48162d9e2 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.rs" @@ -24,12 +24,19 @@ impl Solution { return None; } let val = preorder[0]; - let i = inorder.iter().position(|num| *num == val).unwrap(); - Some(Rc::new(RefCell::new(TreeNode { - val, - left: Self::help(&preorder[1..i + 1], &inorder[..i]), - right: Self::help(&preorder[i + 1..], &inorder[i + 1..]), - }))) + let i = inorder + .iter() + .position(|num| *num == val) + .unwrap(); + Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: Self::help(&preorder[1..i + 1], &inorder[..i]), + right: Self::help(&preorder[i + 1..], &inorder[i + 1..]), + }) + ) + ) } pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index fa1a5f5b2c361..9c426a127deec 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -251,16 +251,14 @@ class CQueue { ```rust struct CQueue { s1: Vec, - s2: Vec + s2: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl CQueue { - fn new() -> Self { CQueue { s1: Vec::new(), @@ -283,9 +281,7 @@ impl CQueue { } } } -} - -/** +}/** * Your CQueue object will be instantiated and called as such: * let obj = CQueue::new(); * obj.append_tail(value); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.rs" index a04394cef6412..034edd21bf558 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.rs" @@ -1,26 +1,24 @@ struct CQueue { s1: Vec, - s2: Vec + s2: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl CQueue { - fn new() -> Self { CQueue { s1: Vec::new(), s2: Vec::new(), } } - + fn append_tail(&mut self, value: i32) { self.s1.push(value); } - + fn delete_head(&mut self) -> i32 { match self.s2.pop() { Some(value) => value, @@ -32,11 +30,9 @@ impl CQueue { } } } -} - -/** +}/** * Your CQueue object will be instantiated and called as such: * let obj = CQueue::new(); * obj.append_tail(value); * let ret_2: i32 = obj.delete_head(); - */ \ No newline at end of file + */ diff --git "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" index 65dda7a8d0beb..1f6d15d41257f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" @@ -260,11 +260,17 @@ impl Solution { let mut l = 0; let mut r = numbers.len() - 1; while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; match numbers[mid].cmp(&numbers[r]) { - std::cmp::Ordering::Less => r = mid, - std::cmp::Ordering::Equal => r -= 1, - std::cmp::Ordering::Greater => l = mid + 1, + std::cmp::Ordering::Less => { + r = mid; + } + std::cmp::Ordering::Equal => { + r -= 1; + } + std::cmp::Ordering::Greater => { + l = mid + 1; + } } } numbers[l] @@ -281,11 +287,17 @@ impl Solution { if numbers[l] < numbers[r] { break; } - let mid = l + r >> 1; + let mid = (l + r) >> 1; match numbers[mid].cmp(&numbers[l]) { - std::cmp::Ordering::Less => r = mid, - std::cmp::Ordering::Equal => l += 1, - std::cmp::Ordering::Greater => l = mid + 1, + std::cmp::Ordering::Less => { + r = mid; + } + std::cmp::Ordering::Equal => { + l += 1; + } + std::cmp::Ordering::Greater => { + l = mid + 1; + } } } numbers[l] diff --git "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.rs" index 6f7fbf0cb52b8..a090a21062505 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.rs" @@ -3,11 +3,17 @@ impl Solution { let mut l = 0; let mut r = numbers.len() - 1; while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; match numbers[mid].cmp(&numbers[r]) { - std::cmp::Ordering::Less => r = mid, - std::cmp::Ordering::Equal => r -= 1, - std::cmp::Ordering::Greater => l = mid + 1, + std::cmp::Ordering::Less => { + r = mid; + } + std::cmp::Ordering::Equal => { + r -= 1; + } + std::cmp::Ordering::Greater => { + l = mid + 1; + } } } numbers[l] diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" index ace882e393290..da567fd6f9555 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" @@ -265,7 +265,13 @@ function exist(board: string[][], word: string): boolean { ```rust impl Solution { - fn dfs(board: &mut Vec>, chars: &Vec, i: usize, j: usize, mut k: usize) -> bool { + fn dfs( + board: &mut Vec>, + chars: &Vec, + i: usize, + j: usize, + mut k: usize + ) -> bool { if board[i][j] != chars[k] { return false; } @@ -275,10 +281,11 @@ impl Solution { } let temp = board[i][j]; board[i][j] = ' '; - if i != 0 && Self::dfs(board, chars, i - 1, j, k) - || j != 0 && Self::dfs(board, chars, i, j - 1, k) - || i != board.len() - 1 && Self::dfs(board, chars, i + 1, j, k) - || j != board[0].len() - 1 && Self::dfs(board, chars, i, j + 1, k) + if + (i != 0 && Self::dfs(board, chars, i - 1, j, k)) || + (j != 0 && Self::dfs(board, chars, i, j - 1, k)) || + (i != board.len() - 1 && Self::dfs(board, chars, i + 1, j, k)) || + (j != board[0].len() - 1 && Self::dfs(board, chars, i, j + 1, k)) { return true; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.rs" index cab219c2587ac..479ca64d8fc71 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.rs" @@ -1,5 +1,11 @@ impl Solution { - fn dfs(board: &mut Vec>, chars: &Vec, i: usize, j: usize, mut k: usize) -> bool { + fn dfs( + board: &mut Vec>, + chars: &Vec, + i: usize, + j: usize, + mut k: usize + ) -> bool { if board[i][j] != chars[k] { return false; } @@ -9,10 +15,11 @@ impl Solution { } let temp = board[i][j]; board[i][j] = ' '; - if i != 0 && Self::dfs(board, chars, i - 1, j, k) - || j != 0 && Self::dfs(board, chars, i, j - 1, k) - || i != board.len() - 1 && Self::dfs(board, chars, i + 1, j, k) - || j != board[0].len() - 1 && Self::dfs(board, chars, i, j + 1, k) + if + (i != 0 && Self::dfs(board, chars, i - 1, j, k)) || + (j != 0 && Self::dfs(board, chars, i, j - 1, k)) || + (i != board.len() - 1 && Self::dfs(board, chars, i + 1, j, k)) || + (j != board[0].len() - 1 && Self::dfs(board, chars, i, j + 1, k)) { return true; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" index 41814192dd6fa..02008e6c0125a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" @@ -315,7 +315,7 @@ function movingCount(m: number, n: number, k: number): number { 循环: ```rust -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn moving_count(m: i32, n: i32, k: i32) -> i32 { let mut set = HashSet::new(); @@ -323,13 +323,15 @@ impl Solution { queue.push_back([0, 0]); while let Some([i, j]) = queue.pop_front() { let key = format!("{},{}", i, j); - if i == m - || j == n - || set.contains(&key) - || k < format!("{}{}", i, j) - .chars() - .map(|c| c.to_string().parse::().unwrap()) - .sum::() + if + i == m || + j == n || + set.contains(&key) || + k < + format!("{}{}", i, j) + .chars() + .map(|c| c.to_string().parse::().unwrap()) + .sum::() { continue; } @@ -347,10 +349,11 @@ impl Solution { ```rust impl Solution { fn dfs(sign: &mut Vec>, k: usize, i: usize, j: usize) -> i32 { - if i == sign.len() - || j == sign[0].len() - || sign[i][j] - || j % 10 + j / 10 % 10 + i % 10 + i / 10 % 10 > k + if + i == sign.len() || + j == sign[0].len() || + sign[i][j] || + (j % 10) + ((j / 10) % 10) + (i % 10) + ((i / 10) % 10) > k { return 0; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.rs" index f757aab23f0ac..161b9fc85b07b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.rs" @@ -1,9 +1,10 @@ impl Solution { fn dfs(sign: &mut Vec>, k: usize, i: usize, j: usize) -> i32 { - if i == sign.len() - || j == sign[0].len() - || sign[i][j] - || j % 10 + j / 10 % 10 + i % 10 + i / 10 % 10 > k + if + i == sign.len() || + j == sign[0].len() || + sign[i][j] || + (j % 10) + ((j / 10) % 10) + (i % 10) + ((i / 10) % 10) > k { return 0; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" index 206fbc2505c0b..6835a8a6c36d1 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" @@ -219,7 +219,7 @@ impl Solution { return n - 1; } let count = (n - 2) / 3; - 3i32.pow(count as u32) * (n - count * 3) + (3i32).pow(count as u32) * (n - count * 3) } } ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.rs" index 41107f309e8a9..9f71ac25e9c8e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.rs" @@ -4,6 +4,6 @@ impl Solution { return n - 1; } let count = (n - 2) / 3; - 3i32.pow(count as u32) * (n - count * 3) + (3i32).pow(count as u32) * (n - count * 3) } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/README.md" index 70ac2b0a1d527..ea8b727fae760 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/README.md" @@ -192,7 +192,7 @@ impl Solution { res = (res * 3) % 1000000007; n -= 3; } - ((res * n as i64) % 1000000007) as i32 + ((res * (n as i64)) % 1000000007) as i32 } } ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/Solution.rs" index 30332f9987f47..574e7adf2117b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- II. \345\211\252\347\273\263\345\255\220 II/Solution.rs" @@ -8,6 +8,6 @@ impl Solution { res = (res * 3) % 1000000007; n -= 3; } - ((res * n as i64) % 1000000007) as i32 + ((res * (n as i64)) % 1000000007) as i32 } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" index c2e9c0289a070..0c0687063abd1 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" @@ -218,7 +218,7 @@ impl Solution { // `n` should greater or equal to zero let mut ret = 1.0; while n != 0 { - if n & 0x1 == 1 { + if (n & 0x1) == 1 { ret *= *x; } *x *= *x; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.rs" index 732a8d14260af..f4cda7e7e8768 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.rs" @@ -1,26 +1,26 @@ -impl Solution { - #[allow(dead_code)] - pub fn my_pow(x: f64, n: i32) -> f64 { - let mut x = x; - let n = n as i64; - if n >= 0 { - Self::quick_pow(&mut x, n) - } else { - 1.0 / Self::quick_pow(&mut x, -n) - } - } - - #[allow(dead_code)] - fn quick_pow(x: &mut f64, mut n: i64) -> f64 { - // `n` should greater or equal to zero - let mut ret = 1.0; - while n != 0 { - if n & 0x1 == 1 { - ret *= *x; - } - *x *= *x; - n >>= 1; - } - ret - } -} \ No newline at end of file +impl Solution { + #[allow(dead_code)] + pub fn my_pow(x: f64, n: i32) -> f64 { + let mut x = x; + let n = n as i64; + if n >= 0 { + Self::quick_pow(&mut x, n) + } else { + 1.0 / Self::quick_pow(&mut x, -n) + } + } + + #[allow(dead_code)] + fn quick_pow(x: &mut f64, mut n: i64) -> f64 { + // `n` should greater or equal to zero + let mut ret = 1.0; + while n != 0 { + if (n & 0x1) == 1 { + ret *= *x; + } + *x *= *x; + n >>= 1; + } + ret + } +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.rs" index d1831bd451f04..9e3df3153bc2e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.rs" @@ -26,4 +26,4 @@ impl Solution { } head } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.rs" index f5b12a69ebe3b..eecefcf187a63 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.rs" @@ -9,4 +9,4 @@ impl Solution { } nums } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" index 3c628d07c7277..7512a2b908d28 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" @@ -403,7 +403,7 @@ function mergeTwoLists(l1: ListNode | null, l2: ListNode | null): ListNode | nul impl Solution { pub fn merge_two_lists( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { match (l1.is_some(), l2.is_some()) { (false, false) => None, @@ -452,7 +452,7 @@ impl Solution { impl Solution { pub fn merge_two_lists( l1: Option>, - l2: Option>, + l2: Option> ) -> Option> { match (l1, l2) { (Some(mut n1), Some(mut n2)) => { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.rs" index 31e1cf11be04f..6b39d8c9626d7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.rs" @@ -17,7 +17,7 @@ impl Solution { pub fn merge_two_lists( l1: Option>, - l2: Option>, + l2: Option> ) -> Option> { match (l1, l2) { (Some(mut n1), Some(mut n2)) => { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" index 9ec4db4613844..a64c0aeac11cd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" @@ -263,22 +263,22 @@ use std::cell::RefCell; impl Solution { pub fn is_sub_structure( a: Option>>, - b: Option>>, + b: Option>> ) -> bool { Self::is_sub_structure_help(&a, &b) } fn is_sub_structure_help( a: &Option>>, - b: &Option>>, + b: &Option>> ) -> bool { if a.is_none() || b.is_none() { return false; } - Self::dfs(a, b) - || Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b) - || Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b) + Self::dfs(a, b) || + Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b) || + Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b) } fn dfs(a: &Option>>, b: &Option>>) -> bool { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.rs" index 3fa492f0f475b..6cadc90c4cf93 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.rs" @@ -21,22 +21,22 @@ use std::cell::RefCell; impl Solution { pub fn is_sub_structure( a: Option>>, - b: Option>>, + b: Option>> ) -> bool { Self::is_sub_structure_help(&a, &b) } fn is_sub_structure_help( a: &Option>>, - b: &Option>>, + b: &Option>> ) -> bool { if a.is_none() || b.is_none() { return false; } - Self::dfs(a, b) - || Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b) - || Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b) + Self::dfs(a, b) || + Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b) || + Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b) } fn dfs(a: &Option>>, b: &Option>>) -> bool { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.rs" index 9b872779d660e..a36212fa60696 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.rs" @@ -1,40 +1,45 @@ impl Solution { pub fn spiral_order(matrix: Vec>) -> Vec { - let mut ans=Vec::new(); - if matrix.len()==0{ + let mut ans = Vec::new(); + if matrix.len() == 0 { return ans; } - let (mut left,mut right,mut top,mut bottom)=(0,matrix[0].len()-1,0,matrix.len()-1); - loop{ - for i in left..right+1{ + let (mut left, mut right, mut top, mut bottom) = ( + 0, + matrix[0].len() - 1, + 0, + matrix.len() - 1, + ); + loop { + for i in left..right + 1 { ans.push(matrix[top][i]); } - top+=1; - if (top as i32)>(bottom as i32){ + top += 1; + if (top as i32) > (bottom as i32) { break; } - for i in top..bottom+1{ + for i in top..bottom + 1 { ans.push(matrix[i][right]); } - right-=1; - if (right as i32)<(left as i32){ + right -= 1; + if (right as i32) < (left as i32) { break; } - for i in (left..right+1).rev(){ + for i in (left..right + 1).rev() { ans.push(matrix[bottom][i]); } - bottom-=1; - if (bottom as i32)<(top as i32){ + bottom -= 1; + if (bottom as i32) < (top as i32) { break; } - for i in (top..bottom+1).rev(){ + for i in (top..bottom + 1).rev() { ans.push(matrix[i][left]); } - left+=1; - if (left as i32)>(right as i32){ + left += 1; + if (left as i32) > (right as i32) { break; } } ans } - } \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" index 19d59581e07ce..ea339d7be34ec 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" @@ -252,13 +252,11 @@ struct MinStack { min_stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - /** initialize your data structure here. */ fn new() -> Self { Self { stack: VecDeque::new(), min_stack: VecDeque::new() } @@ -285,9 +283,7 @@ impl MinStack { fn get_min(&self) -> i32 { *self.min_stack.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.rs" index bb2d1b0d9eb9d..fcbc64f5abc63 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.rs" @@ -4,13 +4,11 @@ struct MinStack { min_stack: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - /** initialize your data structure here. */ fn new() -> Self { Self { stack: VecDeque::new(), min_stack: VecDeque::new() } @@ -37,13 +35,11 @@ impl MinStack { fn get_min(&self) -> i32 { *self.min_stack.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); * obj.pop(); * let ret_3: i32 = obj.top(); * let ret_4: i32 = obj.get_min(); - */ \ No newline at end of file + */ diff --git "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" index 86aa39f6e3321..328d12b08cc95 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" @@ -352,8 +352,10 @@ impl Solution { return false; } if val < root_val { - return Self::dfs(start, i, root_val, postorder) - && Self::dfs(i + 1, end - 1, max_val, postorder); + return ( + Self::dfs(start, i, root_val, postorder) && + Self::dfs(i + 1, end - 1, max_val, postorder) + ); } } Self::dfs(start, end - 1, max_val, postorder) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.rs" index 0dd707bbee237..a5f33356ced69 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.rs" @@ -10,8 +10,10 @@ impl Solution { return false; } if val < root_val { - return Self::dfs(start, i, root_val, postorder) - && Self::dfs(i + 1, end - 1, max_val, postorder); + return ( + Self::dfs(start, i, root_val, postorder) && + Self::dfs(i + 1, end - 1, max_val, postorder) + ); } } Self::dfs(start, end - 1, max_val, postorder) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" index 8ea3d812ce11e..67635eab1ce2d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/README.md" @@ -312,7 +312,7 @@ impl Solution { root: &Option>>, mut target: i32, paths: &mut Vec, - res: &mut Vec>, + res: &mut Vec> ) { if let Some(node) = root.as_ref() { let node = node.borrow(); @@ -360,7 +360,7 @@ impl Solution { fn dfs( root: &Option>>, mut target: i32, - paths: &mut Vec, + paths: &mut Vec ) -> Vec> { let node = root.as_ref().unwrap().borrow(); paths.push(node.val); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.rs" index 5239f5c06216d..76d22e22357ee 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23034. \344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/Solution.rs" @@ -23,7 +23,7 @@ impl Solution { root: &Option>>, mut target: i32, paths: &mut Vec, - res: &mut Vec>, + res: &mut Vec> ) { if let Some(node) = root.as_ref() { let node = node.borrow(); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.rs" index 6270a69510a27..7b09af00a2c12 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.rs" @@ -12,4 +12,4 @@ impl Solution { } m } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" index aba6be58d2713..ad58f7eeb800d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" @@ -382,7 +382,7 @@ impl Solution { while start < end && end > k { let num = arr[start]; let mut mark = start; - for i in (start + 1)..end { + for i in start + 1..end { if arr[i] < num { mark += 1; arr.swap(i, mark); @@ -393,7 +393,7 @@ impl Solution { if mark <= k { start = mark + 1; } else { - end = mark + end = mark; } } arr[0..k].to_vec() diff --git "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.rs" index 330afd159a8e8..1f41c3c2b307d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.rs" @@ -6,7 +6,7 @@ impl Solution { while start < end && end > k { let num = arr[start]; let mut mark = start; - for i in (start + 1)..end { + for i in start + 1..end { if arr[i] < num { mark += 1; arr.swap(i, mark); @@ -17,7 +17,7 @@ impl Solution { if mark <= k { start = mark + 1; } else { - end = mark + end = mark; } } arr[0..k].to_vec() diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" index afe8eb22789cc..1bf17ab921c10 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" @@ -347,7 +347,7 @@ impl MedianFinder { let mut l = 0; let mut r = self.nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if self.nums[mid] < num { l = mid + 1; } else { @@ -364,9 +364,7 @@ impl MedianFinder { } f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 } -} - -/** +}/** * Your MedianFinder object will be instantiated and called as such: * let obj = MedianFinder::new(); * obj.add_num(num); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.rs" index c377c54db931c..be3a7ac65744b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.rs" @@ -16,7 +16,7 @@ impl MedianFinder { let mut l = 0; let mut r = self.nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if self.nums[mid] < num { l = mid + 1; } else { @@ -33,11 +33,9 @@ impl MedianFinder { } f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 } -} - -/** +}/** * Your MedianFinder object will be instantiated and called as such: * let obj = MedianFinder::new(); * obj.add_num(num); * let ret_2: f64 = obj.find_median(); - */ \ No newline at end of file + */ diff --git "a/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/README.md" index b8412477da007..3f0a8f7e9a2b4 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/README.md" @@ -143,7 +143,9 @@ function minNumber(nums: number[]): string { impl Solution { pub fn min_number(mut nums: Vec) -> String { nums.sort_by(|a, b| format!("{}{}", a, b).cmp(&format!("{}{}", b, a))); - nums.iter().map(|num| num.to_string()).collect() + nums.iter() + .map(|num| num.to_string()) + .collect() } } ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.rs" index 3417604e55c3e..a9fd968eaaeb4 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.rs" @@ -1,6 +1,8 @@ impl Solution { pub fn min_number(mut nums: Vec) -> String { nums.sort_by(|a, b| format!("{}{}", a, b).cmp(&format!("{}{}", b, a))); - nums.iter().map(|num| num.to_string()).collect() + nums.iter() + .map(|num| num.to_string()) + .collect() } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" index b93d1acc47887..19ea95d97e9ab 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" @@ -280,13 +280,13 @@ impl Solution { if dp[i] == n2 { p2 += 1; - }; + } if dp[i] == n3 { p3 += 1; - }; + } if dp[i] == n5 { p5 += 1; - }; + } } dp[n - 1] } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.rs" index 677fa5c6abcbe..315d0c64aeeae 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.rs" @@ -13,13 +13,13 @@ impl Solution { if dp[i] == n2 { p2 += 1; - }; + } if dp[i] == n3 { p3 += 1; - }; + } if dp[i] == n5 { p5 += 1; - }; + } } dp[n - 1] } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/README.md" index 60e4e92830040..eac2d75c38233 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/README.md" @@ -137,12 +137,12 @@ impl Solution { let search = |x| { let mut l = 0; let mut r = nums.len(); - while l < r { + while l < r { let mid = l + (r - l) / 2; if nums[mid] >= x { r = mid; } else { - l = mid + 1 + l = mid + 1; } } l as i32 diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.rs" index 1cee7af5c28c8..7544d93978cec 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.rs" @@ -3,16 +3,16 @@ impl Solution { let search = |x| { let mut l = 0; let mut r = nums.len(); - while l < r { + while l < r { let mid = l + (r - l) / 2; if nums[mid] >= x { r = mid; } else { - l = mid + 1 + l = mid + 1; } } l as i32 }; search(target + 1) - search(target) } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" index 1efd8776dd8a8..8d426cbca26b9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" @@ -153,7 +153,7 @@ impl Solution { impl Solution { pub fn missing_number(nums: Vec) -> i32 { let n = nums.len() as i32; - let mut sum = (1 + n) * n / 2; + let mut sum = ((1 + n) * n) / 2; for num in nums.iter() { sum -= num; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.rs" index 415474ebaa0cd..cb029b6793691 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.rs" @@ -11,4 +11,4 @@ impl Solution { } l } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.rs" index cfc3a71183a68..8384b6c436c76 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.rs" @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -30,4 +30,4 @@ impl Solution { } } } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" index b40f6da03a6e1..0656059d874ac 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" @@ -279,9 +279,9 @@ impl Solution { Some(node) => { let mut node = node.borrow_mut(); let a = 10; - (Self::dfs(&node.left) - Self::dfs(&node.right)).abs() <= 1 - && Self::is_balanced(node.left.take()) - && Self::is_balanced(node.right.take()) + (Self::dfs(&node.left) - Self::dfs(&node.right)).abs() <= 1 && + Self::is_balanced(node.left.take()) && + Self::is_balanced(node.right.take()) } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.rs" index 63d5e905076fe..fe28d0c91e750 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.rs" @@ -34,9 +34,9 @@ impl Solution { Some(node) => { let mut node = node.borrow_mut(); let a = 10; - (Self::dfs(&node.left) - Self::dfs(&node.right)).abs() <= 1 - && Self::is_balanced(node.left.take()) - && Self::is_balanced(node.right.take()) + (Self::dfs(&node.left) - Self::dfs(&node.right)).abs() <= 1 && + Self::is_balanced(node.left.take()) && + Self::is_balanced(node.right.take()) } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" index bdeee5afa6c46..98bea443b94bb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" @@ -161,9 +161,15 @@ impl Solution { let mut r = nums.len() - 1; loop { match target.cmp(&(nums[l] + nums[r])) { - Ordering::Less => r -= 1, - Ordering::Greater => l += 1, - Ordering::Equal => break vec![nums[l], nums[r]], + Ordering::Less => { + r -= 1; + } + Ordering::Greater => { + l += 1; + } + Ordering::Equal => { + break vec![nums[l], nums[r]]; + } } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.rs" index bc79e07dc46dd..379a9719cea17 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.rs" @@ -6,9 +6,15 @@ impl Solution { let mut r = nums.len() - 1; loop { match target.cmp(&(nums[l] + nums[r])) { - Ordering::Less => r -= 1, - Ordering::Greater => l += 1, - Ordering::Equal => break vec![nums[l], nums[r]], + Ordering::Less => { + r -= 1; + } + Ordering::Greater => { + l += 1; + } + Ordering::Equal => { + break vec![nums[l], nums[r]]; + } } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index 8e1692f8cd764..fedafce644d42 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -216,7 +216,7 @@ impl Solution { } q.push_back(i); if i >= k - 1 { - ans[i - k + 1] = nums[q[0]] + ans[i - k + 1] = nums[q[0]]; } } ans diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" index 0ba8d5bd62dfc..5e86415bb5c6a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" @@ -14,9 +14,9 @@ impl Solution { } q.push_back(i); if i >= k - 1 { - ans[i - k + 1] = nums[q[0]] + ans[i - k + 1] = nums[q[0]]; } } ans } -} \ No newline at end of file +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index b781aa05973d4..f7cb574e1a8be 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -359,9 +359,7 @@ impl MaxQueue { } res } -} - -/** +}/** * Your MaxQueue object will be instantiated and called as such: * let obj = MaxQueue::new(); * let ret_1: i32 = obj.max_value(); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" index f1bdb32c7883c..a01e139c882b5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.rs" @@ -38,12 +38,10 @@ impl MaxQueue { } res } -} - -/** +}/** * Your MaxQueue object will be instantiated and called as such: * let obj = MaxQueue::new(); * let ret_1: i32 = obj.max_value(); * obj.push_back(value); * let ret_3: i32 = obj.pop_front(); - */ \ No newline at end of file + */ diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" index 83e932d00b05e..622634f52d1a6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" @@ -104,7 +104,13 @@ var sumNums = function (n: number): number { ```rust impl Solution { pub fn sum_nums(mut n: i32) -> i32 { - n != 0 && (n += Solution::sum_nums(n - 1), true).1; + n != 0 && + ( + { + n += Solution::sum_nums(n - 1); + }, + true, + ).1; n } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.rs" index 9b81a2223e7c6..c98351dd8f0ce 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.rs" @@ -1,6 +1,12 @@ -impl Solution { - pub fn sum_nums(mut n: i32) -> i32 { - n != 0 && (n += Solution::sum_nums(n - 1), true).1; - n - } -} +impl Solution { + pub fn sum_nums(mut n: i32) -> i32 { + n != 0 && + ( + { + n += Solution::sum_nums(n - 1); + }, + true, + ).1; + n + } +} diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" index 5a69087ad8b10..74a8c34a44e2d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" @@ -368,18 +368,22 @@ impl Solution { pub fn lowest_common_ancestor( mut root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { let p = p.unwrap().borrow().val; let q = q.unwrap().borrow().val; loop { let mut cur = root.as_ref().unwrap().borrow().val; match (cur.cmp(&p), cur.cmp(&q)) { - (Ordering::Less, Ordering::Less) => root = root.unwrap().borrow().right.clone(), + (Ordering::Less, Ordering::Less) => { + root = root.unwrap().borrow().right.clone(); + } (Ordering::Greater, Ordering::Greater) => { - root = root.unwrap().borrow().left.clone() + root = root.unwrap().borrow().left.clone(); + } + (_, _) => { + break root; } - (_, _) => break root, } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" index e520e270d10e3..317dcb8198277 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" @@ -23,18 +23,22 @@ impl Solution { pub fn lowest_common_ancestor( mut root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { let p = p.unwrap().borrow().val; let q = q.unwrap().borrow().val; loop { let mut cur = root.as_ref().unwrap().borrow().val; match (cur.cmp(&p), cur.cmp(&q)) { - (Ordering::Less, Ordering::Less) => root = root.unwrap().borrow().right.clone(), + (Ordering::Less, Ordering::Less) => { + root = root.unwrap().borrow().right.clone(); + } (Ordering::Greater, Ordering::Greater) => { - root = root.unwrap().borrow().left.clone() + root = root.unwrap().borrow().left.clone(); + } + (_, _) => { + break root; } - (_, _) => break root, } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" index 74710ec36b022..eee9071c533bc 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" @@ -264,7 +264,7 @@ impl Solution { pub fn lowest_common_ancestor( root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { if root.is_none() || root == p || root == q { return root; @@ -272,12 +272,12 @@ impl Solution { let left = Self::lowest_common_ancestor( root.as_ref().unwrap().borrow_mut().left.take(), p.clone(), - q.clone(), + q.clone() ); let right = Self::lowest_common_ancestor( root.as_ref().unwrap().borrow_mut().right.take(), p.clone(), - q.clone(), + q.clone() ); match (left.is_none(), right.is_none()) { (true, false) => right, diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" index f4200dfb286a4..e0673044d8cfe 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.rs" @@ -22,7 +22,7 @@ impl Solution { pub fn lowest_common_ancestor( root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { if root.is_none() || root == p || root == q { return root; @@ -30,12 +30,12 @@ impl Solution { let left = Self::lowest_common_ancestor( root.as_ref().unwrap().borrow_mut().left.take(), p.clone(), - q.clone(), + q.clone() ); let right = Self::lowest_common_ancestor( root.as_ref().unwrap().borrow_mut().right.take(), p.clone(), - q.clone(), + q.clone() ); match (left.is_none(), right.is_none()) { (true, false) => right, diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/README.md" index cdda8d7103b38..4c1c8ee3535f0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/README.md" @@ -160,8 +160,8 @@ function addBinary(a: string, b: string): string { ```rust impl Solution { pub fn add_binary(a: String, b: String) -> String { - let mut i = a.len() as i32 - 1; - let mut j = b.len() as i32 - 1; + let mut i = (a.len() as i32) - 1; + let mut j = (b.len() as i32) - 1; let mut carry = 0; let mut ans = String::new(); let a = a.as_bytes(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/Solution.rs" index 77f05c567ae06..60e80f5fa5523 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 002. \344\272\214\350\277\233\345\210\266\345\212\240\346\263\225/Solution.rs" @@ -1,23 +1,23 @@ -impl Solution { - pub fn add_binary(a: String, b: String) -> String { - let mut i = a.len() as i32 - 1; - let mut j = b.len() as i32 - 1; - let mut carry = 0; - let mut ans = String::new(); - let a = a.as_bytes(); - let b = b.as_bytes(); - while i >= 0 || j >= 0 || carry > 0 { - if i >= 0 { - carry += a[i as usize] - b'0'; - i -= 1; - } - if j >= 0 { - carry += b[j as usize] - b'0'; - j -= 1; - } - ans.push_str(&(carry % 2).to_string()); - carry /= 2; - } - ans.chars().rev().collect() - } -} +impl Solution { + pub fn add_binary(a: String, b: String) -> String { + let mut i = (a.len() as i32) - 1; + let mut j = (b.len() as i32) - 1; + let mut carry = 0; + let mut ans = String::new(); + let a = a.as_bytes(); + let b = b.as_bytes(); + while i >= 0 || j >= 0 || carry > 0 { + if i >= 0 { + carry += a[i as usize] - b'0'; + i -= 1; + } + if j >= 0 { + carry += b[j as usize] - b'0'; + j -= 1; + } + ans.push_str(&(carry % 2).to_string()); + carry /= 2; + } + ans.chars().rev().collect() + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" index 59e65268c2479..504da786baeed 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" @@ -259,9 +259,15 @@ impl Solution { let mut r = n - 1; loop { match target.cmp(&(numbers[l] + numbers[r])) { - Ordering::Less => r -= 1, - Ordering::Greater => l += 1, - Ordering::Equal => break, + Ordering::Less => { + r -= 1; + } + Ordering::Greater => { + l += 1; + } + Ordering::Equal => { + break; + } } } vec![l as i32, r as i32] @@ -282,9 +288,15 @@ impl Solution { while l <= r { let mid = l + (r - l) / 2; match num.cmp(&numbers[mid]) { - Ordering::Less => r = mid - 1, - Ordering::Greater => l = mid + 1, - Ordering::Equal => return vec![i as i32, mid as i32], + Ordering::Less => { + r = mid - 1; + } + Ordering::Greater => { + l = mid + 1; + } + Ordering::Equal => { + return vec![i as i32, mid as i32]; + } } } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.rs" index 7bdcc54427501..e836ee60740e2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.rs" @@ -7,9 +7,15 @@ impl Solution { let mut r = n - 1; loop { match target.cmp(&(numbers[l] + numbers[r])) { - Ordering::Less => r -= 1, - Ordering::Greater => l += 1, - Ordering::Equal => break, + Ordering::Less => { + r -= 1; + } + Ordering::Greater => { + l += 1; + } + Ordering::Equal => { + break; + } } } vec![l as i32, r as i32] diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" index 623cb06b7f570..7151c2fcd5c7b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" @@ -364,8 +364,12 @@ impl Solution { let mut r = n - 1; while l < r { match (nums[i] + nums[l] + nums[r]).cmp(&0) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } Ordering::Equal => { res.push(vec![nums[i], nums[l], nums[r]]); l += 1; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.rs" index 087a7e86a16bc..6852a7a09d060 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.rs" @@ -1,36 +1,40 @@ -use std::cmp::Ordering; - -impl Solution { - pub fn three_sum(mut nums: Vec) -> Vec> { - nums.sort(); - let n = nums.len(); - let mut res = vec![]; - let mut i = 0; - while i < n - 2 && nums[i] <= 0 { - let mut l = i + 1; - let mut r = n - 1; - while l < r { - match (nums[i] + nums[l] + nums[r]).cmp(&0) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, - Ordering::Equal => { - res.push(vec![nums[i], nums[l], nums[r]]); - l += 1; - r -= 1; - while l < n && nums[l] == nums[l - 1] { - l += 1; - } - while r > 0 && nums[r] == nums[r + 1] { - r -= 1; - } - } - } - } - i += 1; - while i < n - 2 && nums[i] == nums[i - 1] { - i += 1; - } - } - res - } -} +use std::cmp::Ordering; + +impl Solution { + pub fn three_sum(mut nums: Vec) -> Vec> { + nums.sort(); + let n = nums.len(); + let mut res = vec![]; + let mut i = 0; + while i < n - 2 && nums[i] <= 0 { + let mut l = i + 1; + let mut r = n - 1; + while l < r { + match (nums[i] + nums[l] + nums[r]).cmp(&0) { + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } + Ordering::Equal => { + res.push(vec![nums[i], nums[l], nums[r]]); + l += 1; + r -= 1; + while l < n && nums[l] == nums[l - 1] { + l += 1; + } + while r > 0 && nums[r] == nums[r + 1] { + r -= 1; + } + } + } + } + i += 1; + while i < n - 2 && nums[i] == nums[i - 1] { + i += 1; + } + } + res + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/README.md" index b24dedf458783..b55ec7e34051f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/README.md" @@ -366,9 +366,7 @@ impl LRUCache { None => None, } } -} - -/** +}/** * Your LRUCache object will be instantiated and called as such: * let obj = LRUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/Solution.rs" index 9f2585f1482af..76b8fecb7769d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 031. \346\234\200\350\277\221\346\234\200\345\260\221\344\275\277\347\224\250\347\274\223\345\255\230/Solution.rs" @@ -1,130 +1,128 @@ -use std::cell::RefCell; -use std::collections::HashMap; -use std::rc::Rc; - -struct Node { - key: i32, - value: i32, - prev: Option>>, - next: Option>>, -} - -impl Node { - #[inline] - fn new(key: i32, value: i32) -> Self { - Self { - key, - value, - prev: None, - next: None, - } - } -} - -struct LRUCache { - capacity: usize, - cache: HashMap>>, - head: Option>>, - tail: Option>>, -} - -/** - * `&self` means the method takes an immutable reference. - * If you need a mutable reference, change it to `&mut self` instead. - */ -impl LRUCache { - fn new(capacity: i32) -> Self { - Self { - capacity: capacity as usize, - cache: HashMap::new(), - head: None, - tail: None, - } - } - - fn get(&mut self, key: i32) -> i32 { - match self.cache.get(&key) { - Some(node) => { - let node = Rc::clone(node); - self.remove(&node); - self.push_front(&node); - let value = node.borrow().value; - value - } - None => -1, - } - } - - fn put(&mut self, key: i32, value: i32) { - match self.cache.get(&key) { - Some(node) => { - let node = Rc::clone(node); - node.borrow_mut().value = value; - self.remove(&node); - self.push_front(&node); - } - None => { - let node = Rc::new(RefCell::new(Node::new(key, value))); - self.cache.insert(key, Rc::clone(&node)); - self.push_front(&node); - if self.cache.len() > self.capacity { - let back_key = self.pop_back().unwrap().borrow().key; - self.cache.remove(&back_key); - } - } - }; - } - - fn push_front(&mut self, node: &Rc>) { - match self.head.take() { - Some(head) => { - head.borrow_mut().prev = Some(Rc::clone(node)); - node.borrow_mut().prev = None; - node.borrow_mut().next = Some(head); - self.head = Some(Rc::clone(node)); - } - None => { - self.head = Some(Rc::clone(node)); - self.tail = Some(Rc::clone(node)); - } - }; - } - - fn remove(&mut self, node: &Rc>) { - match (node.borrow().prev.as_ref(), node.borrow().next.as_ref()) { - (None, None) => { - self.head = None; - self.tail = None; - } - (None, Some(next)) => { - self.head = Some(Rc::clone(next)); - next.borrow_mut().prev = None; - } - (Some(prev), None) => { - self.tail = Some(Rc::clone(prev)); - prev.borrow_mut().next = None; - } - (Some(prev), Some(next)) => { - next.borrow_mut().prev = Some(Rc::clone(prev)); - prev.borrow_mut().next = Some(Rc::clone(next)); - } - }; - } - - fn pop_back(&mut self) -> Option>> { - match self.tail.take() { - Some(tail) => { - self.remove(&tail); - Some(tail) - } - None => None, - } - } -} - -/** - * Your LRUCache object will be instantiated and called as such: - * let obj = LRUCache::new(capacity); - * let ret_1: i32 = obj.get(key); - * obj.put(key, value); - */ +use std::cell::RefCell; +use std::collections::HashMap; +use std::rc::Rc; + +struct Node { + key: i32, + value: i32, + prev: Option>>, + next: Option>>, +} + +impl Node { + #[inline] + fn new(key: i32, value: i32) -> Self { + Self { + key, + value, + prev: None, + next: None, + } + } +} + +struct LRUCache { + capacity: usize, + cache: HashMap>>, + head: Option>>, + tail: Option>>, +} + +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl LRUCache { + fn new(capacity: i32) -> Self { + Self { + capacity: capacity as usize, + cache: HashMap::new(), + head: None, + tail: None, + } + } + + fn get(&mut self, key: i32) -> i32 { + match self.cache.get(&key) { + Some(node) => { + let node = Rc::clone(node); + self.remove(&node); + self.push_front(&node); + let value = node.borrow().value; + value + } + None => -1, + } + } + + fn put(&mut self, key: i32, value: i32) { + match self.cache.get(&key) { + Some(node) => { + let node = Rc::clone(node); + node.borrow_mut().value = value; + self.remove(&node); + self.push_front(&node); + } + None => { + let node = Rc::new(RefCell::new(Node::new(key, value))); + self.cache.insert(key, Rc::clone(&node)); + self.push_front(&node); + if self.cache.len() > self.capacity { + let back_key = self.pop_back().unwrap().borrow().key; + self.cache.remove(&back_key); + } + } + }; + } + + fn push_front(&mut self, node: &Rc>) { + match self.head.take() { + Some(head) => { + head.borrow_mut().prev = Some(Rc::clone(node)); + node.borrow_mut().prev = None; + node.borrow_mut().next = Some(head); + self.head = Some(Rc::clone(node)); + } + None => { + self.head = Some(Rc::clone(node)); + self.tail = Some(Rc::clone(node)); + } + }; + } + + fn remove(&mut self, node: &Rc>) { + match (node.borrow().prev.as_ref(), node.borrow().next.as_ref()) { + (None, None) => { + self.head = None; + self.tail = None; + } + (None, Some(next)) => { + self.head = Some(Rc::clone(next)); + next.borrow_mut().prev = None; + } + (Some(prev), None) => { + self.tail = Some(Rc::clone(prev)); + prev.borrow_mut().next = None; + } + (Some(prev), Some(next)) => { + next.borrow_mut().prev = Some(Rc::clone(prev)); + prev.borrow_mut().next = Some(Rc::clone(next)); + } + }; + } + + fn pop_back(&mut self) -> Option>> { + match self.tail.take() { + Some(tail) => { + self.remove(&tail); + Some(tail) + } + None => None, + } + } +}/** + * Your LRUCache object will be instantiated and called as such: + * let obj = LRUCache::new(capacity); + * let ret_1: i32 = obj.get(key); + * obj.put(key, value); + */ diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs" index bd3c7e88457d9..a92f4decd6bd0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs" @@ -1,21 +1,21 @@ -impl Solution { - #[allow(dead_code)] - pub fn asteroid_collision(asteroids: Vec) -> Vec { - let mut stk = Vec::new(); - for &x in &asteroids { - if x > 0 { - stk.push(x); - } else { - while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x { - stk.pop(); - } - if !stk.is_empty() && *stk.last().unwrap() == -x { - stk.pop(); - } else if stk.is_empty() || *stk.last().unwrap() < 0 { - stk.push(x); - } - } - } - stk - } -} \ No newline at end of file +impl Solution { + #[allow(dead_code)] + pub fn asteroid_collision(asteroids: Vec) -> Vec { + let mut stk = Vec::new(); + for &x in &asteroids { + if x > 0 { + stk.push(x); + } else { + while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x { + stk.pop(); + } + if !stk.is_empty() && *stk.last().unwrap() == -x { + stk.pop(); + } else if stk.is_empty() || *stk.last().unwrap() < 0 { + stk.push(x); + } + } + } + stk + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" index ac54bcd7fd83b..10759b524ab28 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" @@ -262,11 +262,7 @@ impl Solution { while !stack.is_empty() && temperatures[*stack.last().unwrap()] <= temperatures[i] { stack.pop(); } - res[i] = if stack.is_empty() { - 0 - } else { - (stack.last().unwrap() - i) as i32 - }; + res[i] = if stack.is_empty() { 0 } else { (stack.last().unwrap() - i) as i32 }; stack.push(i); } res diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/README.md" index 2753e3d68b873..2dec89f03201a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/README.md" @@ -329,8 +329,8 @@ impl Solution { return 0; } let node = root.as_ref().unwrap().borrow(); - let left = 0.max(Self::dfs(&node.left, res)); - let right = 0.max(Self::dfs(&node.right, res)); + let left = (0).max(Self::dfs(&node.left, res)); + let right = (0).max(Self::dfs(&node.right, res)); *res = (node.val + left + right).max(*res); node.val + left.max(right) } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/Solution.rs" index 7e990e669f77b..5b24aa51d2e32 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 051. \350\212\202\347\202\271\344\271\213\345\222\214\346\234\200\345\244\247\347\232\204\350\267\257\345\276\204/Solution.rs" @@ -1,38 +1,38 @@ -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::rc::Rc; -use std::cell::RefCell; -impl Solution { - fn dfs(root: &Option>>, res: &mut i32) -> i32 { - if root.is_none() { - return 0; - } - let node = root.as_ref().unwrap().borrow(); - let left = 0.max(Self::dfs(&node.left, res)); - let right = 0.max(Self::dfs(&node.right, res)); - *res = (node.val + left + right).max(*res); - node.val + left.max(right) - } - - pub fn max_path_sum(root: Option>>) -> i32 { - let mut res = -1000; - Self::dfs(&root, &mut res); - res - } -} +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + fn dfs(root: &Option>>, res: &mut i32) -> i32 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + let left = (0).max(Self::dfs(&node.left, res)); + let right = (0).max(Self::dfs(&node.right, res)); + *res = (node.val + left + right).max(*res); + node.val + left.max(right) + } + + pub fn max_path_sum(root: Option>>) -> i32 { + let mut res = -1000; + Self::dfs(&root, &mut res); + res + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/README.md" index 4d75b68265fb3..faaaad51d41ff 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/README.md" @@ -400,11 +400,15 @@ impl Solution { let mut dummy = Rc::new(RefCell::new(TreeNode::new(0))); for &val in vals.iter().rev() { let mut dummy = dummy.as_ref().borrow_mut(); - dummy.right = Some(Rc::new(RefCell::new(TreeNode { - val, - left: None, - right: dummy.right.take(), - }))); + dummy.right = Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: None, + right: dummy.right.take(), + }) + ) + ); } let ans = dummy.as_ref().borrow_mut().right.take(); ans diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/Solution.rs" index e19a3d3d327ec..a8cdfc977c7a6 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 052. \345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/Solution.rs" @@ -35,11 +35,15 @@ impl Solution { let mut dummy = Rc::new(RefCell::new(TreeNode::new(0))); for &val in vals.iter().rev() { let mut dummy = dummy.as_ref().borrow_mut(); - dummy.right = Some(Rc::new(RefCell::new(TreeNode { - val, - left: None, - right: dummy.right.take(), - }))); + dummy.right = Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: None, + right: dummy.right.take(), + }) + ) + ); } let ans = dummy.as_ref().borrow_mut().right.take(); ans diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/README.md" index b1ba1c42ec93c..a52123ab656c9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/README.md" @@ -512,10 +512,9 @@ class BSTIterator { use std::rc::Rc; use std::cell::RefCell; struct BSTIterator { - stack: Vec + stack: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -536,7 +535,7 @@ impl BSTIterator { let mut stack = Vec::new(); Self::dfs(&root, &mut stack); Self { - stack + stack, } } @@ -547,9 +546,7 @@ impl BSTIterator { fn has_next(&self) -> bool { !self.stack.is_empty() } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.rs" index 6deb3ef864f81..9380f07ddd8f2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.rs" @@ -17,12 +17,11 @@ // } // } use std::rc::Rc; -use std::cell::RefCell; +use std::cell::RefCell; struct BSTIterator { - stack: Vec + stack: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -43,7 +42,7 @@ impl BSTIterator { let mut stack = Vec::new(); Self::dfs(&root, &mut stack); Self { - stack + stack, } } @@ -54,9 +53,7 @@ impl BSTIterator { fn has_next(&self) -> bool { !self.stack.is_empty() } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/README.md" index 28a80f2eec979..f1a144afff175 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/README.md" @@ -216,7 +216,7 @@ impl Solution { pub fn rob(nums: Vec) -> i32 { let mut f = [0, 0]; for x in nums { - f = [f[0].max(f[1]), f[0] + x] + f = [f[0].max(f[1]), f[0] + x]; } f[0].max(f[1]) } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" index fe67b113421c0..d1b3a459ce727 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 089. \346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" @@ -1,9 +1,9 @@ -impl Solution { - pub fn rob(nums: Vec) -> i32 { - let mut f = [0, 0]; - for x in nums { - f = [f[0].max(f[1]), f[0] + x] - } - f[0].max(f[1]) - } -} +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let mut f = [0, 0]; + for x in nums { + f = [f[0].max(f[1]), f[0] + x]; + } + f[0].max(f[1]) + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 090. \347\216\257\345\275\242\346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 090. \347\216\257\345\275\242\346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" index 454f81de045b3..ae3dd2c86ab89 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 090. \347\216\257\345\275\242\346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 090. \347\216\257\345\275\242\346\210\277\345\261\213\345\201\267\347\233\227/Solution.rs" @@ -1,16 +1,16 @@ -impl Solution { - pub fn rob(nums: Vec) -> i32 { - let n = nums.len(); - if n == 1 { - return nums[0]; - } - let rob_range = |l, r| { - let mut f = [0, 0]; - for i in l..r { - f = [f[0].max(f[1]), f[0] + nums[i]]; - } - f[0].max(f[1]) - }; - rob_range(0, n - 1).max(rob_range(1, n)) - } -} +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let n = nums.len(); + if n == 1 { + return nums[0]; + } + let rob_range = |l, r| { + let mut f = [0, 0]; + for i in l..r { + f = [f[0].max(f[1]), f[0] + nums[i]]; + } + f[0].max(f[1]) + }; + rob_range(0, n - 1).max(rob_range(1, n)) + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.rs" index 86f961eda5e51..78db47309d9ff 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.rs" @@ -1,17 +1,17 @@ -impl Solution { - pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { - let (m, n) = (text1.len(), text2.len()); - let (text1, text2) = (text1.as_bytes(), text2.as_bytes()); - let mut f = vec![vec![0; n + 1]; m + 1]; - for i in 1..=m { - for j in 1..=n { - f[i][j] = if text1[i - 1] == text2[j - 1] { - f[i - 1][j - 1] + 1 - } else { - f[i - 1][j].max(f[i][j - 1]) - } - } - } - f[m][n] - } -} +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let (m, n) = (text1.len(), text2.len()); + let (text1, text2) = (text1.as_bytes(), text2.as_bytes()); + let mut f = vec![vec![0; n + 1]; m + 1]; + for i in 1..=m { + for j in 1..=n { + f[i][j] = if text1[i - 1] == text2[j - 1] { + f[i - 1][j - 1] + 1 + } else { + f[i - 1][j].max(f[i][j - 1]) + }; + } + } + f[m][n] + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.rs" index cc9dc4ad2d9a5..4481cea587b7d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.rs" @@ -1,12 +1,12 @@ -impl Solution { - pub fn unique_paths(m: i32, n: i32) -> i32 { - let (m, n) = (m as usize, n as usize); - let mut f = vec![1; n]; - for i in 1..m { - for j in 1..n { - f[j] += f[j - 1]; - } - } - f[n - 1] - } -} +impl Solution { + pub fn unique_paths(m: i32, n: i32) -> i32 { + let (m, n) = (m as usize, n as usize); + let mut f = vec![1; n]; + for i in 1..m { + for j in 1..n { + f[j] += f[j - 1]; + } + } + f[n - 1] + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" index 95e4902bc9f1d..b0a437ea829dd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" @@ -223,10 +223,10 @@ impl Solution { grid[i][j] = 0; let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); if i != 0 { - res += Self::dfs(grid, i - 1, j) + res += Self::dfs(grid, i - 1, j); } if j != 0 { - res += Self::dfs(grid, i, j - 1) + res += Self::dfs(grid, i, j - 1); } res } @@ -237,7 +237,7 @@ impl Solution { let mut res = 0; for i in 0..m { for j in 0..n { - res = res.max(Self::dfs(&mut grid, i, j)) + res = res.max(Self::dfs(&mut grid, i, j)); } } res diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.rs" index 40779eb7bc3ea..a1882b0146834 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.rs" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.rs" @@ -1,28 +1,28 @@ -impl Solution { - fn dfs(grid: &mut Vec>, i: usize, j: usize) -> i32 { - if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 { - return 0; - } - grid[i][j] = 0; - let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); - if i != 0 { - res += Self::dfs(grid, i - 1, j) - } - if j != 0 { - res += Self::dfs(grid, i, j - 1) - } - res - } - - pub fn max_area_of_island(mut grid: Vec>) -> i32 { - let m = grid.len(); - let n = grid[0].len(); - let mut res = 0; - for i in 0..m { - for j in 0..n { - res = res.max(Self::dfs(&mut grid, i, j)) - } - } - res - } -} +impl Solution { + fn dfs(grid: &mut Vec>, i: usize, j: usize) -> i32 { + if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 { + return 0; + } + grid[i][j] = 0; + let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); + if i != 0 { + res += Self::dfs(grid, i - 1, j); + } + if j != 0 { + res += Self::dfs(grid, i, j - 1); + } + res + } + + pub fn max_area_of_island(mut grid: Vec>) -> i32 { + let m = grid.len(); + let n = grid[0].len(); + let mut res = 0; + for i in 0..m { + for j in 0..n { + res = res.max(Self::dfs(&mut grid, i, j)); + } + } + res + } +} diff --git "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" index 0612321f5df16..c1d6e93a234f9 100644 --- "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" +++ "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" @@ -114,7 +114,10 @@ function minCount(coins: number[]): number { ```rust impl Solution { pub fn min_count(coins: Vec) -> i32 { - coins.iter().map(|&x| (x + 1) >> 1).sum::() + coins + .iter() + .map(|&x| (x + 1) >> 1) + .sum::() } } ``` diff --git "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.rs" "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.rs" index a7e6073350592..a2a576e71ac40 100644 --- "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.rs" +++ "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.rs" @@ -1,5 +1,8 @@ -impl Solution { - pub fn min_count(coins: Vec) -> i32 { - coins.iter().map(|&x| (x + 1) >> 1).sum::() - } -} \ No newline at end of file +impl Solution { + pub fn min_count(coins: Vec) -> i32 { + coins + .iter() + .map(|&x| (x + 1) >> 1) + .sum::() + } +} diff --git a/package-lock.json b/package-lock.json index b7a71c1fcb60c..bf07421f8a211 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "husky": "^8.0.3", "lint-staged": "^13.2.2", "prettier": "^2.8.8", + "prettier-plugin-rust": "^0.1.9", "prettier-plugin-sql-cst": "^0.8.2" } }, @@ -2706,6 +2707,12 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/jinx-rust": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/jinx-rust/-/jinx-rust-0.1.6.tgz", + "integrity": "sha512-qP+wtQL1PrDDFwtPKhNGtjWOmijCrKdfUHWTV2G/ikxfjrh+cjdvkQTmny9RAsVF0jiui9m+F0INWu4cuRcZeQ==", + "dev": true + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", @@ -3870,6 +3877,16 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/prettier-plugin-rust": { + "version": "0.1.9", + "resolved": "https://registry.npmjs.org/prettier-plugin-rust/-/prettier-plugin-rust-0.1.9.tgz", + "integrity": "sha512-n1DTTJQaHMdnoG/+nKUvBm3EKsMVWsYES2UPCiOPiZdBrmuAO/pX++m7L3+Hz3uuhtddpH0HRKHB2F3jbtJBOQ==", + "dev": true, + "dependencies": { + "jinx-rust": "0.1.6", + "prettier": "^2.7.1" + } + }, "node_modules/prettier-plugin-sql-cst": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/prettier-plugin-sql-cst/-/prettier-plugin-sql-cst-0.8.2.tgz", @@ -7329,6 +7346,12 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "jinx-rust": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/jinx-rust/-/jinx-rust-0.1.6.tgz", + "integrity": "sha512-qP+wtQL1PrDDFwtPKhNGtjWOmijCrKdfUHWTV2G/ikxfjrh+cjdvkQTmny9RAsVF0jiui9m+F0INWu4cuRcZeQ==", + "dev": true + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", @@ -8216,6 +8239,16 @@ "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", "dev": true }, + "prettier-plugin-rust": { + "version": "0.1.9", + "resolved": "https://registry.npmjs.org/prettier-plugin-rust/-/prettier-plugin-rust-0.1.9.tgz", + "integrity": "sha512-n1DTTJQaHMdnoG/+nKUvBm3EKsMVWsYES2UPCiOPiZdBrmuAO/pX++m7L3+Hz3uuhtddpH0HRKHB2F3jbtJBOQ==", + "dev": true, + "requires": { + "jinx-rust": "0.1.6", + "prettier": "^2.7.1" + } + }, "prettier-plugin-sql-cst": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/prettier-plugin-sql-cst/-/prettier-plugin-sql-cst-0.8.2.tgz", diff --git a/package.json b/package.json index d36dc799936dd..22cd07e7a2c07 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,11 @@ "husky": "^8.0.3", "lint-staged": "^13.2.2", "prettier": "^2.8.8", + "prettier-plugin-rust": "^0.1.9", "prettier-plugin-sql-cst": "^0.8.2" }, "lint-staged": { - "*.{js,ts,php,md,sql}": "prettier --write", + "*.{js,ts,php,sql,rs,md}": "prettier --write", "*.py": "black -S" } } diff --git a/run_format.py b/run_format.py index 63a2bc20b4821..f2f2f9abb4eef 100644 --- a/run_format.py +++ b/run_format.py @@ -330,7 +330,7 @@ def run(): os.system(f'npx clang-format -i --style=file "{path}"') # format with prettier - os.system('npx prettier --write "**/*.{md,js,ts,php,sql}"') + os.system('npx prettier --write "**/*.{js,ts,php,sql,rs,md}"') # format with gofmt os.system("gofmt -w .") diff --git a/solution/0000-0099/0001.Two Sum/Solution.rs b/solution/0000-0099/0001.Two Sum/Solution.rs index e733a65e674cf..d7b994288ccd5 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.rs +++ b/solution/0000-0099/0001.Two Sum/Solution.rs @@ -13,4 +13,4 @@ impl Solution { } unreachable!() } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 6209685dacde3..d6254efb74e06 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -409,7 +409,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = &mut dummy; diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index 0c4d5bc294343..4e31b588358df 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -397,7 +397,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = &mut dummy; diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.rs b/solution/0000-0099/0002.Add Two Numbers/Solution.rs index d583581839409..d5fb81cb7ec7b 100644 --- a/solution/0000-0099/0002.Add Two Numbers/Solution.rs +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.rs @@ -17,7 +17,7 @@ impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { let mut dummy = Some(Box::new(ListNode::new(0))); let mut cur = &mut dummy; diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index ab7ff7ce9f5af..497814a323c11 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -333,7 +333,8 @@ impl Solution { let s = s.as_bytes(); let mut set = HashSet::new(); let mut i = 0; - s.iter() + s + .iter() .map(|c| { while set.contains(&c) { set.remove(&s[i]); diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index 1faca599daad1..16d6fa6943582 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -323,7 +323,8 @@ impl Solution { let s = s.as_bytes(); let mut set = HashSet::new(); let mut i = 0; - s.iter() + s + .iter() .map(|c| { while set.contains(&c) { set.remove(&s[i]); diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.rs b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.rs index 7403420380743..bf4cd2ef4a301 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.rs +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.rs @@ -5,7 +5,8 @@ impl Solution { let s = s.as_bytes(); let mut set = HashSet::new(); let mut i = 0; - s.iter() + s + .iter() .map(|c| { while set.contains(&c) { set.remove(&s[i]); diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README.md b/solution/0000-0099/0005.Longest Palindromic Substring/README.md index adb7c56508ade..f3e895b5a171a 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README.md @@ -363,7 +363,7 @@ impl Solution { for start in 0..=end { if data[start] == data[end] { dp[start][end] = end - start < 2 || dp[start + 1][end - 1]; - if dp[start][end] && (end - start + 1) > ans.len() { + if dp[start][end] && end - start + 1 > ans.len() { ans = &s[start..=end]; } } diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md index f2ff9ef24195a..924ffdfb82e22 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md +++ b/solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md @@ -348,7 +348,7 @@ impl Solution { for start in 0..=end { if data[start] == data[end] { dp[start][end] = end - start < 2 || dp[start + 1][end - 1]; - if dp[start][end] && (end - start + 1) > ans.len() { + if dp[start][end] && end - start + 1 > ans.len() { ans = &s[start..=end]; } } diff --git a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.rs b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.rs index 19e99c1b39b60..387be09115e91 100644 --- a/solution/0000-0099/0005.Longest Palindromic Substring/Solution.rs +++ b/solution/0000-0099/0005.Longest Palindromic Substring/Solution.rs @@ -8,7 +8,7 @@ impl Solution { for start in 0..=end { if data[start] == data[end] { dp[start][end] = end - start < 2 || dp[start + 1][end - 1]; - if dp[start][end] && (end - start + 1) > ans.len() { + if dp[start][end] && end - start + 1 > ans.len() { ans = &s[start..=end]; } } @@ -17,4 +17,3 @@ impl Solution { ans.to_string() } } - diff --git a/solution/0000-0099/0007.Reverse Integer/README.md b/solution/0000-0099/0007.Reverse Integer/README.md index 15352da6b9909..2479e7e597128 100644 --- a/solution/0000-0099/0007.Reverse Integer/README.md +++ b/solution/0000-0099/0007.Reverse Integer/README.md @@ -190,15 +190,8 @@ int reverse(int x) { impl Solution { pub fn reverse(mut x: i32) -> i32 { let is_minus = x < 0; - match x - .abs() - .to_string() - .chars() - .rev() - .collect::() - .parse::() - { - Ok(x) => x * if is_minus { -1 } else { 1 }, + match x.abs().to_string().chars().rev().collect::().parse::() { + Ok(x) => x * (if is_minus { -1 } else { 1 }), Err(_) => 0, } } diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index 773f04572aba7..aa8413064f71a 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -172,15 +172,8 @@ int reverse(int x) { impl Solution { pub fn reverse(mut x: i32) -> i32 { let is_minus = x < 0; - match x - .abs() - .to_string() - .chars() - .rev() - .collect::() - .parse::() - { - Ok(x) => x * if is_minus { -1 } else { 1 }, + match x.abs().to_string().chars().rev().collect::().parse::() { + Ok(x) => x * (if is_minus { -1 } else { 1 }), Err(_) => 0, } } diff --git a/solution/0000-0099/0007.Reverse Integer/Solution.rs b/solution/0000-0099/0007.Reverse Integer/Solution.rs index 7f396611307c9..425ec8fe15e28 100644 --- a/solution/0000-0099/0007.Reverse Integer/Solution.rs +++ b/solution/0000-0099/0007.Reverse Integer/Solution.rs @@ -1,15 +1,8 @@ impl Solution { pub fn reverse(mut x: i32) -> i32 { let is_minus = x < 0; - match x - .abs() - .to_string() - .chars() - .rev() - .collect::() - .parse::() - { - Ok(x) => x * if is_minus { -1 } else { 1 }, + match x.abs().to_string().chars().rev().collect::().parse::() { + Ok(x) => x * (if is_minus { -1 } else { 1 }), Err(_) => 0, } } diff --git a/solution/0000-0099/0010.Regular Expression Matching/Solution.rs b/solution/0000-0099/0010.Regular Expression Matching/Solution.rs index bc77367d06801..22a554739476f 100644 --- a/solution/0000-0099/0010.Regular Expression Matching/Solution.rs +++ b/solution/0000-0099/0010.Regular Expression Matching/Solution.rs @@ -35,4 +35,4 @@ impl Solution { dp[n][m] } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0011.Container With Most Water/README.md b/solution/0000-0099/0011.Container With Most Water/README.md index 387ab9e2becd3..0cc9b5a2414c0 100644 --- a/solution/0000-0099/0011.Container With Most Water/README.md +++ b/solution/0000-0099/0011.Container With Most Water/README.md @@ -214,7 +214,7 @@ impl Solution { let mut j = height.len() - 1; let mut res = 0; while i < j { - res = res.max(height[i].min(height[j]) * (j - i) as i32); + res = res.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { diff --git a/solution/0000-0099/0011.Container With Most Water/README_EN.md b/solution/0000-0099/0011.Container With Most Water/README_EN.md index 804e41ebeb219..2f778ed5afbea 100644 --- a/solution/0000-0099/0011.Container With Most Water/README_EN.md +++ b/solution/0000-0099/0011.Container With Most Water/README_EN.md @@ -193,7 +193,7 @@ impl Solution { let mut j = height.len() - 1; let mut res = 0; while i < j { - res = res.max(height[i].min(height[j]) * (j - i) as i32); + res = res.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.rs b/solution/0000-0099/0011.Container With Most Water/Solution.rs index 425f0382fc028..29ad6eb63d77a 100644 --- a/solution/0000-0099/0011.Container With Most Water/Solution.rs +++ b/solution/0000-0099/0011.Container With Most Water/Solution.rs @@ -4,7 +4,7 @@ impl Solution { let mut j = height.len() - 1; let mut res = 0; while i < j { - res = res.max(height[i].min(height[j]) * (j - i) as i32); + res = res.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { diff --git a/solution/0000-0099/0014.Longest Common Prefix/README.md b/solution/0000-0099/0014.Longest Common Prefix/README.md index e206114f081ae..3eca57184aa0a 100644 --- a/solution/0000-0099/0014.Longest Common Prefix/README.md +++ b/solution/0000-0099/0014.Longest Common Prefix/README.md @@ -206,7 +206,11 @@ function longestCommonPrefix(strs: string[]): string { ```rust impl Solution { pub fn longest_common_prefix(strs: Vec) -> String { - let mut len = strs.iter().map(|s| s.len()).min().unwrap(); + let mut len = strs + .iter() + .map(|s| s.len()) + .min() + .unwrap(); for i in (1..=len).rev() { let mut is_equal = true; let target = strs[0][0..i].to_string(); diff --git a/solution/0000-0099/0014.Longest Common Prefix/README_EN.md b/solution/0000-0099/0014.Longest Common Prefix/README_EN.md index 890f110a7d9a7..d8f81dce4fd45 100644 --- a/solution/0000-0099/0014.Longest Common Prefix/README_EN.md +++ b/solution/0000-0099/0014.Longest Common Prefix/README_EN.md @@ -197,7 +197,11 @@ function longestCommonPrefix(strs: string[]): string { ```rust impl Solution { pub fn longest_common_prefix(strs: Vec) -> String { - let mut len = strs.iter().map(|s| s.len()).min().unwrap(); + let mut len = strs + .iter() + .map(|s| s.len()) + .min() + .unwrap(); for i in (1..=len).rev() { let mut is_equal = true; let target = strs[0][0..i].to_string(); diff --git a/solution/0000-0099/0014.Longest Common Prefix/Solution.rs b/solution/0000-0099/0014.Longest Common Prefix/Solution.rs index 3846ae51e6375..4d73b28f946a6 100644 --- a/solution/0000-0099/0014.Longest Common Prefix/Solution.rs +++ b/solution/0000-0099/0014.Longest Common Prefix/Solution.rs @@ -1,6 +1,10 @@ impl Solution { pub fn longest_common_prefix(strs: Vec) -> String { - let mut len = strs.iter().map(|s| s.len()).min().unwrap(); + let mut len = strs + .iter() + .map(|s| s.len()) + .min() + .unwrap(); for i in (1..=len).rev() { let mut is_equal = true; let target = strs[0][0..i].to_string(); diff --git a/solution/0000-0099/0015.3Sum/README.md b/solution/0000-0099/0015.3Sum/README.md index a74973b0bef4f..5ac5cbab03bce 100644 --- a/solution/0000-0099/0015.3Sum/README.md +++ b/solution/0000-0099/0015.3Sum/README.md @@ -376,8 +376,12 @@ impl Solution { let mut r = n - 1; while l < r { match (nums[i] + nums[l] + nums[r]).cmp(&0) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } Ordering::Equal => { res.push(vec![nums[i], nums[l], nums[r]]); l += 1; diff --git a/solution/0000-0099/0015.3Sum/README_EN.md b/solution/0000-0099/0015.3Sum/README_EN.md index 2585d71168243..b27095b114987 100644 --- a/solution/0000-0099/0015.3Sum/README_EN.md +++ b/solution/0000-0099/0015.3Sum/README_EN.md @@ -362,8 +362,12 @@ impl Solution { let mut r = n - 1; while l < r { match (nums[i] + nums[l] + nums[r]).cmp(&0) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } Ordering::Equal => { res.push(vec![nums[i], nums[l], nums[r]]); l += 1; diff --git a/solution/0000-0099/0015.3Sum/Solution.rs b/solution/0000-0099/0015.3Sum/Solution.rs index 140f590ec1913..6852a7a09d060 100644 --- a/solution/0000-0099/0015.3Sum/Solution.rs +++ b/solution/0000-0099/0015.3Sum/Solution.rs @@ -11,8 +11,12 @@ impl Solution { let mut r = n - 1; while l < r { match (nums[i] + nums[l] + nums[r]).cmp(&0) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } Ordering::Equal => { res.push(vec![nums[i], nums[l], nums[r]]); l += 1; diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md index e6cca54dbd8d7..7f35c1ce3ffe7 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md @@ -280,7 +280,7 @@ impl Solution { s: &mut String, cs: &Vec, map: &HashMap, - res: &mut Vec, + res: &mut Vec ) { if i == cs.len() { res.push(s.clone()); @@ -309,13 +309,7 @@ impl Solution { map.insert('8', String::from("tuv")); map.insert('9', String::from("wxyz")); - Self::dfs( - 0, - &mut String::new(), - &digits.chars().collect(), - &map, - &mut res, - ); + Self::dfs(0, &mut String::new(), &digits.chars().collect(), &map, &mut res); res } } @@ -348,7 +342,7 @@ impl Solution { vec!['m', 'n', 'o'], vec!['p', 'q', 'r', 's'], vec!['t', 'u', 'v'], - vec!['w', 'x', 'y', 'z'], + vec!['w', 'x', 'y', 'z'] ]; let mut res = Vec::new(); Self::dfs(0, digits, &map, &mut String::new(), &mut res); diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index fabc33be33963..5d9f013595969 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -262,7 +262,7 @@ impl Solution { s: &mut String, cs: &Vec, map: &HashMap, - res: &mut Vec, + res: &mut Vec ) { if i == cs.len() { res.push(s.clone()); @@ -291,13 +291,7 @@ impl Solution { map.insert('8', String::from("tuv")); map.insert('9', String::from("wxyz")); - Self::dfs( - 0, - &mut String::new(), - &digits.chars().collect(), - &map, - &mut res, - ); + Self::dfs(0, &mut String::new(), &digits.chars().collect(), &map, &mut res); res } } @@ -330,7 +324,7 @@ impl Solution { vec!['m', 'n', 'o'], vec!['p', 'q', 'r', 's'], vec!['t', 'u', 'v'], - vec!['w', 'x', 'y', 'z'], + vec!['w', 'x', 'y', 'z'] ]; let mut res = Vec::new(); Self::dfs(0, digits, &map, &mut String::new(), &mut res); diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.rs b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.rs index f31a5cacd552c..1c8221c075606 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.rs +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.rs @@ -24,7 +24,7 @@ impl Solution { vec!['m', 'n', 'o'], vec!['p', 'q', 'r', 's'], vec!['t', 'u', 'v'], - vec!['w', 'x', 'y', 'z'], + vec!['w', 'x', 'y', 'z'] ]; let mut res = Vec::new(); Self::dfs(0, digits, &map, &mut String::new(), &mut res); diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/README.md b/solution/0000-0099/0021.Merge Two Sorted Lists/README.md index 3eb2407f5e9fb..b854f145e8067 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/README.md +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/README.md @@ -509,7 +509,7 @@ function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode impl Solution { pub fn merge_two_lists( list1: Option>, - list2: Option>, + list2: Option> ) -> Option> { match (list1, list2) { (None, None) => None, @@ -549,7 +549,7 @@ impl Solution { impl Solution { pub fn merge_two_lists( mut list1: Option>, - mut list2: Option>, + mut list2: Option> ) -> Option> { let mut new_list = ListNode::new(0); let mut cur = &mut new_list; diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md b/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md index ad05f2dfcfecd..55b6157e44de2 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md @@ -485,7 +485,7 @@ function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode impl Solution { pub fn merge_two_lists( list1: Option>, - list2: Option>, + list2: Option> ) -> Option> { match (list1, list2) { (None, None) => None, @@ -525,7 +525,7 @@ impl Solution { impl Solution { pub fn merge_two_lists( mut list1: Option>, - mut list2: Option>, + mut list2: Option> ) -> Option> { let mut new_list = ListNode::new(0); let mut cur = &mut new_list; diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.rs b/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.rs index 481056c2ddb0a..ef4c95dbafb61 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.rs +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.rs @@ -17,7 +17,7 @@ impl Solution { pub fn merge_two_lists( list1: Option>, - list2: Option>, + list2: Option> ) -> Option> { match (list1, list2) { (None, None) => None, diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/README.md b/solution/0000-0099/0023.Merge k Sorted Lists/README.md index febf695e45284..c4453f45d02e1 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/README.md +++ b/solution/0000-0099/0023.Merge k Sorted Lists/README.md @@ -346,7 +346,10 @@ impl Ord for ListNode { } impl Solution { pub fn merge_k_lists(lists: Vec>>) -> Option> { - let mut pq = lists.into_iter().filter_map(|head| head).collect::>(); + let mut pq = lists + .into_iter() + .filter_map(|head| head) + .collect::>(); let mut head = None; let mut cur = &mut head; while let Some(node) = pq.pop() { diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md index e64071ecc0ecb..479fadbc8b8ea 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md +++ b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md @@ -333,7 +333,10 @@ impl Ord for ListNode { } impl Solution { pub fn merge_k_lists(lists: Vec>>) -> Option> { - let mut pq = lists.into_iter().filter_map(|head| head).collect::>(); + let mut pq = lists + .into_iter() + .filter_map(|head| head) + .collect::>(); let mut head = None; let mut cur = &mut head; while let Some(node) = pq.pop() { diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.rs b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.rs index 77605ef48ef9c..82a70e5564263 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.rs +++ b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.rs @@ -1,43 +1,46 @@ -// Definition for singly-linked list. -// #[derive(PartialEq, Eq, Clone, Debug)] -// pub struct ListNode { -// pub val: i32, -// pub next: Option> -// } -// -// impl ListNode { -// #[inline] -// fn new(val: i32) -> Self { -// ListNode { -// next: None, -// val -// } -// } -// } -use std::cmp::Ordering; -use std::collections::BinaryHeap; - -impl PartialOrd for ListNode { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -impl Ord for ListNode { - fn cmp(&self, other: &Self) -> Ordering { - self.val.cmp(&other.val).reverse() - } -} -impl Solution { - pub fn merge_k_lists(lists: Vec>>) -> Option> { - let mut pq = lists.into_iter().filter_map(|head| head).collect::>(); - let mut head = None; - let mut cur = &mut head; - while let Some(node) = pq.pop() { - cur = &mut cur.insert(Box::new(ListNode::new(node.val))).next; - if let Some(next) = node.next { - pq.push(next); - } - } - head - } -} \ No newline at end of file +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +use std::cmp::Ordering; +use std::collections::BinaryHeap; + +impl PartialOrd for ListNode { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} +impl Ord for ListNode { + fn cmp(&self, other: &Self) -> Ordering { + self.val.cmp(&other.val).reverse() + } +} +impl Solution { + pub fn merge_k_lists(lists: Vec>>) -> Option> { + let mut pq = lists + .into_iter() + .filter_map(|head| head) + .collect::>(); + let mut head = None; + let mut cur = &mut head; + while let Some(node) = pq.pop() { + cur = &mut cur.insert(Box::new(ListNode::new(node.val))).next; + if let Some(next) = node.next { + pq.push(next); + } + } + head + } +} diff --git a/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.rs b/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.rs index ae42793ff3c6c..18479b1289ca4 100644 --- a/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.rs +++ b/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.rs @@ -1,55 +1,55 @@ -// Definition for singly-linked list. -// #[derive(PartialEq, Eq, Clone, Debug)] -// pub struct ListNode { -// pub val: i32, -// pub next: Option> -// } -// -// impl ListNode { -// #[inline] -// fn new(val: i32) -> Self { -// ListNode { -// next: None, -// val -// } -// } -// } -impl Solution { - pub fn reverse_k_group(head: Option>, k: i32) -> Option> { - fn reverse(head: Option>) -> Option> { - let mut head = head; - let mut pre = None; - while let Some(mut node) = head { - head = node.next.take(); - node.next = pre.take(); - pre = Some(node); - } - pre - } - - let mut dummy = Some(Box::new(ListNode::new(0))); - let mut pre = &mut dummy; - let mut cur = head; - while cur.is_some() { - let mut q = &mut cur; - for _ in 0..k - 1 { - if q.is_none() { - break; - } - q = &mut q.as_mut().unwrap().next; - } - if q.is_none() { - pre.as_mut().unwrap().next = cur; - return dummy.unwrap().next; - } - - let b = q.as_mut().unwrap().next.take(); - pre.as_mut().unwrap().next = reverse(cur); - while pre.is_some() && pre.as_mut().unwrap().next.is_some() { - pre = &mut pre.as_mut().unwrap().next; - } - cur = b; - } - dummy.unwrap().next - } -} +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reverse_k_group(head: Option>, k: i32) -> Option> { + fn reverse(head: Option>) -> Option> { + let mut head = head; + let mut pre = None; + while let Some(mut node) = head { + head = node.next.take(); + node.next = pre.take(); + pre = Some(node); + } + pre + } + + let mut dummy = Some(Box::new(ListNode::new(0))); + let mut pre = &mut dummy; + let mut cur = head; + while cur.is_some() { + let mut q = &mut cur; + for _ in 0..k - 1 { + if q.is_none() { + break; + } + q = &mut q.as_mut().unwrap().next; + } + if q.is_none() { + pre.as_mut().unwrap().next = cur; + return dummy.unwrap().next; + } + + let b = q.as_mut().unwrap().next.take(); + pre.as_mut().unwrap().next = reverse(cur); + while pre.is_some() && pre.as_mut().unwrap().next.is_some() { + pre = &mut pre.as_mut().unwrap().next; + } + cur = b; + } + dummy.unwrap().next + } +} diff --git a/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.rs b/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.rs index 23797f1d03e1b..9ce78291951a3 100644 --- a/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.rs +++ b/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.rs @@ -9,4 +9,4 @@ impl Solution { } k as i32 } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0027.Remove Element/Solution.rs b/solution/0000-0099/0027.Remove Element/Solution.rs index 9a317c76d0d53..939ea417073f6 100644 --- a/solution/0000-0099/0027.Remove Element/Solution.rs +++ b/solution/0000-0099/0027.Remove Element/Solution.rs @@ -9,4 +9,4 @@ impl Solution { } k as i32 } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md index 5763f2f1203bb..30dcbe33d77f8 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md @@ -221,7 +221,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l <= r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == target { return mid as i32; } diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md index f0e62c35bd3e0..a8378bc2d17cc 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md @@ -188,7 +188,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l <= r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == target { return mid as i32; } diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.rs b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.rs index 81a767465598d..80d463f412c2c 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.rs +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.rs @@ -3,7 +3,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l <= r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == target { return mid as i32; } diff --git a/solution/0000-0099/0035.Search Insert Position/README.md b/solution/0000-0099/0035.Search Insert Position/README.md index c867a9cd4b0d6..8b5432b4cedfc 100644 --- a/solution/0000-0099/0035.Search Insert Position/README.md +++ b/solution/0000-0099/0035.Search Insert Position/README.md @@ -185,9 +185,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match nums[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } + Ordering::Equal => { + return mid as i32; + } } } left as i32 diff --git a/solution/0000-0099/0035.Search Insert Position/README_EN.md b/solution/0000-0099/0035.Search Insert Position/README_EN.md index 8ba6f6d7a1cd2..0c9f1b95d65b7 100644 --- a/solution/0000-0099/0035.Search Insert Position/README_EN.md +++ b/solution/0000-0099/0035.Search Insert Position/README_EN.md @@ -171,9 +171,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match nums[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } + Ordering::Equal => { + return mid as i32; + } } } left as i32 diff --git a/solution/0000-0099/0035.Search Insert Position/Solution.rs b/solution/0000-0099/0035.Search Insert Position/Solution.rs index 510fbbc56847b..e5323e4d6f2ac 100644 --- a/solution/0000-0099/0035.Search Insert Position/Solution.rs +++ b/solution/0000-0099/0035.Search Insert Position/Solution.rs @@ -6,9 +6,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match nums[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } + Ordering::Equal => { + return mid as i32; + } } } left as i32 diff --git a/solution/0000-0099/0039.Combination Sum/Solution.rs b/solution/0000-0099/0039.Combination Sum/Solution.rs index f90d94a5c38e2..e494119d6cf34 100644 --- a/solution/0000-0099/0039.Combination Sum/Solution.rs +++ b/solution/0000-0099/0039.Combination Sum/Solution.rs @@ -19,4 +19,4 @@ impl Solution { Self::dfs(0, target, &candidates, &mut vec![], &mut ans); ans } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0040.Combination Sum II/README.md b/solution/0000-0099/0040.Combination Sum II/README.md index 3118c3e24e0b9..b819266debcd2 100644 --- a/solution/0000-0099/0040.Combination Sum II/README.md +++ b/solution/0000-0099/0040.Combination Sum II/README.md @@ -483,7 +483,7 @@ impl Solution { Self::dfs(i + 1, s - x, candidates, t, ans); t.pop(); while i < candidates.len() && candidates[i] == x { - i += 1 + i += 1; } Self::dfs(i, s, candidates, t, ans); } diff --git a/solution/0000-0099/0040.Combination Sum II/README_EN.md b/solution/0000-0099/0040.Combination Sum II/README_EN.md index 3d880505cc48b..0a5cea1ebca80 100644 --- a/solution/0000-0099/0040.Combination Sum II/README_EN.md +++ b/solution/0000-0099/0040.Combination Sum II/README_EN.md @@ -457,7 +457,7 @@ impl Solution { Self::dfs(i + 1, s - x, candidates, t, ans); t.pop(); while i < candidates.len() && candidates[i] == x { - i += 1 + i += 1; } Self::dfs(i, s, candidates, t, ans); } diff --git a/solution/0000-0099/0040.Combination Sum II/Solution.rs b/solution/0000-0099/0040.Combination Sum II/Solution.rs index 9dbbf7c19f1e5..9cc50a99b4719 100644 --- a/solution/0000-0099/0040.Combination Sum II/Solution.rs +++ b/solution/0000-0099/0040.Combination Sum II/Solution.rs @@ -23,4 +23,4 @@ impl Solution { Self::dfs(0, target, &candidates, &mut vec![], &mut ans); ans } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0041.First Missing Positive/README.md b/solution/0000-0099/0041.First Missing Positive/README.md index 2a00060046a7c..9ba11b4bdabf3 100644 --- a/solution/0000-0099/0041.First Missing Positive/README.md +++ b/solution/0000-0099/0041.First Missing Positive/README.md @@ -252,17 +252,19 @@ impl Solution { let mut i = 0; while i < n { let j = nums[i] - 1; - if i as i32 == j || j < 0 || j >= n as i32 || nums[i] == nums[j as usize] { + if (i as i32) == j || j < 0 || j >= (n as i32) || nums[i] == nums[j as usize] { i += 1; } else { nums.swap(i, j as usize); } } - nums.iter() - .enumerate() - .position(|(i, &v)| v as usize != i + 1) - .unwrap_or(n) as i32 - + 1 + ( + nums + .iter() + .enumerate() + .position(|(i, &v)| (v as usize) != i + 1) + .unwrap_or(n) as i32 + ) + 1 } } ``` diff --git a/solution/0000-0099/0041.First Missing Positive/README_EN.md b/solution/0000-0099/0041.First Missing Positive/README_EN.md index 56632e00157c3..458ee47900ee6 100644 --- a/solution/0000-0099/0041.First Missing Positive/README_EN.md +++ b/solution/0000-0099/0041.First Missing Positive/README_EN.md @@ -238,17 +238,19 @@ impl Solution { let mut i = 0; while i < n { let j = nums[i] - 1; - if i as i32 == j || j < 0 || j >= n as i32 || nums[i] == nums[j as usize] { + if (i as i32) == j || j < 0 || j >= (n as i32) || nums[i] == nums[j as usize] { i += 1; } else { nums.swap(i, j as usize); } } - nums.iter() - .enumerate() - .position(|(i, &v)| v as usize != i + 1) - .unwrap_or(n) as i32 - + 1 + ( + nums + .iter() + .enumerate() + .position(|(i, &v)| (v as usize) != i + 1) + .unwrap_or(n) as i32 + ) + 1 } } ``` diff --git a/solution/0000-0099/0041.First Missing Positive/Solution.rs b/solution/0000-0099/0041.First Missing Positive/Solution.rs index 3ce8ae4018ab6..d9bc29d11be9b 100644 --- a/solution/0000-0099/0041.First Missing Positive/Solution.rs +++ b/solution/0000-0099/0041.First Missing Positive/Solution.rs @@ -4,16 +4,18 @@ impl Solution { let mut i = 0; while i < n { let j = nums[i] - 1; - if i as i32 == j || j < 0 || j >= n as i32 || nums[i] == nums[j as usize] { + if (i as i32) == j || j < 0 || j >= (n as i32) || nums[i] == nums[j as usize] { i += 1; } else { nums.swap(i, j as usize); } } - nums.iter() - .enumerate() - .position(|(i, &v)| v as usize != i + 1) - .unwrap_or(n) as i32 - + 1 + ( + nums + .iter() + .enumerate() + .position(|(i, &v)| (v as usize) != i + 1) + .unwrap_or(n) as i32 + ) + 1 } } diff --git a/solution/0000-0099/0042.Trapping Rain Water/Solution.rs b/solution/0000-0099/0042.Trapping Rain Water/Solution.rs index 19e0a3a22b305..0d860c2ac13af 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/Solution.rs +++ b/solution/0000-0099/0042.Trapping Rain Water/Solution.rs @@ -23,4 +23,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0043.Multiply Strings/README.md b/solution/0000-0099/0043.Multiply Strings/README.md index 71e5976d0f186..6a4f102863b5d 100644 --- a/solution/0000-0099/0043.Multiply Strings/README.md +++ b/solution/0000-0099/0043.Multiply Strings/README.md @@ -260,7 +260,7 @@ impl Solution { let mut j = 0; while j < m || sum != 0 { if i + j == res.len() { - res.push(0) + res.push(0); } let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0'; sum += a * b + res[i + j]; diff --git a/solution/0000-0099/0043.Multiply Strings/README_EN.md b/solution/0000-0099/0043.Multiply Strings/README_EN.md index c9dacedc8ac7d..c52ec1b3c7a48 100644 --- a/solution/0000-0099/0043.Multiply Strings/README_EN.md +++ b/solution/0000-0099/0043.Multiply Strings/README_EN.md @@ -222,7 +222,7 @@ impl Solution { let mut j = 0; while j < m || sum != 0 { if i + j == res.len() { - res.push(0) + res.push(0); } let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0'; sum += a * b + res[i + j]; diff --git a/solution/0000-0099/0043.Multiply Strings/Solution.rs b/solution/0000-0099/0043.Multiply Strings/Solution.rs index ede3d91f40dda..f33fe51b52920 100644 --- a/solution/0000-0099/0043.Multiply Strings/Solution.rs +++ b/solution/0000-0099/0043.Multiply Strings/Solution.rs @@ -12,7 +12,7 @@ impl Solution { let mut j = 0; while j < m || sum != 0 { if i + j == res.len() { - res.push(0) + res.push(0); } let b = num2.get(m - j - 1).unwrap_or(&b'0') - b'0'; sum += a * b + res[i + j]; diff --git a/solution/0000-0099/0046.Permutations/README.md b/solution/0000-0099/0046.Permutations/README.md index 6af8a6d4b4151..be38bb075a030 100644 --- a/solution/0000-0099/0046.Permutations/README.md +++ b/solution/0000-0099/0046.Permutations/README.md @@ -327,9 +327,15 @@ impl Solution { } #[allow(dead_code)] - fn dfs(nums: &Vec, vis: &mut Vec, index: i32, cur_vec: &mut Vec, ans: &mut Vec>) { + fn dfs( + nums: &Vec, + vis: &mut Vec, + index: i32, + cur_vec: &mut Vec, + ans: &mut Vec> + ) { let n = nums.len(); - if index as usize == n { + if (index as usize) == n { ans.push(cur_vec.clone()); return; } diff --git a/solution/0000-0099/0046.Permutations/README_EN.md b/solution/0000-0099/0046.Permutations/README_EN.md index 2665f00e8a346..d445a9f5d47af 100644 --- a/solution/0000-0099/0046.Permutations/README_EN.md +++ b/solution/0000-0099/0046.Permutations/README_EN.md @@ -298,9 +298,15 @@ impl Solution { } #[allow(dead_code)] - fn dfs(nums: &Vec, vis: &mut Vec, index: i32, cur_vec: &mut Vec, ans: &mut Vec>) { + fn dfs( + nums: &Vec, + vis: &mut Vec, + index: i32, + cur_vec: &mut Vec, + ans: &mut Vec> + ) { let n = nums.len(); - if index as usize == n { + if (index as usize) == n { ans.push(cur_vec.clone()); return; } diff --git a/solution/0000-0099/0048.Rotate Image/README.md b/solution/0000-0099/0048.Rotate Image/README.md index 7b33e1e236020..3b7768083826a 100644 --- a/solution/0000-0099/0048.Rotate Image/README.md +++ b/solution/0000-0099/0048.Rotate Image/README.md @@ -198,11 +198,11 @@ public class Solution { impl Solution { pub fn rotate(matrix: &mut Vec>) { let n = matrix.len(); - for i in 0..n/2 { + for i in 0..n / 2 { for j in 0..n { let t = matrix[i][j]; - matrix[i][j] = matrix[n-i-1][j]; - matrix[n-i-1][j] = t; + matrix[i][j] = matrix[n - i - 1][j]; + matrix[n - i - 1][j] = t; } } for i in 0..n { diff --git a/solution/0000-0099/0048.Rotate Image/README_EN.md b/solution/0000-0099/0048.Rotate Image/README_EN.md index b6cdf343222cf..5cf62d0b5e77f 100644 --- a/solution/0000-0099/0048.Rotate Image/README_EN.md +++ b/solution/0000-0099/0048.Rotate Image/README_EN.md @@ -186,11 +186,11 @@ public class Solution { impl Solution { pub fn rotate(matrix: &mut Vec>) { let n = matrix.len(); - for i in 0..n/2 { + for i in 0..n / 2 { for j in 0..n { let t = matrix[i][j]; - matrix[i][j] = matrix[n-i-1][j]; - matrix[n-i-1][j] = t; + matrix[i][j] = matrix[n - i - 1][j]; + matrix[n - i - 1][j] = t; } } for i in 0..n { diff --git a/solution/0000-0099/0048.Rotate Image/Solution.rs b/solution/0000-0099/0048.Rotate Image/Solution.rs index bc8a07b1de6ec..06552d6ffb320 100644 --- a/solution/0000-0099/0048.Rotate Image/Solution.rs +++ b/solution/0000-0099/0048.Rotate Image/Solution.rs @@ -1,11 +1,11 @@ impl Solution { pub fn rotate(matrix: &mut Vec>) { let n = matrix.len(); - for i in 0..n/2 { + for i in 0..n / 2 { for j in 0..n { let t = matrix[i][j]; - matrix[i][j] = matrix[n-i-1][j]; - matrix[n-i-1][j] = t; + matrix[i][j] = matrix[n - i - 1][j]; + matrix[n - i - 1][j] = t; } } for i in 0..n { @@ -16,4 +16,4 @@ impl Solution { } } } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0049.Group Anagrams/README.md b/solution/0000-0099/0049.Group Anagrams/README.md index 753d9706c6eda..549e00d5e0315 100644 --- a/solution/0000-0099/0049.Group Anagrams/README.md +++ b/solution/0000-0099/0049.Group Anagrams/README.md @@ -261,7 +261,9 @@ impl Solution { let val = map.entry(key).or_insert(vec![]); val.push(s); } - map.into_iter().map(|(_, v)| v).collect() + map.into_iter() + .map(|(_, v)| v) + .collect() } } ``` diff --git a/solution/0000-0099/0049.Group Anagrams/README_EN.md b/solution/0000-0099/0049.Group Anagrams/README_EN.md index ae571622d5230..b11ae937f401c 100644 --- a/solution/0000-0099/0049.Group Anagrams/README_EN.md +++ b/solution/0000-0099/0049.Group Anagrams/README_EN.md @@ -218,7 +218,9 @@ impl Solution { let val = map.entry(key).or_insert(vec![]); val.push(s); } - map.into_iter().map(|(_, v)| v).collect() + map.into_iter() + .map(|(_, v)| v) + .collect() } } ``` diff --git a/solution/0000-0099/0049.Group Anagrams/Solution.rs b/solution/0000-0099/0049.Group Anagrams/Solution.rs index a3e87c12792fc..ca7195d109292 100644 --- a/solution/0000-0099/0049.Group Anagrams/Solution.rs +++ b/solution/0000-0099/0049.Group Anagrams/Solution.rs @@ -12,6 +12,8 @@ impl Solution { let val = map.entry(key).or_insert(vec![]); val.push(s); } - map.into_iter().map(|(_, v)| v).collect() + map.into_iter() + .map(|(_, v)| v) + .collect() } } diff --git a/solution/0000-0099/0050.Pow(x, n)/README.md b/solution/0000-0099/0050.Pow(x, n)/README.md index 16604d3a7552b..2d2a6e4f9a457 100644 --- a/solution/0000-0099/0050.Pow(x, n)/README.md +++ b/solution/0000-0099/0050.Pow(x, n)/README.md @@ -139,7 +139,7 @@ impl Solution { // `n` should greater or equal to zero let mut ret = 1.0; while n != 0 { - if n & 0x1 == 1 { + if (n & 0x1) == 1 { ret *= *x; } *x *= *x; diff --git a/solution/0000-0099/0050.Pow(x, n)/README_EN.md b/solution/0000-0099/0050.Pow(x, n)/README_EN.md index b5532e39a7d23..0d4bc3a50f6f0 100644 --- a/solution/0000-0099/0050.Pow(x, n)/README_EN.md +++ b/solution/0000-0099/0050.Pow(x, n)/README_EN.md @@ -123,7 +123,7 @@ impl Solution { // `n` should greater or equal to zero let mut ret = 1.0; while n != 0 { - if n & 0x1 == 1 { + if (n & 0x1) == 1 { ret *= *x; } *x *= *x; diff --git a/solution/0000-0099/0050.Pow(x, n)/Solution.rs b/solution/0000-0099/0050.Pow(x, n)/Solution.rs index 95eb1fbc897bc..f4cda7e7e8768 100644 --- a/solution/0000-0099/0050.Pow(x, n)/Solution.rs +++ b/solution/0000-0099/0050.Pow(x, n)/Solution.rs @@ -15,7 +15,7 @@ impl Solution { // `n` should greater or equal to zero let mut ret = 1.0; while n != 0 { - if n & 0x1 == 1 { + if (n & 0x1) == 1 { ret *= *x; } *x *= *x; @@ -23,4 +23,4 @@ impl Solution { } ret } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0053.Maximum Subarray/Solution.rs b/solution/0000-0099/0053.Maximum Subarray/Solution.rs index d5fcc76943148..a42410ec923a4 100644 --- a/solution/0000-0099/0053.Maximum Subarray/Solution.rs +++ b/solution/0000-0099/0053.Maximum Subarray/Solution.rs @@ -9,4 +9,4 @@ impl Solution { } ans } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0055.Jump Game/README.md b/solution/0000-0099/0055.Jump Game/README.md index cc63810c1b6c2..696b28942a723 100644 --- a/solution/0000-0099/0055.Jump Game/README.md +++ b/solution/0000-0099/0055.Jump Game/README.md @@ -124,7 +124,7 @@ impl Solution { if mx < i { return false; } - mx = std::cmp::max(mx, i + nums[i] as usize); + mx = std::cmp::max(mx, i + (nums[i] as usize)); } true diff --git a/solution/0000-0099/0055.Jump Game/README_EN.md b/solution/0000-0099/0055.Jump Game/README_EN.md index 1aaea3cdbf78e..213bfca418d69 100644 --- a/solution/0000-0099/0055.Jump Game/README_EN.md +++ b/solution/0000-0099/0055.Jump Game/README_EN.md @@ -108,7 +108,7 @@ impl Solution { if mx < i { return false; } - mx = std::cmp::max(mx, i + nums[i] as usize); + mx = std::cmp::max(mx, i + (nums[i] as usize)); } true diff --git a/solution/0000-0099/0055.Jump Game/Solution.rs b/solution/0000-0099/0055.Jump Game/Solution.rs index e13d0c3f19fc2..40047426f32f7 100644 --- a/solution/0000-0099/0055.Jump Game/Solution.rs +++ b/solution/0000-0099/0055.Jump Game/Solution.rs @@ -8,9 +8,9 @@ impl Solution { if mx < i { return false; } - mx = std::cmp::max(mx, i + nums[i] as usize); + mx = std::cmp::max(mx, i + (nums[i] as usize)); } true } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0057.Insert Interval/Solution.rs b/solution/0000-0099/0057.Insert Interval/Solution.rs index 36777f5f31ee0..49be224f5754b 100644 --- a/solution/0000-0099/0057.Insert Interval/Solution.rs +++ b/solution/0000-0099/0057.Insert Interval/Solution.rs @@ -1,28 +1,28 @@ -impl Solution { - pub fn insert(intervals: Vec>, new_interval: Vec) -> Vec> { - let mut inserted = false; - let mut result = vec![]; - - let (mut start, mut end) = (new_interval[0], new_interval[1]); - for iter in intervals.iter() { - let (cur_st, cur_ed) = (iter[0], iter[1]); - if cur_ed < start { - result.push(vec![cur_st, cur_ed]); - } else if cur_st > end { - if !inserted { - inserted = true; - result.push(vec![start, end]); - } - result.push(vec![cur_st, cur_ed]); - } else { - start = std::cmp::min(start, cur_st); - end = std::cmp::max(end, cur_ed); - } - } - - if !inserted { - result.push(vec![start, end]); - } - result - } -} \ No newline at end of file +impl Solution { + pub fn insert(intervals: Vec>, new_interval: Vec) -> Vec> { + let mut inserted = false; + let mut result = vec![]; + + let (mut start, mut end) = (new_interval[0], new_interval[1]); + for iter in intervals.iter() { + let (cur_st, cur_ed) = (iter[0], iter[1]); + if cur_ed < start { + result.push(vec![cur_st, cur_ed]); + } else if cur_st > end { + if !inserted { + inserted = true; + result.push(vec![start, end]); + } + result.push(vec![cur_st, cur_ed]); + } else { + start = std::cmp::min(start, cur_st); + end = std::cmp::max(end, cur_ed); + } + } + + if !inserted { + result.push(vec![start, end]); + } + result + } +} diff --git a/solution/0000-0099/0062.Unique Paths/Solution.rs b/solution/0000-0099/0062.Unique Paths/Solution.rs index cc9dc4ad2d9a5..4481cea587b7d 100644 --- a/solution/0000-0099/0062.Unique Paths/Solution.rs +++ b/solution/0000-0099/0062.Unique Paths/Solution.rs @@ -1,12 +1,12 @@ -impl Solution { - pub fn unique_paths(m: i32, n: i32) -> i32 { - let (m, n) = (m as usize, n as usize); - let mut f = vec![1; n]; - for i in 1..m { - for j in 1..n { - f[j] += f[j - 1]; - } - } - f[n - 1] - } -} +impl Solution { + pub fn unique_paths(m: i32, n: i32) -> i32 { + let (m, n) = (m as usize, n as usize); + let mut f = vec![1; n]; + for i in 1..m { + for j in 1..n { + f[j] += f[j - 1]; + } + } + f[n - 1] + } +} diff --git a/solution/0000-0099/0067.Add Binary/README.md b/solution/0000-0099/0067.Add Binary/README.md index 04dad5ac14f42..2067df00fb9fe 100644 --- a/solution/0000-0099/0067.Add Binary/README.md +++ b/solution/0000-0099/0067.Add Binary/README.md @@ -156,8 +156,8 @@ function addBinary(a: string, b: string): string { ```rust impl Solution { pub fn add_binary(a: String, b: String) -> String { - let mut i = a.len() as i32 - 1; - let mut j = b.len() as i32 - 1; + let mut i = (a.len() as i32) - 1; + let mut j = (b.len() as i32) - 1; let mut carry = 0; let mut ans = String::new(); let a = a.as_bytes(); diff --git a/solution/0000-0099/0067.Add Binary/README_EN.md b/solution/0000-0099/0067.Add Binary/README_EN.md index fb68324beac32..c1bd9e99d210e 100644 --- a/solution/0000-0099/0067.Add Binary/README_EN.md +++ b/solution/0000-0099/0067.Add Binary/README_EN.md @@ -135,8 +135,8 @@ function addBinary(a: string, b: string): string { ```rust impl Solution { pub fn add_binary(a: String, b: String) -> String { - let mut i = a.len() as i32 - 1; - let mut j = b.len() as i32 - 1; + let mut i = (a.len() as i32) - 1; + let mut j = (b.len() as i32) - 1; let mut carry = 0; let mut ans = String::new(); let a = a.as_bytes(); diff --git a/solution/0000-0099/0067.Add Binary/Solution.rs b/solution/0000-0099/0067.Add Binary/Solution.rs index 77f05c567ae06..60e80f5fa5523 100644 --- a/solution/0000-0099/0067.Add Binary/Solution.rs +++ b/solution/0000-0099/0067.Add Binary/Solution.rs @@ -1,23 +1,23 @@ -impl Solution { - pub fn add_binary(a: String, b: String) -> String { - let mut i = a.len() as i32 - 1; - let mut j = b.len() as i32 - 1; - let mut carry = 0; - let mut ans = String::new(); - let a = a.as_bytes(); - let b = b.as_bytes(); - while i >= 0 || j >= 0 || carry > 0 { - if i >= 0 { - carry += a[i as usize] - b'0'; - i -= 1; - } - if j >= 0 { - carry += b[j as usize] - b'0'; - j -= 1; - } - ans.push_str(&(carry % 2).to_string()); - carry /= 2; - } - ans.chars().rev().collect() - } -} +impl Solution { + pub fn add_binary(a: String, b: String) -> String { + let mut i = (a.len() as i32) - 1; + let mut j = (b.len() as i32) - 1; + let mut carry = 0; + let mut ans = String::new(); + let a = a.as_bytes(); + let b = b.as_bytes(); + while i >= 0 || j >= 0 || carry > 0 { + if i >= 0 { + carry += a[i as usize] - b'0'; + i -= 1; + } + if j >= 0 { + carry += b[j as usize] - b'0'; + j -= 1; + } + ans.push_str(&(carry % 2).to_string()); + carry /= 2; + } + ans.chars().rev().collect() + } +} diff --git a/solution/0000-0099/0069.Sqrt(x)/README.md b/solution/0000-0099/0069.Sqrt(x)/README.md index 9d7950f25e274..61b466ca3a94b 100644 --- a/solution/0000-0099/0069.Sqrt(x)/README.md +++ b/solution/0000-0099/0069.Sqrt(x)/README.md @@ -179,7 +179,7 @@ impl Solution { while l < r { let mid = (l + r + 1) >> 1; if x / mid < mid { - r = mid - 1 + r = mid - 1; } else { l = mid; } diff --git a/solution/0000-0099/0069.Sqrt(x)/README_EN.md b/solution/0000-0099/0069.Sqrt(x)/README_EN.md index d73dd961271fe..5ab23f99d75db 100644 --- a/solution/0000-0099/0069.Sqrt(x)/README_EN.md +++ b/solution/0000-0099/0069.Sqrt(x)/README_EN.md @@ -172,7 +172,7 @@ impl Solution { while l < r { let mid = (l + r + 1) >> 1; if x / mid < mid { - r = mid - 1 + r = mid - 1; } else { l = mid; } diff --git a/solution/0000-0099/0069.Sqrt(x)/Solution.rs b/solution/0000-0099/0069.Sqrt(x)/Solution.rs index 6e75343a2c05e..5e596f21681ae 100644 --- a/solution/0000-0099/0069.Sqrt(x)/Solution.rs +++ b/solution/0000-0099/0069.Sqrt(x)/Solution.rs @@ -8,7 +8,7 @@ impl Solution { while l < r { let mid = (l + r + 1) >> 1; if x / mid < mid { - r = mid - 1 + r = mid - 1; } else { l = mid; } diff --git a/solution/0000-0099/0071.Simplify Path/README.md b/solution/0000-0099/0071.Simplify Path/README.md index b7936570b1eaa..b2e0c60431616 100644 --- a/solution/0000-0099/0071.Simplify Path/README.md +++ b/solution/0000-0099/0071.Simplify Path/README.md @@ -171,7 +171,9 @@ impl Solution { for p in p_vec { match p { // Do nothing for "" or "." - "" | "." => continue, + "" | "." => { + continue; + } ".." => { if !s.is_empty() { s.pop(); diff --git a/solution/0000-0099/0071.Simplify Path/README_EN.md b/solution/0000-0099/0071.Simplify Path/README_EN.md index f3f2a77fca79a..18f4d2aff4fa7 100644 --- a/solution/0000-0099/0071.Simplify Path/README_EN.md +++ b/solution/0000-0099/0071.Simplify Path/README_EN.md @@ -155,7 +155,9 @@ impl Solution { for p in p_vec { match p { // Do nothing for "" or "." - "" | "." => continue, + "" | "." => { + continue; + } ".." => { if !s.is_empty() { s.pop(); diff --git a/solution/0000-0099/0071.Simplify Path/Solution.rs b/solution/0000-0099/0071.Simplify Path/Solution.rs index 36015c19b4295..21fe415de1af1 100644 --- a/solution/0000-0099/0071.Simplify Path/Solution.rs +++ b/solution/0000-0099/0071.Simplify Path/Solution.rs @@ -10,7 +10,9 @@ impl Solution { for p in p_vec { match p { // Do nothing for "" or "." - "" | "." => continue, + "" | "." => { + continue; + } ".." => { if !s.is_empty() { s.pop(); diff --git a/solution/0000-0099/0074.Search a 2D Matrix/README.md b/solution/0000-0099/0074.Search a 2D Matrix/README.md index 73089e44c5fa8..3634d252e2b52 100644 --- a/solution/0000-0099/0074.Search a 2D Matrix/README.md +++ b/solution/0000-0099/0074.Search a 2D Matrix/README.md @@ -325,9 +325,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match matrix[i][j - 1].cmp(&target) { - Ordering::Equal => return true, - Ordering::Less => i += 1, - Ordering::Greater => j -= 1, + Ordering::Equal => { + return true; + } + Ordering::Less => { + i += 1; + } + Ordering::Greater => { + j -= 1; + } } } false @@ -348,9 +354,15 @@ impl Solution { let i = mid / n; let j = mid % n; match matrix[i][j].cmp(&target) { - Ordering::Equal => return true, - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, + Ordering::Equal => { + return true; + } + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } } } false diff --git a/solution/0000-0099/0074.Search a 2D Matrix/README_EN.md b/solution/0000-0099/0074.Search a 2D Matrix/README_EN.md index 9418e44fc814f..aba485147802b 100644 --- a/solution/0000-0099/0074.Search a 2D Matrix/README_EN.md +++ b/solution/0000-0099/0074.Search a 2D Matrix/README_EN.md @@ -279,9 +279,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match matrix[i][j - 1].cmp(&target) { - Ordering::Equal => return true, - Ordering::Less => i += 1, - Ordering::Greater => j -= 1, + Ordering::Equal => { + return true; + } + Ordering::Less => { + i += 1; + } + Ordering::Greater => { + j -= 1; + } } } false @@ -302,9 +308,15 @@ impl Solution { let i = mid / n; let j = mid % n; match matrix[i][j].cmp(&target) { - Ordering::Equal => return true, - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, + Ordering::Equal => { + return true; + } + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } } } false diff --git a/solution/0000-0099/0074.Search a 2D Matrix/Solution.rs b/solution/0000-0099/0074.Search a 2D Matrix/Solution.rs index 38a90749c2fce..011b3ae2422a6 100644 --- a/solution/0000-0099/0074.Search a 2D Matrix/Solution.rs +++ b/solution/0000-0099/0074.Search a 2D Matrix/Solution.rs @@ -7,9 +7,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match matrix[i][j - 1].cmp(&target) { - Ordering::Equal => return true, - Ordering::Less => i += 1, - Ordering::Greater => j -= 1, + Ordering::Equal => { + return true; + } + Ordering::Less => { + i += 1; + } + Ordering::Greater => { + j -= 1; + } } } false diff --git a/solution/0000-0099/0075.Sort Colors/Solution.rs b/solution/0000-0099/0075.Sort Colors/Solution.rs index d8aee4a1d2922..6aaed6f31e373 100644 --- a/solution/0000-0099/0075.Sort Colors/Solution.rs +++ b/solution/0000-0099/0075.Sort Colors/Solution.rs @@ -1,19 +1,19 @@ -impl Solution { - pub fn sort_colors(nums: &mut Vec) { - let mut i = -1; - let mut j = nums.len(); - let mut k = 0; - while k < j { - if nums[k] == 0 { - i += 1; - nums.swap(i as usize, k as usize); - k += 1; - } else if nums[k] == 2 { - j -= 1; - nums.swap(j, k); - } else { - k += 1; - } - } - } -} \ No newline at end of file +impl Solution { + pub fn sort_colors(nums: &mut Vec) { + let mut i = -1; + let mut j = nums.len(); + let mut k = 0; + while k < j { + if nums[k] == 0 { + i += 1; + nums.swap(i as usize, k as usize); + k += 1; + } else if nums[k] == 2 { + j -= 1; + nums.swap(j, k); + } else { + k += 1; + } + } + } +} diff --git a/solution/0000-0099/0077.Combinations/README.md b/solution/0000-0099/0077.Combinations/README.md index 489263cc0f488..1a44ac73f0538 100644 --- a/solution/0000-0099/0077.Combinations/README.md +++ b/solution/0000-0099/0077.Combinations/README.md @@ -334,7 +334,7 @@ function combine(n: number, k: number): number[][] { ```rust impl Solution { fn dfs(i: i32, n: i32, k: i32, t: &mut Vec, ans: &mut Vec>) { - if t.len() == k as usize { + if t.len() == (k as usize) { ans.push(t.clone()); return; } @@ -358,7 +358,7 @@ impl Solution { ```rust impl Solution { fn dfs(i: i32, n: i32, k: i32, t: &mut Vec, ans: &mut Vec>) { - if t.len() == k as usize { + if t.len() == (k as usize) { ans.push(t.clone()); return; } diff --git a/solution/0000-0099/0077.Combinations/README_EN.md b/solution/0000-0099/0077.Combinations/README_EN.md index 55b5ac691b765..2bd1b8a784229 100644 --- a/solution/0000-0099/0077.Combinations/README_EN.md +++ b/solution/0000-0099/0077.Combinations/README_EN.md @@ -301,7 +301,7 @@ function combine(n: number, k: number): number[][] { ```rust impl Solution { fn dfs(i: i32, n: i32, k: i32, t: &mut Vec, ans: &mut Vec>) { - if t.len() == k as usize { + if t.len() == (k as usize) { ans.push(t.clone()); return; } @@ -325,7 +325,7 @@ impl Solution { ```rust impl Solution { fn dfs(i: i32, n: i32, k: i32, t: &mut Vec, ans: &mut Vec>) { - if t.len() == k as usize { + if t.len() == (k as usize) { ans.push(t.clone()); return; } diff --git a/solution/0000-0099/0077.Combinations/Solution.rs b/solution/0000-0099/0077.Combinations/Solution.rs index de64a2baa371e..dff341deac4e6 100644 --- a/solution/0000-0099/0077.Combinations/Solution.rs +++ b/solution/0000-0099/0077.Combinations/Solution.rs @@ -1,6 +1,6 @@ impl Solution { fn dfs(i: i32, n: i32, k: i32, t: &mut Vec, ans: &mut Vec>) { - if t.len() == k as usize { + if t.len() == (k as usize) { ans.push(t.clone()); return; } diff --git a/solution/0000-0099/0079.Word Search/README.md b/solution/0000-0099/0079.Word Search/README.md index 08b3d74e952f4..d33ede5b768b5 100644 --- a/solution/0000-0099/0079.Word Search/README.md +++ b/solution/0000-0099/0079.Word Search/README.md @@ -310,20 +310,25 @@ impl Solution { c: usize, word: &[u8], board: &Vec>, - vis: &mut Vec>, + vis: &mut Vec> ) -> bool { - if board[i][j] as u8 != word[c] { + if (board[i][j] as u8) != word[c] { return false; } if c == word.len() - 1 { return true; } vis[i][j] = true; - let dirs = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + let dirs = [ + [-1, 0], + [0, -1], + [1, 0], + [0, 1], + ]; for [x, y] in dirs.into_iter() { - let i = x + i as i32; - let j = y + j as i32; - if i < 0 || i == board.len() as i32 || j < 0 || j == board[0].len() as i32 { + let i = x + (i as i32); + let j = y + (j as i32); + if i < 0 || i == (board.len() as i32) || j < 0 || j == (board[0].len() as i32) { continue; } let (i, j) = (i as usize, j as usize); diff --git a/solution/0000-0099/0079.Word Search/README_EN.md b/solution/0000-0099/0079.Word Search/README_EN.md index d6db21c1053ee..4bff079af746d 100644 --- a/solution/0000-0099/0079.Word Search/README_EN.md +++ b/solution/0000-0099/0079.Word Search/README_EN.md @@ -285,20 +285,25 @@ impl Solution { c: usize, word: &[u8], board: &Vec>, - vis: &mut Vec>, + vis: &mut Vec> ) -> bool { - if board[i][j] as u8 != word[c] { + if (board[i][j] as u8) != word[c] { return false; } if c == word.len() - 1 { return true; } vis[i][j] = true; - let dirs = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + let dirs = [ + [-1, 0], + [0, -1], + [1, 0], + [0, 1], + ]; for [x, y] in dirs.into_iter() { - let i = x + i as i32; - let j = y + j as i32; - if i < 0 || i == board.len() as i32 || j < 0 || j == board[0].len() as i32 { + let i = x + (i as i32); + let j = y + (j as i32); + if i < 0 || i == (board.len() as i32) || j < 0 || j == (board[0].len() as i32) { continue; } let (i, j) = (i as usize, j as usize); diff --git a/solution/0000-0099/0079.Word Search/Solution.rs b/solution/0000-0099/0079.Word Search/Solution.rs index c05def22a2595..59e4bbed2dfa7 100644 --- a/solution/0000-0099/0079.Word Search/Solution.rs +++ b/solution/0000-0099/0079.Word Search/Solution.rs @@ -5,21 +5,26 @@ impl Solution { c: usize, word: &[u8], board: &Vec>, - vis: &mut Vec>, + vis: &mut Vec> ) -> bool { - if board[i][j] as u8 != word[c] { + if (board[i][j] as u8) != word[c] { return false; } if c == word.len() - 1 { return true; } vis[i][j] = true; - let dirs = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + let dirs = [ + [-1, 0], + [0, -1], + [1, 0], + [0, 1], + ]; for [x, y] in dirs.into_iter() { // 索引合法性审核 - let i = x + i as i32; - let j = y + j as i32; - if i < 0 || i == board.len() as i32 || j < 0 || j == board[0].len() as i32 { + let i = x + (i as i32); + let j = y + (j as i32); + if i < 0 || i == (board.len() as i32) || j < 0 || j == (board[0].len() as i32) { continue; } let (i, j) = (i as usize, j as usize); diff --git a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.rs b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.rs index a0ff6c34d12a3..17971739cdfce 100644 --- a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.rs +++ b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.rs @@ -9,4 +9,4 @@ impl Solution { } k as i32 } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.rs b/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.rs index ef2e7d6cfdce0..d391ed02c1407 100644 --- a/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.rs +++ b/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.rs @@ -42,4 +42,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0085.Maximal Rectangle/Solution.rs b/solution/0000-0099/0085.Maximal Rectangle/Solution.rs index 5ca704368ace6..9e2d40066eb98 100644 --- a/solution/0000-0099/0085.Maximal Rectangle/Solution.rs +++ b/solution/0000-0099/0085.Maximal Rectangle/Solution.rs @@ -72,4 +72,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0088.Merge Sorted Array/README.md b/solution/0000-0099/0088.Merge Sorted Array/README.md index 008f4762c9e93..6b98ab68353f8 100644 --- a/solution/0000-0099/0088.Merge Sorted Array/README.md +++ b/solution/0000-0099/0088.Merge Sorted Array/README.md @@ -201,7 +201,7 @@ impl Solution { nums2[n] } } - } + }; } } } diff --git a/solution/0000-0099/0088.Merge Sorted Array/README_EN.md b/solution/0000-0099/0088.Merge Sorted Array/README_EN.md index 389b3d8d5df8b..5cf3d5cdb73d5 100644 --- a/solution/0000-0099/0088.Merge Sorted Array/README_EN.md +++ b/solution/0000-0099/0088.Merge Sorted Array/README_EN.md @@ -190,7 +190,7 @@ impl Solution { nums2[n] } } - } + }; } } } diff --git a/solution/0000-0099/0088.Merge Sorted Array/Solution.rs b/solution/0000-0099/0088.Merge Sorted Array/Solution.rs index b4707952585e0..91cf281490977 100644 --- a/solution/0000-0099/0088.Merge Sorted Array/Solution.rs +++ b/solution/0000-0099/0088.Merge Sorted Array/Solution.rs @@ -20,7 +20,7 @@ impl Solution { nums2[n] } } - } + }; } } } diff --git a/solution/0000-0099/0092.Reverse Linked List II/README.md b/solution/0000-0099/0092.Reverse Linked List II/README.md index 9281a72ec2669..6401210567726 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README.md @@ -337,7 +337,11 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis // } // } impl Solution { - pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + pub fn reverse_between( + head: Option>, + left: i32, + right: i32 + ) -> Option> { let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); let mut pre = &mut dummy; for _ in 1..left { diff --git a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md index 684d3e908cd75..4dfb0608eb91e 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md @@ -320,7 +320,11 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis // } // } impl Solution { - pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + pub fn reverse_between( + head: Option>, + left: i32, + right: i32 + ) -> Option> { let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); let mut pre = &mut dummy; for _ in 1..left { diff --git a/solution/0000-0099/0092.Reverse Linked List II/Solution.rs b/solution/0000-0099/0092.Reverse Linked List II/Solution.rs index e7b023c4d60ca..1678ffa65a60d 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/Solution.rs +++ b/solution/0000-0099/0092.Reverse Linked List II/Solution.rs @@ -15,7 +15,11 @@ // } // } impl Solution { - pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + pub fn reverse_between( + head: Option>, + left: i32, + right: i32 + ) -> Option> { let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); let mut pre = &mut dummy; for _ in 1..left { diff --git a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md index d816fe5d2e780..652b514b92ebb 100644 --- a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md +++ b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md @@ -590,7 +590,7 @@ function inorderTraversal(root: TreeNode | null): number[] { use std::rc::Rc; use std::cell::RefCell; impl Solution { - fn dfs (root: &Option>>, res: &mut Vec) { + fn dfs(root: &Option>>, res: &mut Vec) { if root.is_none() { return; } diff --git a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md index f23746b30c367..c54e1c8288d57 100644 --- a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md +++ b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md @@ -560,7 +560,7 @@ Recursion: use std::rc::Rc; use std::cell::RefCell; impl Solution { - fn dfs (root: &Option>>, res: &mut Vec) { + fn dfs(root: &Option>>, res: &mut Vec) { if root.is_none() { return; } diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs index c3bdef0ae9e91..a32acb576c953 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.rs @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -56,4 +56,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0097.Interleaving String/README.md b/solution/0000-0099/0097.Interleaving String/README.md index 582d6198bdaa7..5352a0549a721 100644 --- a/solution/0000-0099/0097.Interleaving String/README.md +++ b/solution/0000-0099/0097.Interleaving String/README.md @@ -362,11 +362,29 @@ impl Solution { let mut record = vec![vec![-1; m + 1]; n + 1]; - Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect()) + Self::dfs( + &mut record, + n, + m, + 0, + 0, + &s1.chars().collect(), + &s2.chars().collect(), + &s3.chars().collect() + ) } #[allow(dead_code)] - fn dfs(record: &mut Vec>, n: usize, m: usize, i: usize, j: usize, s1: &Vec, s2: &Vec, s3: &Vec) -> bool { + fn dfs( + record: &mut Vec>, + n: usize, + m: usize, + i: usize, + j: usize, + s1: &Vec, + s2: &Vec, + s3: &Vec + ) -> bool { if i >= n && j >= m { return true; } @@ -385,7 +403,12 @@ impl Solution { } // If the first approach does not succeed, let's then try `s2` - if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) { + if + record[i][j] == 0 && + j < m && + s2[j] == s3[k] && + Self::dfs(record, n, m, i, j + 1, s1, s2, s3) + { record[i][j] = 1; } diff --git a/solution/0000-0099/0097.Interleaving String/README_EN.md b/solution/0000-0099/0097.Interleaving String/README_EN.md index a3befbf3881b5..fa443cee8522a 100644 --- a/solution/0000-0099/0097.Interleaving String/README_EN.md +++ b/solution/0000-0099/0097.Interleaving String/README_EN.md @@ -314,11 +314,29 @@ impl Solution { let mut record = vec![vec![-1; m + 1]; n + 1]; - Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect()) + Self::dfs( + &mut record, + n, + m, + 0, + 0, + &s1.chars().collect(), + &s2.chars().collect(), + &s3.chars().collect() + ) } #[allow(dead_code)] - fn dfs(record: &mut Vec>, n: usize, m: usize, i: usize, j: usize, s1: &Vec, s2: &Vec, s3: &Vec) -> bool { + fn dfs( + record: &mut Vec>, + n: usize, + m: usize, + i: usize, + j: usize, + s1: &Vec, + s2: &Vec, + s3: &Vec + ) -> bool { if i >= n && j >= m { return true; } @@ -337,7 +355,12 @@ impl Solution { } // If the first approach does not succeed, let's then try `s2` - if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) { + if + record[i][j] == 0 && + j < m && + s2[j] == s3[k] && + Self::dfs(record, n, m, i, j + 1, s1, s2, s3) + { record[i][j] = 1; } diff --git a/solution/0000-0099/0097.Interleaving String/Solution.rs b/solution/0000-0099/0097.Interleaving String/Solution.rs index a22b98a0c5631..419d697d533cf 100644 --- a/solution/0000-0099/0097.Interleaving String/Solution.rs +++ b/solution/0000-0099/0097.Interleaving String/Solution.rs @@ -10,11 +10,29 @@ impl Solution { let mut record = vec![vec![-1; m + 1]; n + 1]; - Self::dfs(&mut record, n, m, 0, 0, &s1.chars().collect(), &s2.chars().collect(), &s3.chars().collect()) + Self::dfs( + &mut record, + n, + m, + 0, + 0, + &s1.chars().collect(), + &s2.chars().collect(), + &s3.chars().collect() + ) } #[allow(dead_code)] - fn dfs(record: &mut Vec>, n: usize, m: usize, i: usize, j: usize, s1: &Vec, s2: &Vec, s3: &Vec) -> bool { + fn dfs( + record: &mut Vec>, + n: usize, + m: usize, + i: usize, + j: usize, + s1: &Vec, + s2: &Vec, + s3: &Vec + ) -> bool { if i >= n && j >= m { return true; } @@ -33,10 +51,15 @@ impl Solution { } // If the first approach does not succeed, let's then try `s2` - if record[i][j] == 0 && j < m && s2[j] == s3[k] && Self::dfs(record, n, m, i, j + 1, s1, s2, s3) { + if + record[i][j] == 0 && + j < m && + s2[j] == s3[k] && + Self::dfs(record, n, m, i, j + 1, s1, s2, s3) + { record[i][j] = 1; } record[i][j] == 1 } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0100.Same Tree/README.md b/solution/0100-0199/0100.Same Tree/README.md index 3b0cbb5e8856a..11ff1245648ae 100644 --- a/solution/0100-0199/0100.Same Tree/README.md +++ b/solution/0100-0199/0100.Same Tree/README.md @@ -485,7 +485,7 @@ impl Solution { pub fn is_same_tree( p: Option>>, - q: Option>>, + q: Option>> ) -> bool { Self::dfs(&p, &q) } @@ -517,7 +517,7 @@ use std::collections::VecDeque; impl Solution { pub fn is_same_tree( mut p: Option>>, - mut q: Option>>, + mut q: Option>> ) -> bool { let mut queue = VecDeque::new(); if p.is_some() { @@ -542,7 +542,9 @@ impl Solution { queue.push_back(node1.left.take()); queue.push_back(node2.left.take()); } - (_, _) => return false, + (_, _) => { + return false; + } } match (node1.right.is_some(), node2.right.is_some()) { (false, false) => {} @@ -550,7 +552,9 @@ impl Solution { queue.push_back(node1.right.take()); queue.push_back(node2.right.take()); } - (_, _) => return false, + (_, _) => { + return false; + } } } } diff --git a/solution/0100-0199/0100.Same Tree/README_EN.md b/solution/0100-0199/0100.Same Tree/README_EN.md index 326c8cef66b35..03202e3ef831f 100644 --- a/solution/0100-0199/0100.Same Tree/README_EN.md +++ b/solution/0100-0199/0100.Same Tree/README_EN.md @@ -457,7 +457,7 @@ impl Solution { pub fn is_same_tree( p: Option>>, - q: Option>>, + q: Option>> ) -> bool { Self::dfs(&p, &q) } @@ -489,7 +489,7 @@ use std::collections::VecDeque; impl Solution { pub fn is_same_tree( mut p: Option>>, - mut q: Option>>, + mut q: Option>> ) -> bool { let mut queue = VecDeque::new(); if p.is_some() { @@ -514,7 +514,9 @@ impl Solution { queue.push_back(node1.left.take()); queue.push_back(node2.left.take()); } - (_, _) => return false, + (_, _) => { + return false; + } } match (node1.right.is_some(), node2.right.is_some()) { (false, false) => {} @@ -522,7 +524,9 @@ impl Solution { queue.push_back(node1.right.take()); queue.push_back(node2.right.take()); } - (_, _) => return false, + (_, _) => { + return false; + } } } } diff --git a/solution/0100-0199/0100.Same Tree/Solution.rs b/solution/0100-0199/0100.Same Tree/Solution.rs index 8db3324fa010f..ecc58869aff89 100644 --- a/solution/0100-0199/0100.Same Tree/Solution.rs +++ b/solution/0100-0199/0100.Same Tree/Solution.rs @@ -33,7 +33,7 @@ impl Solution { pub fn is_same_tree( p: Option>>, - q: Option>>, + q: Option>> ) -> bool { Self::dfs(&p, &q) } diff --git a/solution/0100-0199/0101.Symmetric Tree/README.md b/solution/0100-0199/0101.Symmetric Tree/README.md index ca41db7062d7a..62652309f309a 100644 --- a/solution/0100-0199/0101.Symmetric Tree/README.md +++ b/solution/0100-0199/0101.Symmetric Tree/README.md @@ -233,9 +233,9 @@ impl Solution { } let node1 = root1.as_ref().unwrap().borrow(); let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) + node1.val == node2.val && + Self::dfs(&node1.left, &node2.right) && + Self::dfs(&node1.right, &node2.left) } pub fn is_symmetric(root: Option>>) -> bool { diff --git a/solution/0100-0199/0101.Symmetric Tree/README_EN.md b/solution/0100-0199/0101.Symmetric Tree/README_EN.md index 0794283810e8d..31031186aa60b 100644 --- a/solution/0100-0199/0101.Symmetric Tree/README_EN.md +++ b/solution/0100-0199/0101.Symmetric Tree/README_EN.md @@ -210,9 +210,9 @@ impl Solution { } let node1 = root1.as_ref().unwrap().borrow(); let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) + node1.val == node2.val && + Self::dfs(&node1.left, &node2.right) && + Self::dfs(&node1.right, &node2.left) } pub fn is_symmetric(root: Option>>) -> bool { diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.rs b/solution/0100-0199/0101.Symmetric Tree/Solution.rs index ed38697d6b2c6..6efb004cc48a5 100644 --- a/solution/0100-0199/0101.Symmetric Tree/Solution.rs +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.rs @@ -28,9 +28,9 @@ impl Solution { } let node1 = root1.as_ref().unwrap().borrow(); let node2 = root2.as_ref().unwrap().borrow(); - node1.val == node2.val - && Self::dfs(&node1.left, &node2.right) - && Self::dfs(&node1.right, &node2.left) + node1.val == node2.val && + Self::dfs(&node1.left, &node2.right) && + Self::dfs(&node1.right, &node2.left) } pub fn is_symmetric(root: Option>>) -> bool { diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md index 08e5d5e80ee32..1e3974e3bcac6 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md @@ -325,7 +325,7 @@ impl Solution { } node.val }) - .collect(), + .collect() ); } res diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md index 4b5d3f9c64998..a323ff49be9ea 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md @@ -306,7 +306,7 @@ impl Solution { } node.val }) - .collect(), + .collect() ); } res diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.rs b/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.rs index 86f08319a7ad8..6a98a41411aec 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.rs +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.rs @@ -42,7 +42,7 @@ impl Solution { } node.val }) - .collect(), + .collect() ); } res diff --git a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README.md b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README.md index 6aeaffa2e7672..a0d8d2c772a55 100644 --- a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README.md +++ b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README.md @@ -410,7 +410,13 @@ impl Solution { Self::dfs(&preorder, &d, 0, 0, preorder.len()) } - pub fn dfs(preorder: &Vec, d: &HashMap, i: usize, j: usize, n: usize) -> Option>> { + pub fn dfs( + preorder: &Vec, + d: &HashMap, + i: usize, + j: usize, + n: usize + ) -> Option>> { if n <= 0 { return None; } diff --git a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README_EN.md b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README_EN.md index 96fd98a357981..ea52dc7232c33 100644 --- a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README_EN.md +++ b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README_EN.md @@ -386,7 +386,13 @@ impl Solution { Self::dfs(&preorder, &d, 0, 0, preorder.len()) } - pub fn dfs(preorder: &Vec, d: &HashMap, i: usize, j: usize, n: usize) -> Option>> { + pub fn dfs( + preorder: &Vec, + d: &HashMap, + i: usize, + j: usize, + n: usize + ) -> Option>> { if n <= 0 { return None; } diff --git a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/Solution.rs b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/Solution.rs index e3aa1d1b43534..0fbbf24b5efd7 100644 --- a/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/Solution.rs +++ b/solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/Solution.rs @@ -1,42 +1,48 @@ -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::rc::Rc; -use std::cell::RefCell; -use std::collections::HashMap; -impl Solution { - pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { - let mut d = HashMap::new(); - for (i, &x) in inorder.iter().enumerate() { - d.insert(x, i); - } - Self::dfs(&preorder, &d, 0, 0, preorder.len()) - } - - pub fn dfs(preorder: &Vec, d: &HashMap, i: usize, j: usize, n: usize) -> Option>> { - if n <= 0 { - return None; - } - let v = preorder[i]; - let k = d[&v]; - let mut root = TreeNode::new(v); - root.left = Self::dfs(preorder, d, i + 1, j, k - j); - root.right = Self::dfs(preorder, d, i + k - j + 1, k + 1, n - k + j - 1); - Some(Rc::new(RefCell::new(root))) - } -} \ No newline at end of file +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +use std::collections::HashMap; +impl Solution { + pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { + let mut d = HashMap::new(); + for (i, &x) in inorder.iter().enumerate() { + d.insert(x, i); + } + Self::dfs(&preorder, &d, 0, 0, preorder.len()) + } + + pub fn dfs( + preorder: &Vec, + d: &HashMap, + i: usize, + j: usize, + n: usize + ) -> Option>> { + if n <= 0 { + return None; + } + let v = preorder[i]; + let k = d[&v]; + let mut root = TreeNode::new(v); + root.left = Self::dfs(preorder, d, i + 1, j, k - j); + root.right = Self::dfs(preorder, d, i + k - j + 1, k + 1, n - k + j - 1); + Some(Rc::new(RefCell::new(root))) + } +} diff --git a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README.md b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README.md index 3ac604c0029e9..fd0fe626984ec 100644 --- a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README.md +++ b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README.md @@ -246,32 +246,39 @@ impl Solution { i_right: usize, postorder: &Vec, p_left: usize, - p_right: usize, + p_right: usize ) -> Option>> { if i_left == i_right { return None; } let val = postorder[p_right - 1]; - let index = inorder.iter().position(|&v| v == val).unwrap(); - Some(Rc::new(RefCell::new(TreeNode { - val, - left: Self::reset( - inorder, - i_left, - index, - postorder, - p_left, - p_left + index - i_left, - ), - right: Self::reset( - inorder, - index + 1, - i_right, - postorder, - p_left + index - i_left, - p_right - 1, - ), - }))) + let index = inorder + .iter() + .position(|&v| v == val) + .unwrap(); + Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: Self::reset( + inorder, + i_left, + index, + postorder, + p_left, + p_left + index - i_left + ), + right: Self::reset( + inorder, + index + 1, + i_right, + postorder, + p_left + index - i_left, + p_right - 1 + ), + }) + ) + ) } pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { diff --git a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README_EN.md b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README_EN.md index 4413425b20a54..04ee7ec6251be 100644 --- a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README_EN.md +++ b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/README_EN.md @@ -230,32 +230,39 @@ impl Solution { i_right: usize, postorder: &Vec, p_left: usize, - p_right: usize, + p_right: usize ) -> Option>> { if i_left == i_right { return None; } let val = postorder[p_right - 1]; - let index = inorder.iter().position(|&v| v == val).unwrap(); - Some(Rc::new(RefCell::new(TreeNode { - val, - left: Self::reset( - inorder, - i_left, - index, - postorder, - p_left, - p_left + index - i_left, - ), - right: Self::reset( - inorder, - index + 1, - i_right, - postorder, - p_left + index - i_left, - p_right - 1, - ), - }))) + let index = inorder + .iter() + .position(|&v| v == val) + .unwrap(); + Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: Self::reset( + inorder, + i_left, + index, + postorder, + p_left, + p_left + index - i_left + ), + right: Self::reset( + inorder, + index + 1, + i_right, + postorder, + p_left + index - i_left, + p_right - 1 + ), + }) + ) + ) } pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { diff --git a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/Solution.rs b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/Solution.rs index ee32cf45f7c3d..110cd8c554124 100644 --- a/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/Solution.rs +++ b/solution/0100-0199/0106.Construct Binary Tree from Inorder and Postorder Traversal/Solution.rs @@ -25,32 +25,39 @@ impl Solution { i_right: usize, postorder: &Vec, p_left: usize, - p_right: usize, + p_right: usize ) -> Option>> { if i_left == i_right { return None; } let val = postorder[p_right - 1]; - let index = inorder.iter().position(|&v| v == val).unwrap(); - Some(Rc::new(RefCell::new(TreeNode { - val, - left: Self::reset( - inorder, - i_left, - index, - postorder, - p_left, - p_left + index - i_left, - ), - right: Self::reset( - inorder, - index + 1, - i_right, - postorder, - p_left + index - i_left, - p_right - 1, - ), - }))) + let index = inorder + .iter() + .position(|&v| v == val) + .unwrap(); + Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: Self::reset( + inorder, + i_left, + index, + postorder, + p_left, + p_left + index - i_left + ), + right: Self::reset( + inorder, + index + 1, + i_right, + postorder, + p_left + index - i_left, + p_right - 1 + ), + }) + ) + ) } pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md index 1f3b43f309d93..152bfbca35c4e 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md @@ -183,7 +183,7 @@ public: // } // } // } -use std::{rc::Rc, cell::RefCell, collections::VecDeque}; +use std::{ rc::Rc, cell::RefCell, collections::VecDeque }; impl Solution { #[allow(dead_code)] pub fn level_order_bottom(root: Option>>) -> Vec> { diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md index 48ffe8db42058..9031190a766de 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md @@ -171,7 +171,7 @@ public: // } // } // } -use std::{rc::Rc, cell::RefCell, collections::VecDeque}; +use std::{ rc::Rc, cell::RefCell, collections::VecDeque }; impl Solution { #[allow(dead_code)] pub fn level_order_bottom(root: Option>>) -> Vec> { diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.rs b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.rs index b00041599620b..0b872fd97bbff 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.rs +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.rs @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -16,7 +16,7 @@ // } // } // } -use std::{rc::Rc, cell::RefCell, collections::VecDeque}; +use std::{ rc::Rc, cell::RefCell, collections::VecDeque }; impl Solution { #[allow(dead_code)] pub fn level_order_bottom(root: Option>>) -> Vec> { @@ -51,4 +51,4 @@ impl Solution { ret_vec.reverse(); ret_vec } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md index 5eae7be73cf24..5ca3933b216d7 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md @@ -270,11 +270,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::to_bst(nums, start, mid), - right: Self::to_bst(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::to_bst(nums, start, mid), + right: Self::to_bst(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md index db76414f26bf2..e8709c0726d1b 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md @@ -242,11 +242,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::to_bst(nums, start, mid), - right: Self::to_bst(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::to_bst(nums, start, mid), + right: Self::to_bst(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.rs b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.rs index 2c6dba49494d2..8c1898c14351d 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.rs +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.rs @@ -24,11 +24,15 @@ impl Solution { return None; } let mid = start + (end - start) / 2; - Some(Rc::new(RefCell::new(TreeNode { - val: nums[mid], - left: Self::to_bst(nums, start, mid), - right: Self::to_bst(nums, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: nums[mid], + left: Self::to_bst(nums, start, mid), + right: Self::to_bst(nums, mid + 1, end), + }) + ) + ) } pub fn sorted_array_to_bst(nums: Vec) -> Option>> { diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md index bbfc48832fb07..b1470c6e469b1 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md @@ -312,15 +312,19 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if (start == end) { + if start == end { return None; } let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[mid], + left: Self::build(vals, start, mid), + right: Self::build(vals, mid + 1, end), + }) + ) + ) } pub fn sorted_list_to_bst(head: Option>) -> Option>> { diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md index 59351906c82f5..7ae1ce0a51866 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md @@ -298,15 +298,19 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if (start == end) { + if start == end { return None; } let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[mid], + left: Self::build(vals, start, mid), + right: Self::build(vals, mid + 1, end), + }) + ) + ) } pub fn sorted_list_to_bst(head: Option>) -> Option>> { diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs index a5e56b6bae869..cb37a4deb5439 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs @@ -36,15 +36,19 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if (start == end) { + if start == end { return None; } let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[mid], + left: Self::build(vals, start, mid), + right: Self::build(vals, mid + 1, end), + }) + ) + ) } pub fn sorted_list_to_bst(head: Option>) -> Option>> { diff --git a/solution/0100-0199/0112.Path Sum/README.md b/solution/0100-0199/0112.Path Sum/README.md index 594e2207ac96d..80cbf144f5952 100644 --- a/solution/0100-0199/0112.Path Sum/README.md +++ b/solution/0100-0199/0112.Path Sum/README.md @@ -240,8 +240,8 @@ impl Solution { return target_sum - node.val == 0; } let val = node.val; - Self::has_path_sum(node.left.take(), target_sum - val) - || Self::has_path_sum(node.right.take(), target_sum - val) + Self::has_path_sum(node.left.take(), target_sum - val) || + Self::has_path_sum(node.right.take(), target_sum - val) } } } diff --git a/solution/0100-0199/0112.Path Sum/README_EN.md b/solution/0100-0199/0112.Path Sum/README_EN.md index 7fdf5c5dfcf72..729a9dc91a565 100644 --- a/solution/0100-0199/0112.Path Sum/README_EN.md +++ b/solution/0100-0199/0112.Path Sum/README_EN.md @@ -225,8 +225,8 @@ impl Solution { return target_sum - node.val == 0; } let val = node.val; - Self::has_path_sum(node.left.take(), target_sum - val) - || Self::has_path_sum(node.right.take(), target_sum - val) + Self::has_path_sum(node.left.take(), target_sum - val) || + Self::has_path_sum(node.right.take(), target_sum - val) } } } diff --git a/solution/0100-0199/0112.Path Sum/Solution.rs b/solution/0100-0199/0112.Path Sum/Solution.rs index 258035d60ecec..a43a5bff6b471 100644 --- a/solution/0100-0199/0112.Path Sum/Solution.rs +++ b/solution/0100-0199/0112.Path Sum/Solution.rs @@ -29,8 +29,8 @@ impl Solution { return target_sum - node.val == 0; } let val = node.val; - Self::has_path_sum(node.left.take(), target_sum - val) - || Self::has_path_sum(node.right.take(), target_sum - val) + Self::has_path_sum(node.left.take(), target_sum - val) || + Self::has_path_sum(node.right.take(), target_sum - val) } } } diff --git a/solution/0100-0199/0113.Path Sum II/README.md b/solution/0100-0199/0113.Path Sum II/README.md index 633b78b33043b..8f5e9e900a4f2 100644 --- a/solution/0100-0199/0113.Path Sum II/README.md +++ b/solution/0100-0199/0113.Path Sum II/README.md @@ -228,9 +228,9 @@ use std::cell::RefCell; impl Solution { fn dfs( root: Option>>, - paths: &mut Vec, + paths: &mut Vec, mut target_sum: i32, - res: &mut Vec>, + res: &mut Vec> ) { if let Some(node) = root { let mut node = node.borrow_mut(); @@ -255,7 +255,7 @@ impl Solution { pub fn path_sum(root: Option>>, target_sum: i32) -> Vec> { let mut res = vec![]; let mut paths = vec![]; - Self::dfs(root, &mut paths, target_sum, &mut res); + Self::dfs(root, &mut paths, target_sum, &mut res); res } } diff --git a/solution/0100-0199/0113.Path Sum II/README_EN.md b/solution/0100-0199/0113.Path Sum II/README_EN.md index 03309cea250af..8a2de0c0af4c8 100644 --- a/solution/0100-0199/0113.Path Sum II/README_EN.md +++ b/solution/0100-0199/0113.Path Sum II/README_EN.md @@ -211,9 +211,9 @@ use std::cell::RefCell; impl Solution { fn dfs( root: Option>>, - paths: &mut Vec, + paths: &mut Vec, mut target_sum: i32, - res: &mut Vec>, + res: &mut Vec> ) { if let Some(node) = root { let mut node = node.borrow_mut(); @@ -238,7 +238,7 @@ impl Solution { pub fn path_sum(root: Option>>, target_sum: i32) -> Vec> { let mut res = vec![]; let mut paths = vec![]; - Self::dfs(root, &mut paths, target_sum, &mut res); + Self::dfs(root, &mut paths, target_sum, &mut res); res } } diff --git a/solution/0100-0199/0113.Path Sum II/Solution.rs b/solution/0100-0199/0113.Path Sum II/Solution.rs index 42e4d8780f760..37d4acd5f5960 100644 --- a/solution/0100-0199/0113.Path Sum II/Solution.rs +++ b/solution/0100-0199/0113.Path Sum II/Solution.rs @@ -21,9 +21,9 @@ use std::cell::RefCell; impl Solution { fn dfs( root: Option>>, - paths: &mut Vec, + paths: &mut Vec, mut target_sum: i32, - res: &mut Vec>, + res: &mut Vec> ) { if let Some(node) = root { let mut node = node.borrow_mut(); @@ -48,7 +48,7 @@ impl Solution { pub fn path_sum(root: Option>>, target_sum: i32) -> Vec> { let mut res = vec![]; let mut paths = vec![]; - Self::dfs(root, &mut paths, target_sum, &mut res); + Self::dfs(root, &mut paths, target_sum, &mut res); res } } diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md index 4341b775b1755..74da7d185f505 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md @@ -207,7 +207,10 @@ impl Solution { } #[allow(dead_code)] - fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + fn pre_order_traverse( + v: &mut Vec>>>, + root: &Option>> + ) { if root.is_none() { return; } diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md index 4a7e406cae59d..46758c585bdd3 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md @@ -183,7 +183,10 @@ impl Solution { } #[allow(dead_code)] - fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + fn pre_order_traverse( + v: &mut Vec>>>, + root: &Option>> + ) { if root.is_none() { return; } diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs index b1d090e0a1ed3..54e3a815dce4d 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.rs @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -36,7 +36,10 @@ impl Solution { } #[allow(dead_code)] - fn pre_order_traverse(v: &mut Vec>>>, root: &Option>>) { + fn pre_order_traverse( + v: &mut Vec>>>, + root: &Option>> + ) { if root.is_none() { return; } @@ -46,4 +49,4 @@ impl Solution { Self::pre_order_traverse(v, &left); Self::pre_order_traverse(v, &right); } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0115.Distinct Subsequences/README.md b/solution/0100-0199/0115.Distinct Subsequences/README.md index fe401239bdb97..01bb3a86772d8 100644 --- a/solution/0100-0199/0115.Distinct Subsequences/README.md +++ b/solution/0100-0199/0115.Distinct Subsequences/README.md @@ -298,7 +298,7 @@ impl Solution { dp[i - 1][j] + dp[i - 1][j - 1] } else { dp[i - 1][j] - } + }; } } diff --git a/solution/0100-0199/0115.Distinct Subsequences/README_EN.md b/solution/0100-0199/0115.Distinct Subsequences/README_EN.md index 70d713ae688e1..01e2b9f6a1b7a 100644 --- a/solution/0100-0199/0115.Distinct Subsequences/README_EN.md +++ b/solution/0100-0199/0115.Distinct Subsequences/README_EN.md @@ -312,7 +312,7 @@ impl Solution { dp[i - 1][j] + dp[i - 1][j - 1] } else { dp[i - 1][j] - } + }; } } diff --git a/solution/0100-0199/0115.Distinct Subsequences/Solution.rs b/solution/0100-0199/0115.Distinct Subsequences/Solution.rs index 24e23eb6c170c..847de9a8ca173 100644 --- a/solution/0100-0199/0115.Distinct Subsequences/Solution.rs +++ b/solution/0100-0199/0115.Distinct Subsequences/Solution.rs @@ -17,10 +17,10 @@ impl Solution { dp[i - 1][j] + dp[i - 1][j - 1] } else { dp[i - 1][j] - } + }; } } dp[n][m] as i32 } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0118.Pascal's Triangle/Solution.rs b/solution/0100-0199/0118.Pascal's Triangle/Solution.rs index 600f46f2e488b..695167530c1f3 100644 --- a/solution/0100-0199/0118.Pascal's Triangle/Solution.rs +++ b/solution/0100-0199/0118.Pascal's Triangle/Solution.rs @@ -15,4 +15,4 @@ impl Solution { ret_vec } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0119.Pascal's Triangle II/Solution.rs b/solution/0100-0199/0119.Pascal's Triangle II/Solution.rs index 9b5d65790edc2..2a97b746da756 100644 --- a/solution/0100-0199/0119.Pascal's Triangle II/Solution.rs +++ b/solution/0100-0199/0119.Pascal's Triangle II/Solution.rs @@ -1,12 +1,12 @@ -impl Solution { - pub fn get_row(row_index: i32) -> Vec { - let n = (row_index + 1) as usize; - let mut f = vec![1; n]; - for i in 2..n { - for j in (1..i).rev() { - f[j] += f[j - 1]; - } - } - f - } -} +impl Solution { + pub fn get_row(row_index: i32) -> Vec { + let n = (row_index + 1) as usize; + let mut f = vec![1; n]; + for i in 2..n { + for j in (1..i).rev() { + f[j] += f[j - 1]; + } + } + f + } +} diff --git a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md index eedccc4139ad7..a2a5d0b4a7fef 100644 --- a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md +++ b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md @@ -319,7 +319,7 @@ impl Solution { pub fn max_profit(prices: Vec) -> i32 { let mut res = 0; for i in 1..prices.len() { - res += 0.max(prices[i] - prices[i - 1]); + res += (0).max(prices[i] - prices[i - 1]); } res } diff --git a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md index 332cfced5165e..2f9b1279c37e9 100644 --- a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md +++ b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md @@ -312,7 +312,7 @@ impl Solution { pub fn max_profit(prices: Vec) -> i32 { let mut res = 0; for i in 1..prices.len() { - res += 0.max(prices[i] - prices[i - 1]); + res += (0).max(prices[i] - prices[i - 1]); } res } diff --git a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/Solution.rs b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/Solution.rs index d1d1e11b16ea6..b24fee02a1c85 100644 --- a/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/Solution.rs +++ b/solution/0100-0199/0122.Best Time to Buy and Sell Stock II/Solution.rs @@ -2,7 +2,7 @@ impl Solution { pub fn max_profit(prices: Vec) -> i32 { let mut res = 0; for i in 1..prices.len() { - res += 0.max(prices[i] - prices[i - 1]); + res += (0).max(prices[i] - prices[i - 1]); } res } diff --git a/solution/0100-0199/0123.Best Time to Buy and Sell Stock III/Solution.rs b/solution/0100-0199/0123.Best Time to Buy and Sell Stock III/Solution.rs index 1d5f2fff47a31..7fd33dee14f73 100644 --- a/solution/0100-0199/0123.Best Time to Buy and Sell Stock III/Solution.rs +++ b/solution/0100-0199/0123.Best Time to Buy and Sell Stock III/Solution.rs @@ -16,4 +16,4 @@ impl Solution { f4 } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README.md b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README.md index c445cbdf520fc..6db9f3df22e1a 100644 --- a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README.md +++ b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README.md @@ -323,8 +323,8 @@ impl Solution { return 0; } let node = root.as_ref().unwrap().borrow(); - let left = 0.max(Self::dfs(&node.left, res)); - let right = 0.max(Self::dfs(&node.right, res)); + let left = (0).max(Self::dfs(&node.left, res)); + let right = (0).max(Self::dfs(&node.right, res)); *res = (node.val + left + right).max(*res); node.val + left.max(right) } diff --git a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md index 23f32ed4c65f6..3d1c996086686 100644 --- a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md +++ b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md @@ -314,8 +314,8 @@ impl Solution { return 0; } let node = root.as_ref().unwrap().borrow(); - let left = 0.max(Self::dfs(&node.left, res)); - let right = 0.max(Self::dfs(&node.right, res)); + let left = (0).max(Self::dfs(&node.left, res)); + let right = (0).max(Self::dfs(&node.right, res)); *res = (node.val + left + right).max(*res); node.val + left.max(right) } diff --git a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/Solution.rs b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/Solution.rs index 49603aa02c3ef..5b24aa51d2e32 100644 --- a/solution/0100-0199/0124.Binary Tree Maximum Path Sum/Solution.rs +++ b/solution/0100-0199/0124.Binary Tree Maximum Path Sum/Solution.rs @@ -24,8 +24,8 @@ impl Solution { return 0; } let node = root.as_ref().unwrap().borrow(); - let left = 0.max(Self::dfs(&node.left, res)); - let right = 0.max(Self::dfs(&node.right, res)); + let left = (0).max(Self::dfs(&node.left, res)); + let right = (0).max(Self::dfs(&node.right, res)); *res = (node.val + left + right).max(*res); node.val + left.max(right) } diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/README.md b/solution/0100-0199/0128.Longest Consecutive Sequence/README.md index af57d0408c23b..d57edb2367a6a 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/README.md +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/README.md @@ -217,7 +217,7 @@ impl Solution { } let mut cur_num = num.clone(); while s.contains(&cur_num) { - cur_num += 1; + cur_num += 1; } // Update the answer ret = std::cmp::max(ret, cur_num - num); diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md b/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md index dfe9ea6d94620..9691e3a2b21aa 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md @@ -208,7 +208,7 @@ impl Solution { } let mut cur_num = num.clone(); while s.contains(&cur_num) { - cur_num += 1; + cur_num += 1; } // Update the answer ret = std::cmp::max(ret, cur_num - num); diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.rs b/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.rs index b3e8d984dcf0e..48b64c1980532 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.rs +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.rs @@ -17,7 +17,7 @@ impl Solution { } let mut cur_num = num.clone(); while s.contains(&cur_num) { - cur_num += 1; + cur_num += 1; } // Update the answer ret = std::cmp::max(ret, cur_num - num); @@ -25,4 +25,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0136.Single Number/README.md b/solution/0100-0199/0136.Single Number/README.md index c521433a81668..4bd6ca358d480 100644 --- a/solution/0100-0199/0136.Single Number/README.md +++ b/solution/0100-0199/0136.Single Number/README.md @@ -149,7 +149,9 @@ function singleNumber(nums: number[]): number { ```rust impl Solution { pub fn single_number(nums: Vec) -> i32 { - nums.into_iter().reduce(|r, v| r ^ v).unwrap() + nums.into_iter() + .reduce(|r, v| r ^ v) + .unwrap() } } ``` diff --git a/solution/0100-0199/0136.Single Number/README_EN.md b/solution/0100-0199/0136.Single Number/README_EN.md index 31a6386f58b35..cf670ec826988 100644 --- a/solution/0100-0199/0136.Single Number/README_EN.md +++ b/solution/0100-0199/0136.Single Number/README_EN.md @@ -124,7 +124,9 @@ function singleNumber(nums: number[]): number { ```rust impl Solution { pub fn single_number(nums: Vec) -> i32 { - nums.into_iter().reduce(|r, v| r ^ v).unwrap() + nums.into_iter() + .reduce(|r, v| r ^ v) + .unwrap() } } ``` diff --git a/solution/0100-0199/0136.Single Number/Solution.rs b/solution/0100-0199/0136.Single Number/Solution.rs index 7285716dc4d1d..92067bc24963a 100644 --- a/solution/0100-0199/0136.Single Number/Solution.rs +++ b/solution/0100-0199/0136.Single Number/Solution.rs @@ -1,5 +1,7 @@ impl Solution { pub fn single_number(nums: Vec) -> i32 { - nums.into_iter().reduce(|r, v| r ^ v).unwrap() + nums.into_iter() + .reduce(|r, v| r ^ v) + .unwrap() } } diff --git a/solution/0100-0199/0137.Single Number II/README.md b/solution/0100-0199/0137.Single Number II/README.md index 371af3b7a9ce8..f7442b4cc2a1f 100644 --- a/solution/0100-0199/0137.Single Number II/README.md +++ b/solution/0100-0199/0137.Single Number II/README.md @@ -251,7 +251,10 @@ impl Solution { pub fn single_number(nums: Vec) -> i32 { let mut ans = 0; for i in 0..32 { - let count = nums.iter().map(|v| v >> i & 1).sum::(); + let count = nums + .iter() + .map(|v| (v >> i) & 1) + .sum::(); ans |= count % 3 << i; } ans diff --git a/solution/0100-0199/0137.Single Number II/README_EN.md b/solution/0100-0199/0137.Single Number II/README_EN.md index f17099282ed8c..2d94ec1673113 100644 --- a/solution/0100-0199/0137.Single Number II/README_EN.md +++ b/solution/0100-0199/0137.Single Number II/README_EN.md @@ -232,7 +232,10 @@ impl Solution { pub fn single_number(nums: Vec) -> i32 { let mut ans = 0; for i in 0..32 { - let count = nums.iter().map(|v| v >> i & 1).sum::(); + let count = nums + .iter() + .map(|v| (v >> i) & 1) + .sum::(); ans |= count % 3 << i; } ans diff --git a/solution/0100-0199/0137.Single Number II/Solution.rs b/solution/0100-0199/0137.Single Number II/Solution.rs index 5a9835dd2f449..392656403e206 100644 --- a/solution/0100-0199/0137.Single Number II/Solution.rs +++ b/solution/0100-0199/0137.Single Number II/Solution.rs @@ -2,7 +2,10 @@ impl Solution { pub fn single_number(nums: Vec) -> i32 { let mut ans = 0; for i in 0..32 { - let count = nums.iter().map(|v| v >> i & 1).sum::(); + let count = nums + .iter() + .map(|v| (v >> i) & 1) + .sum::(); ans |= count % 3 << i; } ans diff --git a/solution/0100-0199/0143.Reorder List/README.md b/solution/0100-0199/0143.Reorder List/README.md index b445ae5705ef5..7dc4b951a86b4 100644 --- a/solution/0100-0199/0143.Reorder List/README.md +++ b/solution/0100-0199/0143.Reorder List/README.md @@ -452,11 +452,7 @@ impl Solution { } let mut flag = false; while !deque.is_empty() { - *tail = if flag { - deque.pop_front().unwrap() - } else { - deque.pop_back().unwrap() - }; + *tail = if flag { deque.pop_front().unwrap() } else { deque.pop_back().unwrap() }; tail = &mut tail.as_mut().unwrap().next; flag = !flag; } diff --git a/solution/0100-0199/0143.Reorder List/README_EN.md b/solution/0100-0199/0143.Reorder List/README_EN.md index 888951f29696c..cfdc205d1d625 100644 --- a/solution/0100-0199/0143.Reorder List/README_EN.md +++ b/solution/0100-0199/0143.Reorder List/README_EN.md @@ -405,11 +405,7 @@ impl Solution { } let mut flag = false; while !deque.is_empty() { - *tail = if flag { - deque.pop_front().unwrap() - } else { - deque.pop_back().unwrap() - }; + *tail = if flag { deque.pop_front().unwrap() } else { deque.pop_back().unwrap() }; tail = &mut tail.as_mut().unwrap().next; flag = !flag; } diff --git a/solution/0100-0199/0143.Reorder List/Solution.rs b/solution/0100-0199/0143.Reorder List/Solution.rs index 63b957184e58b..1c58aa53b92f9 100644 --- a/solution/0100-0199/0143.Reorder List/Solution.rs +++ b/solution/0100-0199/0143.Reorder List/Solution.rs @@ -27,11 +27,7 @@ impl Solution { } let mut flag = false; while !deque.is_empty() { - *tail = if flag { - deque.pop_front().unwrap() - } else { - deque.pop_back().unwrap() - }; + *tail = if flag { deque.pop_front().unwrap() } else { deque.pop_back().unwrap() }; tail = &mut tail.as_mut().unwrap().next; flag = !flag; } diff --git a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md index 06702deb44561..1902d056ccd06 100644 --- a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md +++ b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md @@ -545,7 +545,7 @@ impl Solution { let next = root.as_mut().unwrap().borrow_mut().left.take(); stack.push(root); root = next; - } else { + } else { root = stack.pop().unwrap(); let next = root.as_mut().unwrap().borrow_mut().right.take(); if next.is_some() { diff --git a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md index 8f9218a13fd17..9bb1c9038dc46 100644 --- a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md +++ b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md @@ -513,7 +513,7 @@ impl Solution { let next = root.as_mut().unwrap().borrow_mut().left.take(); stack.push(root); root = next; - } else { + } else { root = stack.pop().unwrap(); let next = root.as_mut().unwrap().borrow_mut().right.take(); if next.is_some() { diff --git a/solution/0100-0199/0146.LRU Cache/README.md b/solution/0100-0199/0146.LRU Cache/README.md index 9aefd15d5b605..9e59aa2756e73 100644 --- a/solution/0100-0199/0146.LRU Cache/README.md +++ b/solution/0100-0199/0146.LRU Cache/README.md @@ -364,9 +364,7 @@ impl LRUCache { None => None, } } -} - -/** +}/** * Your LRUCache object will be instantiated and called as such: * let obj = LRUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0100-0199/0146.LRU Cache/README_EN.md b/solution/0100-0199/0146.LRU Cache/README_EN.md index 06fb9db04cc7d..b08cde0ccc38b 100644 --- a/solution/0100-0199/0146.LRU Cache/README_EN.md +++ b/solution/0100-0199/0146.LRU Cache/README_EN.md @@ -337,9 +337,7 @@ impl LRUCache { None => None, } } -} - -/** +}/** * Your LRUCache object will be instantiated and called as such: * let obj = LRUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0100-0199/0146.LRU Cache/Solution.rs b/solution/0100-0199/0146.LRU Cache/Solution.rs index 72881e2b53847..76b8fecb7769d 100644 --- a/solution/0100-0199/0146.LRU Cache/Solution.rs +++ b/solution/0100-0199/0146.LRU Cache/Solution.rs @@ -120,9 +120,7 @@ impl LRUCache { None => None, } } -} - -/** +}/** * Your LRUCache object will be instantiated and called as such: * let obj = LRUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0100-0199/0148.Sort List/README.md b/solution/0100-0199/0148.Sort List/README.md index bfd721e36daa7..8e048e0010640 100644 --- a/solution/0100-0199/0148.Sort List/README.md +++ b/solution/0100-0199/0148.Sort List/README.md @@ -415,7 +415,7 @@ impl Solution { cur = &cur.as_ref().unwrap().next; } let mut cur = &mut head; - for _ in 0..(length / 2 - 1) { + for _ in 0..length / 2 - 1 { cur = &mut cur.as_mut().unwrap().next; } let right = cur.as_mut().unwrap().next.take(); diff --git a/solution/0100-0199/0148.Sort List/README_EN.md b/solution/0100-0199/0148.Sort List/README_EN.md index 99f030b93fc77..4fa11027d1647 100644 --- a/solution/0100-0199/0148.Sort List/README_EN.md +++ b/solution/0100-0199/0148.Sort List/README_EN.md @@ -399,7 +399,7 @@ impl Solution { cur = &cur.as_ref().unwrap().next; } let mut cur = &mut head; - for _ in 0..(length / 2 - 1) { + for _ in 0..length / 2 - 1 { cur = &mut cur.as_mut().unwrap().next; } let right = cur.as_mut().unwrap().next.take(); diff --git a/solution/0100-0199/0148.Sort List/Solution.rs b/solution/0100-0199/0148.Sort List/Solution.rs index 819aebb8b81e2..bb43a748cb3e4 100644 --- a/solution/0100-0199/0148.Sort List/Solution.rs +++ b/solution/0100-0199/0148.Sort List/Solution.rs @@ -44,7 +44,7 @@ impl Solution { cur = &cur.as_ref().unwrap().next; } let mut cur = &mut head; - for _ in 0..(length / 2 - 1) { + for _ in 0..length / 2 - 1 { cur = &mut cur.as_mut().unwrap().next; } let right = cur.as_mut().unwrap().next.take(); diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md index 33914b03b8071..b27fe526c5c91 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md @@ -318,7 +318,7 @@ impl Solution { "*" => b * a, "/" => b / a, _ => 0, - }) + }); } } } diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md index e45d60a94be78..4cc50b8f199da 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md @@ -286,7 +286,7 @@ impl Solution { "*" => b * a, "/" => b / a, _ => 0, - }) + }); } } } diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.rs b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.rs index b4ae2ddf9a0b8..abec1e8515b6e 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.rs +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.rs @@ -13,7 +13,7 @@ impl Solution { "*" => b * a, "/" => b / a, _ => 0, - }) + }); } } } diff --git a/solution/0100-0199/0152.Maximum Product Subarray/Solution.rs b/solution/0100-0199/0152.Maximum Product Subarray/Solution.rs index b707c0a2e2f20..e4aac2866e491 100644 --- a/solution/0100-0199/0152.Maximum Product Subarray/Solution.rs +++ b/solution/0100-0199/0152.Maximum Product Subarray/Solution.rs @@ -1,14 +1,14 @@ -impl Solution { - pub fn max_product(nums: Vec) -> i32 { - let mut f = nums[0]; - let mut g = nums[0]; - let mut ans = nums[0]; - for &x in nums.iter().skip(1) { - let (ff, gg) = (f, g); - f = x.max(x * ff).max(x * gg); - g = x.min(x * ff).min(x * gg); - ans = ans.max(f); - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn max_product(nums: Vec) -> i32 { + let mut f = nums[0]; + let mut g = nums[0]; + let mut ans = nums[0]; + for &x in nums.iter().skip(1) { + let (ff, gg) = (f, g); + f = x.max(x * ff).max(x * gg); + g = x.min(x * ff).min(x * gg); + ans = ans.max(f); + } + ans + } +} diff --git a/solution/0100-0199/0155.Min Stack/README.md b/solution/0100-0199/0155.Min Stack/README.md index 677a176c63206..7d57f1a226bb8 100644 --- a/solution/0100-0199/0155.Min Stack/README.md +++ b/solution/0100-0199/0155.Min Stack/README.md @@ -324,13 +324,11 @@ struct MinStack { stk2: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - fn new() -> Self { Self { stk1: VecDeque::new(), stk2: VecDeque::new() } } @@ -356,9 +354,7 @@ impl MinStack { fn get_min(&self) -> i32 { *self.stk2.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); diff --git a/solution/0100-0199/0155.Min Stack/README_EN.md b/solution/0100-0199/0155.Min Stack/README_EN.md index 96c94ded44856..f1697dd7c33bf 100644 --- a/solution/0100-0199/0155.Min Stack/README_EN.md +++ b/solution/0100-0199/0155.Min Stack/README_EN.md @@ -305,13 +305,11 @@ struct MinStack { stk2: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - fn new() -> Self { Self { stk1: VecDeque::new(), stk2: VecDeque::new() } } @@ -337,9 +335,7 @@ impl MinStack { fn get_min(&self) -> i32 { *self.stk2.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); diff --git a/solution/0100-0199/0155.Min Stack/Solution.rs b/solution/0100-0199/0155.Min Stack/Solution.rs index de2ff0c383468..9f43fb8a60104 100644 --- a/solution/0100-0199/0155.Min Stack/Solution.rs +++ b/solution/0100-0199/0155.Min Stack/Solution.rs @@ -4,13 +4,11 @@ struct MinStack { stk2: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MinStack { - fn new() -> Self { Self { stk1: VecDeque::new(), stk2: VecDeque::new() } } @@ -36,13 +34,11 @@ impl MinStack { fn get_min(&self) -> i32 { *self.stk2.back().unwrap() } -} - -/** +}/** * Your MinStack object will be instantiated and called as such: * let obj = MinStack::new(); * obj.push(x); * obj.pop(); * let ret_3: i32 = obj.top(); * let ret_4: i32 = obj.get_min(); - */ \ No newline at end of file + */ diff --git a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README.md b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README.md index eb5e18bb4d644..c61b7107f3d89 100644 --- a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README.md +++ b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README.md @@ -308,12 +308,18 @@ impl Solution { let mut r = n - 1; loop { match (numbers[l] + numbers[r]).cmp(&target) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, - Ordering::Equal => break, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } + Ordering::Equal => { + break; + } } } - vec![l as i32 + 1, r as i32 + 1] + vec![(l as i32) + 1, (r as i32) + 1] } } ``` diff --git a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README_EN.md b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README_EN.md index c85ec0eea86b1..45b0058f47232 100644 --- a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README_EN.md +++ b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README_EN.md @@ -301,12 +301,18 @@ impl Solution { let mut r = n - 1; loop { match (numbers[l] + numbers[r]).cmp(&target) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, - Ordering::Equal => break, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } + Ordering::Equal => { + break; + } } } - vec![l as i32 + 1, r as i32 + 1] + vec![(l as i32) + 1, (r as i32) + 1] } } ``` diff --git a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.rs b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.rs index f6c7ab68f56dc..23e1fe074533e 100644 --- a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.rs +++ b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.rs @@ -7,11 +7,17 @@ impl Solution { let mut r = n - 1; loop { match (numbers[l] + numbers[r]).cmp(&target) { - Ordering::Less => l += 1, - Ordering::Greater => r -= 1, - Ordering::Equal => break, + Ordering::Less => { + l += 1; + } + Ordering::Greater => { + r -= 1; + } + Ordering::Equal => { + break; + } } } - vec![l as i32 + 1, r as i32 + 1] + vec![(l as i32) + 1, (r as i32) + 1] } } diff --git a/solution/0100-0199/0168.Excel Sheet Column Title/README.md b/solution/0100-0199/0168.Excel Sheet Column Title/README.md index 606d8e36aef9c..a6cda436428d7 100644 --- a/solution/0100-0199/0168.Excel Sheet Column Title/README.md +++ b/solution/0100-0199/0168.Excel Sheet Column Title/README.md @@ -109,12 +109,12 @@ impl Solution { while column_number > 0 { if column_number <= 26 { - ret.push(('A' as u8 + column_number as u8 - 1) as char); + ret.push((('A' as u8) + (column_number as u8) - 1) as char); break; } else { let mut left = column_number % 26; left = if left == 0 { 26 } else { left }; - ret.push(('A' as u8 + left as u8 - 1) as char); + ret.push((('A' as u8) + (left as u8) - 1) as char); column_number = (column_number - 1) / 26; } } diff --git a/solution/0100-0199/0168.Excel Sheet Column Title/README_EN.md b/solution/0100-0199/0168.Excel Sheet Column Title/README_EN.md index 7723450ea3d0a..f73f4fb5f7903 100644 --- a/solution/0100-0199/0168.Excel Sheet Column Title/README_EN.md +++ b/solution/0100-0199/0168.Excel Sheet Column Title/README_EN.md @@ -92,12 +92,12 @@ impl Solution { while column_number > 0 { if column_number <= 26 { - ret.push(('A' as u8 + column_number as u8 - 1) as char); + ret.push((('A' as u8) + (column_number as u8) - 1) as char); break; } else { let mut left = column_number % 26; left = if left == 0 { 26 } else { left }; - ret.push(('A' as u8 + left as u8 - 1) as char); + ret.push((('A' as u8) + (left as u8) - 1) as char); column_number = (column_number - 1) / 26; } } diff --git a/solution/0100-0199/0168.Excel Sheet Column Title/Solution.rs b/solution/0100-0199/0168.Excel Sheet Column Title/Solution.rs index 260f574331c05..9ed08f0549396 100644 --- a/solution/0100-0199/0168.Excel Sheet Column Title/Solution.rs +++ b/solution/0100-0199/0168.Excel Sheet Column Title/Solution.rs @@ -6,16 +6,16 @@ impl Solution { while column_number > 0 { if column_number <= 26 { - ret.push(('A' as u8 + column_number as u8 - 1) as char); + ret.push((('A' as u8) + (column_number as u8) - 1) as char); break; } else { let mut left = column_number % 26; left = if left == 0 { 26 } else { left }; - ret.push(('A' as u8 + left as u8 - 1) as char); + ret.push((('A' as u8) + (left as u8) - 1) as char); column_number = (column_number - 1) / 26; } } ret.chars().rev().collect() } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0169.Majority Element/Solution.rs b/solution/0100-0199/0169.Majority Element/Solution.rs index ee7eac8ec1bf8..6f92fe63846de 100644 --- a/solution/0100-0199/0169.Majority Element/Solution.rs +++ b/solution/0100-0199/0169.Majority Element/Solution.rs @@ -12,4 +12,4 @@ impl Solution { } m } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0173.Binary Search Tree Iterator/README.md b/solution/0100-0199/0173.Binary Search Tree Iterator/README.md index 9d82d601fbe1b..80a8a21006423 100644 --- a/solution/0100-0199/0173.Binary Search Tree Iterator/README.md +++ b/solution/0100-0199/0173.Binary Search Tree Iterator/README.md @@ -605,9 +605,7 @@ impl BSTIterator { fn has_next(&self) -> bool { self.index != self.vals.len() } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); @@ -647,7 +645,7 @@ use std::cell::RefCell; impl BSTIterator { fn dfs( mut root: Option>>, - stack: &mut Vec>>>, + stack: &mut Vec>>> ) { if root.is_some() { let left = root.as_mut().unwrap().borrow_mut().left.take(); @@ -666,7 +664,7 @@ impl BSTIterator { let node = self.stack.pop().unwrap().unwrap(); let mut node = node.borrow_mut(); if node.right.is_some() { - Self::dfs(node.right.take(), &mut self.stack) + Self::dfs(node.right.take(), &mut self.stack); } node.val } @@ -674,9 +672,7 @@ impl BSTIterator { fn has_next(&self) -> bool { self.stack.len() != 0 } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); diff --git a/solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md b/solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md index d145995f98873..3612731ccf011 100644 --- a/solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md +++ b/solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md @@ -578,9 +578,7 @@ impl BSTIterator { fn has_next(&self) -> bool { self.index != self.vals.len() } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); @@ -620,7 +618,7 @@ use std::cell::RefCell; impl BSTIterator { fn dfs( mut root: Option>>, - stack: &mut Vec>>>, + stack: &mut Vec>>> ) { if root.is_some() { let left = root.as_mut().unwrap().borrow_mut().left.take(); @@ -639,7 +637,7 @@ impl BSTIterator { let node = self.stack.pop().unwrap().unwrap(); let mut node = node.borrow_mut(); if node.right.is_some() { - Self::dfs(node.right.take(), &mut self.stack) + Self::dfs(node.right.take(), &mut self.stack); } node.val } @@ -647,9 +645,7 @@ impl BSTIterator { fn has_next(&self) -> bool { self.stack.len() != 0 } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); diff --git a/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.rs b/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.rs index 0005c5aabf313..6fb31ff8823d8 100644 --- a/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.rs +++ b/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.rs @@ -29,7 +29,7 @@ use std::cell::RefCell; impl BSTIterator { fn dfs( mut root: Option>>, - stack: &mut Vec>>>, + stack: &mut Vec>>> ) { if root.is_some() { let left = root.as_mut().unwrap().borrow_mut().left.take(); @@ -48,7 +48,7 @@ impl BSTIterator { let node = self.stack.pop().unwrap().unwrap(); let mut node = node.borrow_mut(); if node.right.is_some() { - Self::dfs(node.right.take(), &mut self.stack) + Self::dfs(node.right.take(), &mut self.stack); } node.val } @@ -56,11 +56,9 @@ impl BSTIterator { fn has_next(&self) -> bool { self.stack.len() != 0 } -} - -/** +}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); * let ret_2: bool = obj.has_next(); - */ \ No newline at end of file + */ diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/README.md b/solution/0100-0199/0187.Repeated DNA Sequences/README.md index 8c85fafdebb0c..b4d8a2227d50f 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/README.md +++ b/solution/0100-0199/0187.Repeated DNA Sequences/README.md @@ -224,7 +224,7 @@ use std::collections::HashMap; impl Solution { pub fn find_repeated_dna_sequences(s: String) -> Vec { if s.len() < 10 { - return vec![] + return vec![]; } let mut cnt = HashMap::new(); let mut ans = Vec::new(); diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md b/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md index da99ea242b6cc..ad6428947fd9e 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md +++ b/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md @@ -207,7 +207,7 @@ use std::collections::HashMap; impl Solution { pub fn find_repeated_dna_sequences(s: String) -> Vec { if s.len() < 10 { - return vec![] + return vec![]; } let mut cnt = HashMap::new(); let mut ans = Vec::new(); diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/Solution.rs b/solution/0100-0199/0187.Repeated DNA Sequences/Solution.rs index 202793d28e05f..fa395e49d38c5 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/Solution.rs +++ b/solution/0100-0199/0187.Repeated DNA Sequences/Solution.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; impl Solution { pub fn find_repeated_dna_sequences(s: String) -> Vec { if s.len() < 10 { - return vec![] + return vec![]; } let mut cnt = HashMap::new(); let mut ans = Vec::new(); @@ -17,4 +17,4 @@ impl Solution { } ans } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0189.Rotate Array/README.md b/solution/0100-0199/0189.Rotate Array/README.md index 9132782d665e1..1d995132f1d79 100644 --- a/solution/0100-0199/0189.Rotate Array/README.md +++ b/solution/0100-0199/0189.Rotate Array/README.md @@ -233,7 +233,7 @@ var rotate = function (nums, k) { impl Solution { pub fn rotate(nums: &mut Vec, k: i32) { let n = nums.len(); - let k = k as usize % n; + let k = (k as usize) % n; nums.reverse(); nums[..k].reverse(); nums[k..].reverse(); diff --git a/solution/0100-0199/0189.Rotate Array/README_EN.md b/solution/0100-0199/0189.Rotate Array/README_EN.md index 1fa282b69be52..228ddad1e55bd 100644 --- a/solution/0100-0199/0189.Rotate Array/README_EN.md +++ b/solution/0100-0199/0189.Rotate Array/README_EN.md @@ -223,7 +223,7 @@ var rotate = function (nums, k) { impl Solution { pub fn rotate(nums: &mut Vec, k: i32) { let n = nums.len(); - let k = k as usize % n; + let k = (k as usize) % n; nums.reverse(); nums[..k].reverse(); nums[k..].reverse(); diff --git a/solution/0100-0199/0189.Rotate Array/Solution.rs b/solution/0100-0199/0189.Rotate Array/Solution.rs index 602fe6d063a67..3d1cd70fa8770 100644 --- a/solution/0100-0199/0189.Rotate Array/Solution.rs +++ b/solution/0100-0199/0189.Rotate Array/Solution.rs @@ -1,9 +1,9 @@ impl Solution { pub fn rotate(nums: &mut Vec, k: i32) { let n = nums.len(); - let k = k as usize % n; + let k = (k as usize) % n; nums.reverse(); nums[..k].reverse(); nums[k..].reverse(); } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0198.House Robber/README.md b/solution/0100-0199/0198.House Robber/README.md index 81f230c8e598a..41021760645fe 100644 --- a/solution/0100-0199/0198.House Robber/README.md +++ b/solution/0100-0199/0198.House Robber/README.md @@ -214,7 +214,7 @@ impl Solution { pub fn rob(nums: Vec) -> i32 { let mut f = [0, 0]; for x in nums { - f = [f[0].max(f[1]), f[0] + x] + f = [f[0].max(f[1]), f[0] + x]; } f[0].max(f[1]) } diff --git a/solution/0100-0199/0198.House Robber/README_EN.md b/solution/0100-0199/0198.House Robber/README_EN.md index ed6312a8115e2..bcb4b857ab958 100644 --- a/solution/0100-0199/0198.House Robber/README_EN.md +++ b/solution/0100-0199/0198.House Robber/README_EN.md @@ -179,7 +179,7 @@ impl Solution { pub fn rob(nums: Vec) -> i32 { let mut f = [0, 0]; for x in nums { - f = [f[0].max(f[1]), f[0] + x] + f = [f[0].max(f[1]), f[0] + x]; } f[0].max(f[1]) } diff --git a/solution/0100-0199/0198.House Robber/Solution.rs b/solution/0100-0199/0198.House Robber/Solution.rs index fe67b113421c0..d1b3a459ce727 100644 --- a/solution/0100-0199/0198.House Robber/Solution.rs +++ b/solution/0100-0199/0198.House Robber/Solution.rs @@ -1,9 +1,9 @@ -impl Solution { - pub fn rob(nums: Vec) -> i32 { - let mut f = [0, 0]; - for x in nums { - f = [f[0].max(f[1]), f[0] + x] - } - f[0].max(f[1]) - } -} +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let mut f = [0, 0]; + for x in nums { + f = [f[0].max(f[1]), f[0] + x]; + } + f[0].max(f[1]) + } +} diff --git a/solution/0200-0299/0200.Number of Islands/README.md b/solution/0200-0299/0200.Number of Islands/README.md index 597034eafbcd4..a9466f8d163a1 100644 --- a/solution/0200-0299/0200.Number of Islands/README.md +++ b/solution/0200-0299/0200.Number of Islands/README.md @@ -659,13 +659,14 @@ impl Solution { fn dfs(grid: &mut Vec>, i: usize, j: usize) { grid[i][j] = '0'; for k in 0..4 { - let x = i as i32 + DIRS[k]; - let y = j as i32 + DIRS[k + 1]; - if x >= 0 - && (x as usize) < grid.len() - && y >= 0 - && (y as usize) < grid[0].len() - && grid[x as usize][y as usize] == '1' + let x = (i as i32) + DIRS[k]; + let y = (j as i32) + DIRS[k + 1]; + if + x >= 0 && + (x as usize) < grid.len() && + y >= 0 && + (y as usize) < grid[0].len() && + grid[x as usize][y as usize] == '1' { dfs(grid, x as usize, y as usize); } @@ -702,13 +703,14 @@ impl Solution { while !queue.is_empty() { let (i, j) = queue.pop_front().unwrap(); for k in 0..4 { - let x = i as i32 + DIRS[k]; - let y = j as i32 + DIRS[k + 1]; - if x >= 0 - && (x as usize) < grid.len() - && y >= 0 - && (y as usize) < grid[0].len() - && grid[x as usize][y as usize] == '1' + let x = (i as i32) + DIRS[k]; + let y = (j as i32) + DIRS[k + 1]; + if + x >= 0 && + (x as usize) < grid.len() && + y >= 0 && + (y as usize) < grid[0].len() && + grid[x as usize][y as usize] == '1' { grid[x as usize][y as usize] = '0'; queue.push_back((x as usize, y as usize)); @@ -744,7 +746,7 @@ impl Solution { let mut p: Vec = (0..(m * n) as i32).collect(); fn find(p: &mut Vec, x: usize) -> i32 { - if p[x] != x as i32 { + if p[x] != (x as i32) { p[x] = find(p, p[x] as usize); } p[x] @@ -769,7 +771,7 @@ impl Solution { let mut ans = 0; for i in 0..m { for j in 0..n { - if grid[i][j] == '1' && p[i * n + j] == (i * n + j) as i32 { + if grid[i][j] == '1' && p[i * n + j] == ((i * n + j) as i32) { ans += 1; } } diff --git a/solution/0200-0299/0200.Number of Islands/README_EN.md b/solution/0200-0299/0200.Number of Islands/README_EN.md index 3da696b654989..003d43872b844 100644 --- a/solution/0200-0299/0200.Number of Islands/README_EN.md +++ b/solution/0200-0299/0200.Number of Islands/README_EN.md @@ -604,13 +604,14 @@ impl Solution { fn dfs(grid: &mut Vec>, i: usize, j: usize) { grid[i][j] = '0'; for k in 0..4 { - let x = i as i32 + DIRS[k]; - let y = j as i32 + DIRS[k + 1]; - if x >= 0 - && (x as usize) < grid.len() - && y >= 0 - && (y as usize) < grid[0].len() - && grid[x as usize][y as usize] == '1' + let x = (i as i32) + DIRS[k]; + let y = (j as i32) + DIRS[k + 1]; + if + x >= 0 && + (x as usize) < grid.len() && + y >= 0 && + (y as usize) < grid[0].len() && + grid[x as usize][y as usize] == '1' { dfs(grid, x as usize, y as usize); } @@ -647,13 +648,14 @@ impl Solution { while !queue.is_empty() { let (i, j) = queue.pop_front().unwrap(); for k in 0..4 { - let x = i as i32 + DIRS[k]; - let y = j as i32 + DIRS[k + 1]; - if x >= 0 - && (x as usize) < grid.len() - && y >= 0 - && (y as usize) < grid[0].len() - && grid[x as usize][y as usize] == '1' + let x = (i as i32) + DIRS[k]; + let y = (j as i32) + DIRS[k + 1]; + if + x >= 0 && + (x as usize) < grid.len() && + y >= 0 && + (y as usize) < grid[0].len() && + grid[x as usize][y as usize] == '1' { grid[x as usize][y as usize] = '0'; queue.push_back((x as usize, y as usize)); @@ -689,7 +691,7 @@ impl Solution { let mut p: Vec = (0..(m * n) as i32).collect(); fn find(p: &mut Vec, x: usize) -> i32 { - if p[x] != x as i32 { + if p[x] != (x as i32) { p[x] = find(p, p[x] as usize); } p[x] @@ -714,7 +716,7 @@ impl Solution { let mut ans = 0; for i in 0..m { for j in 0..n { - if grid[i][j] == '1' && p[i * n + j] == (i * n + j) as i32 { + if grid[i][j] == '1' && p[i * n + j] == ((i * n + j) as i32) { ans += 1; } } diff --git a/solution/0200-0299/0200.Number of Islands/Solution.rs b/solution/0200-0299/0200.Number of Islands/Solution.rs index 7f00cff1a8e4e..358ee6a149f86 100644 --- a/solution/0200-0299/0200.Number of Islands/Solution.rs +++ b/solution/0200-0299/0200.Number of Islands/Solution.rs @@ -5,13 +5,14 @@ impl Solution { fn dfs(grid: &mut Vec>, i: usize, j: usize) { grid[i][j] = '0'; for k in 0..4 { - let x = i as i32 + DIRS[k]; - let y = j as i32 + DIRS[k + 1]; - if x >= 0 - && (x as usize) < grid.len() - && y >= 0 - && (y as usize) < grid[0].len() - && grid[x as usize][y as usize] == '1' + let x = (i as i32) + DIRS[k]; + let y = (j as i32) + DIRS[k + 1]; + if + x >= 0 && + (x as usize) < grid.len() && + y >= 0 && + (y as usize) < grid[0].len() && + grid[x as usize][y as usize] == '1' { dfs(grid, x as usize, y as usize); } diff --git a/solution/0200-0299/0206.Reverse Linked List/README.md b/solution/0200-0299/0206.Reverse Linked List/README.md index f50638a8f6c7c..2ccd5d6103901 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README.md +++ b/solution/0200-0299/0206.Reverse Linked List/README.md @@ -426,14 +426,14 @@ impl Solution { // } // } impl Solution { - fn rev (pre: Option>, cur: Option>) -> Option> { + fn rev(pre: Option>, cur: Option>) -> Option> { match cur { None => pre, Some(mut node) => { let next = node.next; node.next = pre; Self::rev(Some(node), next) - }, + } } } diff --git a/solution/0200-0299/0206.Reverse Linked List/README_EN.md b/solution/0200-0299/0206.Reverse Linked List/README_EN.md index 478470e2a6195..8a920cf47b153 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README_EN.md +++ b/solution/0200-0299/0206.Reverse Linked List/README_EN.md @@ -405,14 +405,14 @@ Recursion: // } // } impl Solution { - fn rev (pre: Option>, cur: Option>) -> Option> { + fn rev(pre: Option>, cur: Option>) -> Option> { match cur { None => pre, Some(mut node) => { let next = node.next; node.next = pre; Self::rev(Some(node), next) - }, + } } } diff --git a/solution/0200-0299/0207.Course Schedule/Solution.rs b/solution/0200-0299/0207.Course Schedule/Solution.rs index c2dfb6ee7158c..a23d4806b04e2 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.rs +++ b/solution/0200-0299/0207.Course Schedule/Solution.rs @@ -44,4 +44,4 @@ impl Solution { count == num_course } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md index c6d3024f3d784..7b58728078fb8 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md @@ -301,7 +301,7 @@ public: ### **Rust** ```rust -use std::{rc::Rc, cell::RefCell, collections::HashMap}; +use std::{ rc::Rc, cell::RefCell, collections::HashMap }; struct TrieNode { pub val: Option, diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md index 7b6e1980da438..f26f37d158fb0 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md @@ -264,7 +264,7 @@ public: ### **Rust** ```rust -use std::{rc::Rc, cell::RefCell, collections::HashMap}; +use std::{ rc::Rc, cell::RefCell, collections::HashMap }; struct TrieNode { pub val: Option, diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs index 10e8ef6038c59..90d2217a47ef1 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc, cell::RefCell, collections::HashMap}; +use std::{ rc::Rc, cell::RefCell, collections::HashMap }; struct TrieNode { pub val: Option, @@ -88,4 +88,4 @@ impl Trie { } true } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0210.Course Schedule II/Solution.rs b/solution/0200-0299/0210.Course Schedule II/Solution.rs index 0136b4f867a21..6fad541b730a8 100644 --- a/solution/0200-0299/0210.Course Schedule II/Solution.rs +++ b/solution/0200-0299/0210.Course Schedule II/Solution.rs @@ -1,38 +1,38 @@ -impl Solution { - pub fn find_order(num_courses: i32, prerequisites: Vec>) -> Vec { - let n = num_courses as usize; - let mut adjacency = vec![vec![]; n]; - let mut entry = vec![0; n]; - // init - for iter in prerequisites.iter() { - let (a, b) = (iter[0], iter[1]); - adjacency[b as usize].push(a); - entry[a as usize] += 1; - } - // construct deque & reslut - let mut deque = std::collections::VecDeque::new(); - for index in 0..n { - if entry[index] == 0 { - deque.push_back(index); - } - } - let mut result = vec![]; - // bfs - while !deque.is_empty() { - let head = deque.pop_front().unwrap(); - result.push(head as i32); - // update degree of entry - for &out_entry in adjacency[head].iter() { - entry[out_entry as usize] -= 1; - if entry[out_entry as usize] == 0 { - deque.push_back(out_entry as usize); - } - } - } - if result.len() == n { - result - } else { - vec![] - } - } -} +impl Solution { + pub fn find_order(num_courses: i32, prerequisites: Vec>) -> Vec { + let n = num_courses as usize; + let mut adjacency = vec![vec![]; n]; + let mut entry = vec![0; n]; + // init + for iter in prerequisites.iter() { + let (a, b) = (iter[0], iter[1]); + adjacency[b as usize].push(a); + entry[a as usize] += 1; + } + // construct deque & reslut + let mut deque = std::collections::VecDeque::new(); + for index in 0..n { + if entry[index] == 0 { + deque.push_back(index); + } + } + let mut result = vec![]; + // bfs + while !deque.is_empty() { + let head = deque.pop_front().unwrap(); + result.push(head as i32); + // update degree of entry + for &out_entry in adjacency[head].iter() { + entry[out_entry as usize] -= 1; + if entry[out_entry as usize] == 0 { + deque.push_back(out_entry as usize); + } + } + } + if result.len() == n { + result + } else { + vec![] + } + } +} diff --git a/solution/0200-0299/0213.House Robber II/Solution.rs b/solution/0200-0299/0213.House Robber II/Solution.rs index 454f81de045b3..ae3dd2c86ab89 100644 --- a/solution/0200-0299/0213.House Robber II/Solution.rs +++ b/solution/0200-0299/0213.House Robber II/Solution.rs @@ -1,16 +1,16 @@ -impl Solution { - pub fn rob(nums: Vec) -> i32 { - let n = nums.len(); - if n == 1 { - return nums[0]; - } - let rob_range = |l, r| { - let mut f = [0, 0]; - for i in l..r { - f = [f[0].max(f[1]), f[0] + nums[i]]; - } - f[0].max(f[1]) - }; - rob_range(0, n - 1).max(rob_range(1, n)) - } -} +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let n = nums.len(); + if n == 1 { + return nums[0]; + } + let rob_range = |l, r| { + let mut f = [0, 0]; + for i in l..r { + f = [f[0].max(f[1]), f[0] + nums[i]]; + } + f[0].max(f[1]) + }; + rob_range(0, n - 1).max(rob_range(1, n)) + } +} diff --git a/solution/0200-0299/0214.Shortest Palindrome/README.md b/solution/0200-0299/0214.Shortest Palindrome/README.md index 2a0a130d0eaca..389a3a7ffd8e3 100644 --- a/solution/0200-0299/0214.Shortest Palindrome/README.md +++ b/solution/0200-0299/0214.Shortest Palindrome/README.md @@ -169,7 +169,7 @@ impl Solution { let base = 131; let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1); for (i, c) in s.chars().enumerate() { - let t = c as u64 - '0' as u64 + 1; + let t = (c as u64) - ('0' as u64) + 1; prefix = prefix * base + t; suffix = suffix + t * mul; mul *= base; diff --git a/solution/0200-0299/0214.Shortest Palindrome/README_EN.md b/solution/0200-0299/0214.Shortest Palindrome/README_EN.md index c16518e231b8f..f43416eb460ff 100644 --- a/solution/0200-0299/0214.Shortest Palindrome/README_EN.md +++ b/solution/0200-0299/0214.Shortest Palindrome/README_EN.md @@ -140,7 +140,7 @@ impl Solution { let base = 131; let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1); for (i, c) in s.chars().enumerate() { - let t = c as u64 - '0' as u64 + 1; + let t = (c as u64) - ('0' as u64) + 1; prefix = prefix * base + t; suffix = suffix + t * mul; mul *= base; diff --git a/solution/0200-0299/0214.Shortest Palindrome/Solution.rs b/solution/0200-0299/0214.Shortest Palindrome/Solution.rs index b576b5f8547d9..8b0b595b16a13 100644 --- a/solution/0200-0299/0214.Shortest Palindrome/Solution.rs +++ b/solution/0200-0299/0214.Shortest Palindrome/Solution.rs @@ -1,21 +1,21 @@ -impl Solution { - pub fn shortest_palindrome(s: String) -> String { - let base = 131; - let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1); - for (i, c) in s.chars().enumerate() { - let t = c as u64 - '0' as u64 + 1; - prefix = prefix * base + t; - suffix = suffix + t * mul; - mul *= base; - if prefix == suffix { - idx = i + 1; - } - } - if idx == s.len() { - s - } else { - let x: String = (&s[idx..]).chars().rev().collect(); - String::from(x + &s) - } - } -} +impl Solution { + pub fn shortest_palindrome(s: String) -> String { + let base = 131; + let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1); + for (i, c) in s.chars().enumerate() { + let t = (c as u64) - ('0' as u64) + 1; + prefix = prefix * base + t; + suffix = suffix + t * mul; + mul *= base; + if prefix == suffix { + idx = i + 1; + } + } + if idx == s.len() { + s + } else { + let x: String = (&s[idx..]).chars().rev().collect(); + String::from(x + &s) + } + } +} diff --git a/solution/0200-0299/0216.Combination Sum III/README.md b/solution/0200-0299/0216.Combination Sum III/README.md index 6138870f62fdd..62bf67e391af9 100644 --- a/solution/0200-0299/0216.Combination Sum III/README.md +++ b/solution/0200-0299/0216.Combination Sum III/README.md @@ -340,12 +340,20 @@ impl Solution { } #[allow(dead_code)] - fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec, candidates: &Vec, ans: &mut Vec>) { - if cur_sum > target || cur_vec.len() > length as usize { + fn dfs( + target: i32, + length: i32, + cur_index: usize, + cur_sum: i32, + cur_vec: &mut Vec, + candidates: &Vec, + ans: &mut Vec> + ) { + if cur_sum > target || cur_vec.len() > (length as usize) { // No answer for this return; } - if cur_sum == target && cur_vec.len() == length as usize { + if cur_sum == target && cur_vec.len() == (length as usize) { // We get an answer ans.push(cur_vec.clone()); return; diff --git a/solution/0200-0299/0216.Combination Sum III/README_EN.md b/solution/0200-0299/0216.Combination Sum III/README_EN.md index c1339c82089a6..ceee2b355b273 100644 --- a/solution/0200-0299/0216.Combination Sum III/README_EN.md +++ b/solution/0200-0299/0216.Combination Sum III/README_EN.md @@ -297,12 +297,20 @@ impl Solution { } #[allow(dead_code)] - fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec, candidates: &Vec, ans: &mut Vec>) { - if cur_sum > target || cur_vec.len() > length as usize { + fn dfs( + target: i32, + length: i32, + cur_index: usize, + cur_sum: i32, + cur_vec: &mut Vec, + candidates: &Vec, + ans: &mut Vec> + ) { + if cur_sum > target || cur_vec.len() > (length as usize) { // No answer for this return; } - if cur_sum == target && cur_vec.len() == length as usize { + if cur_sum == target && cur_vec.len() == (length as usize) { // We get an answer ans.push(cur_vec.clone()); return; diff --git a/solution/0200-0299/0216.Combination Sum III/Solution.rs b/solution/0200-0299/0216.Combination Sum III/Solution.rs index faf45912fa0cd..78a60f424d22f 100644 --- a/solution/0200-0299/0216.Combination Sum III/Solution.rs +++ b/solution/0200-0299/0216.Combination Sum III/Solution.rs @@ -9,12 +9,20 @@ impl Solution { } #[allow(dead_code)] - fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec, candidates: &Vec, ans: &mut Vec>) { - if cur_sum > target || cur_vec.len() > length as usize { + fn dfs( + target: i32, + length: i32, + cur_index: usize, + cur_sum: i32, + cur_vec: &mut Vec, + candidates: &Vec, + ans: &mut Vec> + ) { + if cur_sum > target || cur_vec.len() > (length as usize) { // No answer for this return; } - if cur_sum == target && cur_vec.len() == length as usize { + if cur_sum == target && cur_vec.len() == (length as usize) { // We get an answer ans.push(cur_vec.clone()); return; @@ -25,4 +33,4 @@ impl Solution { cur_vec.pop().unwrap(); } } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0217.Contains Duplicate/README.md b/solution/0200-0299/0217.Contains Duplicate/README.md index e3b096d7b9dec..c24e10a64997b 100644 --- a/solution/0200-0299/0217.Contains Duplicate/README.md +++ b/solution/0200-0299/0217.Contains Duplicate/README.md @@ -203,7 +203,7 @@ impl Solution { let n = nums.len(); for i in 1..n { if nums[i - 1] == nums[i] { - return true + return true; } } false diff --git a/solution/0200-0299/0217.Contains Duplicate/README_EN.md b/solution/0200-0299/0217.Contains Duplicate/README_EN.md index 3c30908ee4202..05aeeaaf91dee 100644 --- a/solution/0200-0299/0217.Contains Duplicate/README_EN.md +++ b/solution/0200-0299/0217.Contains Duplicate/README_EN.md @@ -169,7 +169,7 @@ impl Solution { let n = nums.len(); for i in 1..n { if nums[i - 1] == nums[i] { - return true + return true; } } false diff --git a/solution/0200-0299/0218.The Skyline Problem/Solution.rs b/solution/0200-0299/0218.The Skyline Problem/Solution.rs index 4a3b8ba014759..b66dc4df0703c 100644 --- a/solution/0200-0299/0218.The Skyline Problem/Solution.rs +++ b/solution/0200-0299/0218.The Skyline Problem/Solution.rs @@ -1,32 +1,32 @@ -impl Solution { - pub fn get_skyline(buildings: Vec>) -> Vec> { - let mut skys: Vec> = vec![]; - let mut lines = vec![]; - for building in buildings.iter() { - lines.push(building[0]); - lines.push(building[1]); - } - lines.sort_unstable(); - let mut pq = std::collections::BinaryHeap::new(); - let (mut city, n) = (0, buildings.len()); - - for line in lines { - while city < n && buildings[city][0] <= line && buildings[city][1] > line { - pq.push((buildings[city][2], buildings[city][1])); - city += 1; - } - while !pq.is_empty() && pq.peek().unwrap().1 <= line { - pq.pop(); - } - let mut high = 0; - if !pq.is_empty() { - high = pq.peek().unwrap().0; - } - if !skys.is_empty() && skys.last().unwrap()[1] == high { - continue; - } - skys.push(vec![line, high]); - } - skys - } -} +impl Solution { + pub fn get_skyline(buildings: Vec>) -> Vec> { + let mut skys: Vec> = vec![]; + let mut lines = vec![]; + for building in buildings.iter() { + lines.push(building[0]); + lines.push(building[1]); + } + lines.sort_unstable(); + let mut pq = std::collections::BinaryHeap::new(); + let (mut city, n) = (0, buildings.len()); + + for line in lines { + while city < n && buildings[city][0] <= line && buildings[city][1] > line { + pq.push((buildings[city][2], buildings[city][1])); + city += 1; + } + while !pq.is_empty() && pq.peek().unwrap().1 <= line { + pq.pop(); + } + let mut high = 0; + if !pq.is_empty() { + high = pq.peek().unwrap().0; + } + if !skys.is_empty() && skys.last().unwrap()[1] == high { + continue; + } + skys.push(vec![line, high]); + } + skys + } +} diff --git a/solution/0200-0299/0222.Count Complete Tree Nodes/README.md b/solution/0200-0299/0222.Count Complete Tree Nodes/README.md index 39e060a567013..ebc6c9921e1e4 100644 --- a/solution/0200-0299/0222.Count Complete Tree Nodes/README.md +++ b/solution/0200-0299/0222.Count Complete Tree Nodes/README.md @@ -435,11 +435,7 @@ impl Solution { } fn depth(root: &Option>>) -> i32 { - if let Some(node) = root { - Self::depth(&node.borrow().left) + 1 - } else { - 0 - } + if let Some(node) = root { Self::depth(&node.borrow().left) + 1 } else { 0 } } } ``` diff --git a/solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md b/solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md index a976ce443bd89..7f510b5038c56 100644 --- a/solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md +++ b/solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md @@ -402,11 +402,7 @@ impl Solution { } fn depth(root: &Option>>) -> i32 { - if let Some(node) = root { - Self::depth(&node.borrow().left) + 1 - } else { - 0 - } + if let Some(node) = root { Self::depth(&node.borrow().left) + 1 } else { 0 } } } ``` diff --git a/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.rs b/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.rs index 9cc7d68b0984b..c6c1f0d5af102 100644 --- a/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.rs +++ b/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.rs @@ -18,10 +18,6 @@ impl Solution { } fn depth(root: &Option>>) -> i32 { - if let Some(node) = root { - Self::depth(&node.borrow().left) + 1 - } else { - 0 - } + if let Some(node) = root { Self::depth(&node.borrow().left) + 1 } else { 0 } } } diff --git a/solution/0200-0299/0225.Implement Stack using Queues/Solution.rs b/solution/0200-0299/0225.Implement Stack using Queues/Solution.rs index 16f8aaa596e9b..7fe42256bd16c 100644 --- a/solution/0200-0299/0225.Implement Stack using Queues/Solution.rs +++ b/solution/0200-0299/0225.Implement Stack using Queues/Solution.rs @@ -46,4 +46,4 @@ impl MyStack { fn empty(&self) -> bool { self.q_1.is_empty() } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0226.Invert Binary Tree/Solution.rs b/solution/0200-0299/0226.Invert Binary Tree/Solution.rs index cdeb639fd70e4..4122b5f8a953e 100644 --- a/solution/0200-0299/0226.Invert Binary Tree/Solution.rs +++ b/solution/0200-0299/0226.Invert Binary Tree/Solution.rs @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -35,4 +35,4 @@ impl Solution { // Return the root root } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0228.Summary Ranges/Solution.rs b/solution/0200-0299/0228.Summary Ranges/Solution.rs index 15071f9a0b2b1..171acb2e6c4ab 100644 --- a/solution/0200-0299/0228.Summary Ranges/Solution.rs +++ b/solution/0200-0299/0228.Summary Ranges/Solution.rs @@ -34,4 +34,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README.md b/solution/0200-0299/0232.Implement Queue using Stacks/README.md index e7257befd6d3e..3d093ebfa8124 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README.md @@ -330,13 +330,11 @@ struct MyQueue { out_stack: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MyQueue { - fn new() -> Self { Self { in_stack: vec![], @@ -366,7 +364,7 @@ impl MyQueue { self.in_stack.is_empty() && self.out_stack.is_empty() } - fn fill_out(&mut self){ + fn fill_out(&mut self) { let MyQueue { in_stack, out_stack } = self; if out_stack.is_empty() { while !in_stack.is_empty() { @@ -374,9 +372,7 @@ impl MyQueue { } } } -} - -/** +}/** * Your MyQueue object will be instantiated and called as such: * let obj = MyQueue::new(); * obj.push(x); diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md index 9aedc6e045509..32dc6e51866dc 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md @@ -300,13 +300,11 @@ struct MyQueue { out_stack: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MyQueue { - fn new() -> Self { Self { in_stack: vec![], @@ -336,7 +334,7 @@ impl MyQueue { self.in_stack.is_empty() && self.out_stack.is_empty() } - fn fill_out(&mut self){ + fn fill_out(&mut self) { let MyQueue { in_stack, out_stack } = self; if out_stack.is_empty() { while !in_stack.is_empty() { @@ -344,9 +342,7 @@ impl MyQueue { } } } -} - -/** +}/** * Your MyQueue object will be instantiated and called as such: * let obj = MyQueue::new(); * obj.push(x); diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.rs b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.rs index 896ddbfc62b04..e74f5a6172b15 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.rs +++ b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.rs @@ -3,13 +3,11 @@ struct MyQueue { out_stack: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MyQueue { - fn new() -> Self { Self { in_stack: vec![], @@ -39,7 +37,7 @@ impl MyQueue { self.in_stack.is_empty() && self.out_stack.is_empty() } - fn fill_out(&mut self){ + fn fill_out(&mut self) { let MyQueue { in_stack, out_stack } = self; if out_stack.is_empty() { while !in_stack.is_empty() { @@ -47,13 +45,11 @@ impl MyQueue { } } } -} - -/** +}/** * Your MyQueue object will be instantiated and called as such: * let obj = MyQueue::new(); * obj.push(x); * let ret_2: i32 = obj.pop(); * let ret_3: i32 = obj.peek(); * let ret_4: bool = obj.empty(); - */ \ No newline at end of file + */ diff --git a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md index 9fcebe16b53fb..d266807450881 100644 --- a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md +++ b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md @@ -262,7 +262,7 @@ impl Solution { fn find( root: &Option>>, p: &Option>>, - q: &Option>>, + q: &Option>> ) -> Option>> { if root.is_none() || root == p || root == q { return root.clone(); @@ -281,7 +281,7 @@ impl Solution { pub fn lowest_common_ancestor( root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { Self::find(&root, &p, &q) } diff --git a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md index 4b73bedd19e53..4155c2a1f8756 100644 --- a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md +++ b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md @@ -237,7 +237,7 @@ impl Solution { fn find( root: &Option>>, p: &Option>>, - q: &Option>>, + q: &Option>> ) -> Option>> { if root.is_none() || root == p || root == q { return root.clone(); @@ -256,7 +256,7 @@ impl Solution { pub fn lowest_common_ancestor( root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { Self::find(&root, &p, &q) } diff --git a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.rs b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.rs index cbd9002826414..ebfd8dea59dbd 100644 --- a/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.rs +++ b/solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.rs @@ -22,7 +22,7 @@ impl Solution { fn find( root: &Option>>, p: &Option>>, - q: &Option>>, + q: &Option>> ) -> Option>> { if root.is_none() || root == p || root == q { return root.clone(); @@ -41,7 +41,7 @@ impl Solution { pub fn lowest_common_ancestor( root: Option>>, p: Option>>, - q: Option>>, + q: Option>> ) -> Option>> { Self::find(&root, &p, &q) } diff --git a/solution/0200-0299/0238.Product of Array Except Self/Solution.rs b/solution/0200-0299/0238.Product of Array Except Self/Solution.rs index ba2990e16a58a..58d037245cf4f 100644 --- a/solution/0200-0299/0238.Product of Array Except Self/Solution.rs +++ b/solution/0200-0299/0238.Product of Array Except Self/Solution.rs @@ -1,15 +1,15 @@ -impl Solution { - pub fn product_except_self(nums: Vec) -> Vec { - let n = nums.len(); - let mut ans = vec![1; n]; - for i in 1..n { - ans[i] = ans[i - 1] * nums[i - 1]; - } - let mut r = 1; - for i in (0..n).rev() { - ans[i] *= r; - r *= nums[i]; - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn product_except_self(nums: Vec) -> Vec { + let n = nums.len(); + let mut ans = vec![1; n]; + for i in 1..n { + ans[i] = ans[i - 1] * nums[i - 1]; + } + let mut r = 1; + for i in (0..n).rev() { + ans[i] *= r; + r *= nums[i]; + } + ans + } +} diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README.md b/solution/0200-0299/0239.Sliding Window Maximum/README.md index ce336d828dabf..7044302d1b78c 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README.md @@ -220,7 +220,7 @@ impl Solution { for i in 0..nums.len() { // Check the first element of queue, if it's out of bound - if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + if !q.is_empty() && (i as i32) - k + 1 > (*q.front().unwrap() as i32) { // Pop it out q.pop_front(); } @@ -232,7 +232,7 @@ impl Solution { // Push the current index in queue q.push_back(i); // Check if the condition is satisfied - if i >= (k - 1) as usize { + if i >= ((k - 1) as usize) { ans_vec.push(nums[*q.front().unwrap()]); } } diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md index 6db752cc618be..279315714e278 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md @@ -185,7 +185,7 @@ impl Solution { for i in 0..nums.len() { // Check the first element of queue, if it's out of bound - if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + if !q.is_empty() && (i as i32) - k + 1 > (*q.front().unwrap() as i32) { // Pop it out q.pop_front(); } @@ -197,7 +197,7 @@ impl Solution { // Push the current index in queue q.push_back(i); // Check if the condition is satisfied - if i >= (k - 1) as usize { + if i >= ((k - 1) as usize) { ans_vec.push(nums[*q.front().unwrap()]); } } diff --git a/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs b/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs index d5043304e179d..dd27b35cc9810 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs +++ b/solution/0200-0299/0239.Sliding Window Maximum/Solution.rs @@ -9,7 +9,7 @@ impl Solution { for i in 0..nums.len() { // Check the first element of queue, if it's out of bound - if !q.is_empty() && (i as i32 - k + 1) > *q.front().unwrap() as i32 { + if !q.is_empty() && (i as i32) - k + 1 > (*q.front().unwrap() as i32) { // Pop it out q.pop_front(); } @@ -21,11 +21,11 @@ impl Solution { // Push the current index in queue q.push_back(i); // Check if the condition is satisfied - if i >= (k - 1) as usize { + if i >= ((k - 1) as usize) { ans_vec.push(nums[*q.front().unwrap()]); } } ans_vec } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README.md b/solution/0200-0299/0240.Search a 2D Matrix II/README.md index 0000a9dec0ae3..34e47f18fb678 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README.md @@ -298,9 +298,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match target.cmp(&matrix[i][j - 1]) { - Ordering::Less => j -= 1, - Ordering::Greater => i += 1, - Ordering::Equal => return true, + Ordering::Less => { + j -= 1; + } + Ordering::Greater => { + i += 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md index 56de5181c07ea..261c99e538125 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md @@ -268,9 +268,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match target.cmp(&matrix[i][j - 1]) { - Ordering::Less => j -= 1, - Ordering::Greater => i += 1, - Ordering::Equal => return true, + Ordering::Less => { + j -= 1; + } + Ordering::Greater => { + i += 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/Solution.rs b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.rs index 5430fc549dce2..ceb61d1abcfc4 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/Solution.rs +++ b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.rs @@ -8,9 +8,15 @@ impl Solution { let mut j = n; while i < m && j > 0 { match target.cmp(&matrix[i][j - 1]) { - Ordering::Less => j -= 1, - Ordering::Greater => i += 1, - Ordering::Equal => return true, + Ordering::Less => { + j -= 1; + } + Ordering::Greater => { + i += 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0200-0299/0252.Meeting Rooms/README.md b/solution/0200-0299/0252.Meeting Rooms/README.md index e5c3671d0a63c..475fbc2af3298 100644 --- a/solution/0200-0299/0252.Meeting Rooms/README.md +++ b/solution/0200-0299/0252.Meeting Rooms/README.md @@ -105,15 +105,13 @@ impl Solution { #[allow(dead_code)] pub fn can_attend_meetings(intervals: Vec>) -> bool { if intervals.len() == 1 { - return true + return true; } let mut intervals = intervals; // Sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); let mut end = -1; diff --git a/solution/0200-0299/0252.Meeting Rooms/README_EN.md b/solution/0200-0299/0252.Meeting Rooms/README_EN.md index c1ec83204cc0e..e633f568307ea 100644 --- a/solution/0200-0299/0252.Meeting Rooms/README_EN.md +++ b/solution/0200-0299/0252.Meeting Rooms/README_EN.md @@ -80,15 +80,13 @@ impl Solution { #[allow(dead_code)] pub fn can_attend_meetings(intervals: Vec>) -> bool { if intervals.len() == 1 { - return true + return true; } let mut intervals = intervals; // Sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); let mut end = -1; diff --git a/solution/0200-0299/0252.Meeting Rooms/Solution.rs b/solution/0200-0299/0252.Meeting Rooms/Solution.rs index 218b779a78fbd..8c313dc9e9440 100644 --- a/solution/0200-0299/0252.Meeting Rooms/Solution.rs +++ b/solution/0200-0299/0252.Meeting Rooms/Solution.rs @@ -2,15 +2,13 @@ impl Solution { #[allow(dead_code)] pub fn can_attend_meetings(intervals: Vec>) -> bool { if intervals.len() == 1 { - return true + return true; } let mut intervals = intervals; // Sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); let mut end = -1; @@ -29,4 +27,4 @@ impl Solution { true } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0253.Meeting Rooms II/README.md b/solution/0200-0299/0253.Meeting Rooms II/README.md index 09a5e02b07875..5ceb581eb894c 100644 --- a/solution/0200-0299/0253.Meeting Rooms II/README.md +++ b/solution/0200-0299/0253.Meeting Rooms II/README.md @@ -101,7 +101,7 @@ public: ### **Rust** ```rust -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; impl Solution { #[allow(dead_code)] @@ -112,9 +112,7 @@ impl Solution { let n = intervals.len(); // Let's first sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Push the first end time to the heap pq.push(Reverse(intervals[0][1])); diff --git a/solution/0200-0299/0253.Meeting Rooms II/README_EN.md b/solution/0200-0299/0253.Meeting Rooms II/README_EN.md index ae68ff368d90c..3d0fa3d0de462 100644 --- a/solution/0200-0299/0253.Meeting Rooms II/README_EN.md +++ b/solution/0200-0299/0253.Meeting Rooms II/README_EN.md @@ -82,7 +82,7 @@ public: ### **Rust** ```rust -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; impl Solution { #[allow(dead_code)] @@ -93,9 +93,7 @@ impl Solution { let n = intervals.len(); // Let's first sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Push the first end time to the heap pq.push(Reverse(intervals[0][1])); diff --git a/solution/0200-0299/0253.Meeting Rooms II/Solution.rs b/solution/0200-0299/0253.Meeting Rooms II/Solution.rs index 52df4461bfd82..a79e5216a86b1 100644 --- a/solution/0200-0299/0253.Meeting Rooms II/Solution.rs +++ b/solution/0200-0299/0253.Meeting Rooms II/Solution.rs @@ -1,4 +1,4 @@ -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; impl Solution { #[allow(dead_code)] @@ -9,9 +9,7 @@ impl Solution { let n = intervals.len(); // Let's first sort the intervals vector - intervals.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Push the first end time to the heap pq.push(Reverse(intervals[0][1])); @@ -31,7 +29,7 @@ impl Solution { } } } - + pq.len() as i32 } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0258.Add Digits/README.md b/solution/0200-0299/0258.Add Digits/README.md index 664158d557925..b02e0e9dda330 100644 --- a/solution/0200-0299/0258.Add Digits/README.md +++ b/solution/0200-0299/0258.Add Digits/README.md @@ -113,10 +113,11 @@ impl Solution { return num; } Self::add_digits( - num.to_string() + num + .to_string() .chars() .map(|c| c.to_string().parse::().unwrap()) - .sum::(), + .sum::() ) } } @@ -125,7 +126,7 @@ impl Solution { ```rust impl Solution { pub fn add_digits(mut num: i32) -> i32 { - (num - 1) % 9 + 1 + ((num - 1) % 9) + 1 } } ``` diff --git a/solution/0200-0299/0258.Add Digits/README_EN.md b/solution/0200-0299/0258.Add Digits/README_EN.md index 2c124fee11c0c..6b27d9f42911f 100644 --- a/solution/0200-0299/0258.Add Digits/README_EN.md +++ b/solution/0200-0299/0258.Add Digits/README_EN.md @@ -88,10 +88,11 @@ impl Solution { return num; } Self::add_digits( - num.to_string() + num + .to_string() .chars() .map(|c| c.to_string().parse::().unwrap()) - .sum::(), + .sum::() ) } } @@ -100,7 +101,7 @@ impl Solution { ```rust impl Solution { pub fn add_digits(mut num: i32) -> i32 { - (num - 1) % 9 + 1 + ((num - 1) % 9) + 1 } } ``` diff --git a/solution/0200-0299/0258.Add Digits/Solution.rs b/solution/0200-0299/0258.Add Digits/Solution.rs index 0af62e864f665..dcd23f532bf85 100644 --- a/solution/0200-0299/0258.Add Digits/Solution.rs +++ b/solution/0200-0299/0258.Add Digits/Solution.rs @@ -1,5 +1,5 @@ impl Solution { pub fn add_digits(mut num: i32) -> i32 { - (num - 1) % 9 + 1 + ((num - 1) % 9) + 1 } } diff --git a/solution/0200-0299/0260.Single Number III/README.md b/solution/0200-0299/0260.Single Number III/README.md index eafd4043bc2f5..618feae729908 100644 --- a/solution/0200-0299/0260.Single Number III/README.md +++ b/solution/0200-0299/0260.Single Number III/README.md @@ -158,7 +158,7 @@ impl Solution { let lb = xs & -xs; let mut a = 0; for x in &nums { - if x & lb != 0 { + if (x & lb) != 0 { a ^= x; } } diff --git a/solution/0200-0299/0260.Single Number III/README_EN.md b/solution/0200-0299/0260.Single Number III/README_EN.md index 93d5902d58ed4..796da9ed6073c 100644 --- a/solution/0200-0299/0260.Single Number III/README_EN.md +++ b/solution/0200-0299/0260.Single Number III/README_EN.md @@ -148,7 +148,7 @@ impl Solution { let lb = xs & -xs; let mut a = 0; for x in &nums { - if x & lb != 0 { + if (x & lb) != 0 { a ^= x; } } diff --git a/solution/0200-0299/0260.Single Number III/Solution.rs b/solution/0200-0299/0260.Single Number III/Solution.rs index f644c31518307..9999b71813f02 100644 --- a/solution/0200-0299/0260.Single Number III/Solution.rs +++ b/solution/0200-0299/0260.Single Number III/Solution.rs @@ -4,11 +4,11 @@ impl Solution { let lb = xs & -xs; let mut a = 0; for x in &nums { - if x & lb != 0 { + if (x & lb) != 0 { a ^= x; } } let b = xs ^ a; vec![a, b] } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0268.Missing Number/README.md b/solution/0200-0299/0268.Missing Number/README.md index 23ca26d64479b..727d71ee35915 100644 --- a/solution/0200-0299/0268.Missing Number/README.md +++ b/solution/0200-0299/0268.Missing Number/README.md @@ -184,7 +184,7 @@ impl Solution { let n = nums.len() as i32; let mut ans = n; for (i, v) in nums.iter().enumerate() { - ans ^= i as i32 ^ v; + ans ^= (i as i32) ^ v; } ans } @@ -197,7 +197,7 @@ impl Solution { let n = nums.len() as i32; let mut ans = n; for (i, &v) in nums.iter().enumerate() { - ans += i as i32 - v; + ans += (i as i32) - v; } ans } diff --git a/solution/0200-0299/0268.Missing Number/README_EN.md b/solution/0200-0299/0268.Missing Number/README_EN.md index 30196a67fd671..50c6461cafb7c 100644 --- a/solution/0200-0299/0268.Missing Number/README_EN.md +++ b/solution/0200-0299/0268.Missing Number/README_EN.md @@ -166,7 +166,7 @@ impl Solution { let n = nums.len() as i32; let mut ans = n; for (i, v) in nums.iter().enumerate() { - ans ^= i as i32 ^ v; + ans ^= (i as i32) ^ v; } ans } @@ -179,7 +179,7 @@ impl Solution { let n = nums.len() as i32; let mut ans = n; for (i, &v) in nums.iter().enumerate() { - ans += i as i32 - v; + ans += (i as i32) - v; } ans } diff --git a/solution/0200-0299/0268.Missing Number/Solution.rs b/solution/0200-0299/0268.Missing Number/Solution.rs index a3047794ed9db..b019b7648194b 100644 --- a/solution/0200-0299/0268.Missing Number/Solution.rs +++ b/solution/0200-0299/0268.Missing Number/Solution.rs @@ -1,10 +1,10 @@ impl Solution { pub fn missing_number(nums: Vec) -> i32 { let n = nums.len() as i32; - let mut ans = n; + let mut ans = n; for (i, v) in nums.iter().enumerate() { - ans ^= i as i32 ^ v; + ans ^= (i as i32) ^ v; } ans } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0274.H-Index/README.md b/solution/0200-0299/0274.H-Index/README.md index fd555ad518096..1d215942689c9 100644 --- a/solution/0200-0299/0274.H-Index/README.md +++ b/solution/0200-0299/0274.H-Index/README.md @@ -234,14 +234,12 @@ impl Solution { #[allow(dead_code)] pub fn h_index(citations: Vec) -> i32 { let mut citations = citations; - citations.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + citations.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); let n = citations.len(); for i in (1..=n).rev() { - if citations[i - 1] >= i as i32 { + if citations[i - 1] >= (i as i32) { return i as i32; } } diff --git a/solution/0200-0299/0274.H-Index/README_EN.md b/solution/0200-0299/0274.H-Index/README_EN.md index 3ae05803a1322..89844fc110084 100644 --- a/solution/0200-0299/0274.H-Index/README_EN.md +++ b/solution/0200-0299/0274.H-Index/README_EN.md @@ -225,14 +225,12 @@ impl Solution { #[allow(dead_code)] pub fn h_index(citations: Vec) -> i32 { let mut citations = citations; - citations.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + citations.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); let n = citations.len(); for i in (1..=n).rev() { - if citations[i - 1] >= i as i32 { + if citations[i - 1] >= (i as i32) { return i as i32; } } diff --git a/solution/0200-0299/0274.H-Index/Solution.rs b/solution/0200-0299/0274.H-Index/Solution.rs index f0f6d4e0d3db3..163d8639a09e9 100644 --- a/solution/0200-0299/0274.H-Index/Solution.rs +++ b/solution/0200-0299/0274.H-Index/Solution.rs @@ -2,18 +2,16 @@ impl Solution { #[allow(dead_code)] pub fn h_index(citations: Vec) -> i32 { let mut citations = citations; - citations.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + citations.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); let n = citations.len(); for i in (1..=n).rev() { - if citations[i - 1] >= i as i32 { + if citations[i - 1] >= (i as i32) { return i as i32; } } 0 } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0275.H-Index II/README.md b/solution/0200-0299/0275.H-Index II/README.md index 6b67eb67cf768..6dcc09335081f 100644 --- a/solution/0200-0299/0275.H-Index II/README.md +++ b/solution/0200-0299/0275.H-Index II/README.md @@ -141,7 +141,7 @@ impl Solution { let (mut left, mut right) = (0, n); while left < right { let mid = ((left + right + 1) >> 1) as usize; - if citations[n - mid] >= mid as i32 { + if citations[n - mid] >= (mid as i32) { left = mid; } else { right = mid - 1; diff --git a/solution/0200-0299/0275.H-Index II/README_EN.md b/solution/0200-0299/0275.H-Index II/README_EN.md index cfb0868c9b8fd..fee1adb4f8ccd 100644 --- a/solution/0200-0299/0275.H-Index II/README_EN.md +++ b/solution/0200-0299/0275.H-Index II/README_EN.md @@ -132,7 +132,7 @@ impl Solution { let (mut left, mut right) = (0, n); while left < right { let mid = ((left + right + 1) >> 1) as usize; - if citations[n - mid] >= mid as i32 { + if citations[n - mid] >= (mid as i32) { left = mid; } else { right = mid - 1; diff --git a/solution/0200-0299/0275.H-Index II/Solution.rs b/solution/0200-0299/0275.H-Index II/Solution.rs index caf02b391eb7f..14618a24160df 100644 --- a/solution/0200-0299/0275.H-Index II/Solution.rs +++ b/solution/0200-0299/0275.H-Index II/Solution.rs @@ -1,15 +1,15 @@ -impl Solution { - pub fn h_index(citations: Vec) -> i32 { - let n = citations.len(); - let (mut left, mut right) = (0, n); - while left < right { - let mid = ((left + right + 1) >> 1) as usize; - if citations[n - mid] >= mid as i32 { - left = mid; - } else { - right = mid - 1; - } - } - left as i32 - } -} +impl Solution { + pub fn h_index(citations: Vec) -> i32 { + let n = citations.len(); + let (mut left, mut right) = (0, n); + while left < right { + let mid = ((left + right + 1) >> 1) as usize; + if citations[n - mid] >= (mid as i32) { + left = mid; + } else { + right = mid - 1; + } + } + left as i32 + } +} diff --git a/solution/0200-0299/0279.Perfect Squares/Solution.rs b/solution/0200-0299/0279.Perfect Squares/Solution.rs index 9beb1ee3ab15b..d65ec36a8b043 100644 --- a/solution/0200-0299/0279.Perfect Squares/Solution.rs +++ b/solution/0200-0299/0279.Perfect Squares/Solution.rs @@ -1,16 +1,16 @@ -impl Solution { - pub fn num_squares(n: i32) -> i32 { - let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); - let mut dp = vec![vec![i32::MAX; col + 1]; row + 1]; - dp[0][0] = 0; - for i in 1..=row { - for j in 0..=col { - dp[i][j] = dp[i - 1][j]; - if j >= i * i { - dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1); - } - } - } - dp[row][col] - } -} \ No newline at end of file +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![vec![i32::MAX; col + 1]; row + 1]; + dp[0][0] = 0; + for i in 1..=row { + for j in 0..=col { + dp[i][j] = dp[i - 1][j]; + if j >= i * i { + dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1); + } + } + } + dp[row][col] + } +} diff --git a/solution/0200-0299/0281.Zigzag Iterator/README.md b/solution/0200-0299/0281.Zigzag Iterator/README.md index 1df4efb530813..5bd1539c77372 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README.md @@ -109,7 +109,7 @@ impl ZigzagIterator { let ret = self.v1.remove(0); self.flag = true; return ret; - } else { + } else { // v2 if self.v2.is_empty() && !self.v1.is_empty() { self.flag = false; diff --git a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md index 3bedf165d6ae4..c05ee2562041f 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/README_EN.md +++ b/solution/0200-0299/0281.Zigzag Iterator/README_EN.md @@ -131,7 +131,7 @@ impl ZigzagIterator { let ret = self.v1.remove(0); self.flag = true; return ret; - } else { + } else { // v2 if self.v2.is_empty() && !self.v1.is_empty() { self.flag = false; diff --git a/solution/0200-0299/0281.Zigzag Iterator/Solution.rs b/solution/0200-0299/0281.Zigzag Iterator/Solution.rs index 48c5c44c01146..35054efd3461b 100644 --- a/solution/0200-0299/0281.Zigzag Iterator/Solution.rs +++ b/solution/0200-0299/0281.Zigzag Iterator/Solution.rs @@ -30,7 +30,7 @@ impl ZigzagIterator { let ret = self.v1.remove(0); self.flag = true; return ret; - } else { + } else { // v2 if self.v2.is_empty() && !self.v1.is_empty() { self.flag = false; @@ -50,4 +50,4 @@ impl ZigzagIterator { fn has_next(&self) -> bool { !self.v1.is_empty() || !self.v2.is_empty() } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0287.Find the Duplicate Number/README.md b/solution/0200-0299/0287.Find the Duplicate Number/README.md index 94a85be0d38d4..de4c579533da3 100644 --- a/solution/0200-0299/0287.Find the Duplicate Number/README.md +++ b/solution/0200-0299/0287.Find the Duplicate Number/README.md @@ -139,11 +139,11 @@ impl Solution { let mid = (left + right) >> 1; let cnt = nums .iter() - .filter(|x| **x <= mid as i32) + .filter(|x| **x <= (mid as i32)) .count(); if cnt > mid { right = mid; - } else { + } else { left = mid + 1; } } diff --git a/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md b/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md index 654de635a701d..0e4b06c1e9628 100644 --- a/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md +++ b/solution/0200-0299/0287.Find the Duplicate Number/README_EN.md @@ -128,11 +128,11 @@ impl Solution { let mid = (left + right) >> 1; let cnt = nums .iter() - .filter(|x| **x <= mid as i32) + .filter(|x| **x <= (mid as i32)) .count(); if cnt > mid { right = mid; - } else { + } else { left = mid + 1; } } diff --git a/solution/0200-0299/0287.Find the Duplicate Number/Solution.rs b/solution/0200-0299/0287.Find the Duplicate Number/Solution.rs index cd59c27fa1df6..8e65044e108e5 100644 --- a/solution/0200-0299/0287.Find the Duplicate Number/Solution.rs +++ b/solution/0200-0299/0287.Find the Duplicate Number/Solution.rs @@ -8,15 +8,15 @@ impl Solution { let mid = (left + right) >> 1; let cnt = nums .iter() - .filter(|x| **x <= mid as i32) + .filter(|x| **x <= (mid as i32)) .count(); if cnt > mid { right = mid; - } else { + } else { left = mid + 1; } } left as i32 } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0289.Game of Life/README.md b/solution/0200-0299/0289.Game of Life/README.md index 59923a2601b12..fb122a80c264f 100644 --- a/solution/0200-0299/0289.Game of Life/README.md +++ b/solution/0200-0299/0289.Game of Life/README.md @@ -178,7 +178,16 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; +const DIR: [(i32, i32); 8] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), + (-1, -1), + (-1, 1), + (1, -1), + (1, 1), +]; impl Solution { #[allow(dead_code)] @@ -194,8 +203,8 @@ impl Solution { continue; } for (dx, dy) in DIR { - let x = i as i32 + dx; - let y = j as i32 + dy; + let x = (i as i32) + dx; + let y = (j as i32) + dy; if Self::check_bounds(x, y, n as i32, m as i32) { weight_vec[x as usize][y as usize] += 1; } diff --git a/solution/0200-0299/0289.Game of Life/README_EN.md b/solution/0200-0299/0289.Game of Life/README_EN.md index 79633c3f54539..c3d319b50d4bb 100644 --- a/solution/0200-0299/0289.Game of Life/README_EN.md +++ b/solution/0200-0299/0289.Game of Life/README_EN.md @@ -167,7 +167,16 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; +const DIR: [(i32, i32); 8] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), + (-1, -1), + (-1, 1), + (1, -1), + (1, 1), +]; impl Solution { #[allow(dead_code)] @@ -183,8 +192,8 @@ impl Solution { continue; } for (dx, dy) in DIR { - let x = i as i32 + dx; - let y = j as i32 + dy; + let x = (i as i32) + dx; + let y = (j as i32) + dy; if Self::check_bounds(x, y, n as i32, m as i32) { weight_vec[x as usize][y as usize] += 1; } diff --git a/solution/0200-0299/0289.Game of Life/Solution.rs b/solution/0200-0299/0289.Game of Life/Solution.rs index 990dad21b8f8e..8476d5f84fcea 100644 --- a/solution/0200-0299/0289.Game of Life/Solution.rs +++ b/solution/0200-0299/0289.Game of Life/Solution.rs @@ -1,4 +1,13 @@ -const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; +const DIR: [(i32, i32); 8] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), + (-1, -1), + (-1, 1), + (1, -1), + (1, 1), +]; impl Solution { #[allow(dead_code)] @@ -14,8 +23,8 @@ impl Solution { continue; } for (dx, dy) in DIR { - let x = i as i32 + dx; - let y = j as i32 + dy; + let x = (i as i32) + dx; + let y = (j as i32) + dy; if Self::check_bounds(x, y, n as i32, m as i32) { weight_vec[x as usize][y as usize] += 1; } @@ -43,4 +52,4 @@ impl Solution { fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { i >= 0 && i < n && j >= 0 && j < m } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0290.Word Pattern/README.md b/solution/0200-0299/0290.Word Pattern/README.md index 1665b6e40d119..534257f25cd51 100644 --- a/solution/0200-0299/0290.Word Pattern/README.md +++ b/solution/0200-0299/0290.Word Pattern/README.md @@ -240,7 +240,7 @@ impl Solution { map2.insert(s, i); } if map1.get(&c) != map2.get(&s) { - return false + return false; } } true diff --git a/solution/0200-0299/0290.Word Pattern/README_EN.md b/solution/0200-0299/0290.Word Pattern/README_EN.md index c2fe0226901de..982334612add2 100644 --- a/solution/0200-0299/0290.Word Pattern/README_EN.md +++ b/solution/0200-0299/0290.Word Pattern/README_EN.md @@ -233,7 +233,7 @@ impl Solution { map2.insert(s, i); } if map1.get(&c) != map2.get(&s) { - return false + return false; } } true diff --git a/solution/0200-0299/0290.Word Pattern/Solution.rs b/solution/0200-0299/0290.Word Pattern/Solution.rs index 1235f0e58120d..94cff5320d0d7 100644 --- a/solution/0200-0299/0290.Word Pattern/Solution.rs +++ b/solution/0200-0299/0290.Word Pattern/Solution.rs @@ -20,7 +20,7 @@ impl Solution { map2.insert(s, i); } if map1.get(&c) != map2.get(&s) { - return false + return false; } } true diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README.md b/solution/0200-0299/0295.Find Median from Data Stream/README.md index b819f5630340d..dae41eeeb8ea6 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README.md @@ -325,7 +325,7 @@ impl MedianFinder { let mut l = 0; let mut r = self.nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if self.nums[mid] < num { l = mid + 1; } else { @@ -342,9 +342,7 @@ impl MedianFinder { } f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 } -} - -/** +}/** * Your MedianFinder object will be instantiated and called as such: * let obj = MedianFinder::new(); * obj.add_num(num); diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md index 0988873ad085a..7ed2552de8009 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md @@ -310,7 +310,7 @@ impl MedianFinder { let mut l = 0; let mut r = self.nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if self.nums[mid] < num { l = mid + 1; } else { @@ -327,9 +327,7 @@ impl MedianFinder { } f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 } -} - -/** +}/** * Your MedianFinder object will be instantiated and called as such: * let obj = MedianFinder::new(); * obj.add_num(num); diff --git a/solution/0200-0299/0295.Find Median from Data Stream/Solution.rs b/solution/0200-0299/0295.Find Median from Data Stream/Solution.rs index c377c54db931c..be3a7ac65744b 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/Solution.rs +++ b/solution/0200-0299/0295.Find Median from Data Stream/Solution.rs @@ -16,7 +16,7 @@ impl MedianFinder { let mut l = 0; let mut r = self.nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if self.nums[mid] < num { l = mid + 1; } else { @@ -33,11 +33,9 @@ impl MedianFinder { } f64::from(self.nums[n >> 1] + self.nums[(n >> 1) - 1]) / 2.0 } -} - -/** +}/** * Your MedianFinder object will be instantiated and called as such: * let obj = MedianFinder::new(); * obj.add_num(num); * let ret_2: f64 = obj.find_median(); - */ \ No newline at end of file + */ diff --git a/solution/0200-0299/0296.Best Meeting Point/Solution.rs b/solution/0200-0299/0296.Best Meeting Point/Solution.rs index 38a1b4919afef..4b91ebdb6ec70 100644 --- a/solution/0200-0299/0296.Best Meeting Point/Solution.rs +++ b/solution/0200-0299/0296.Best Meeting Point/Solution.rs @@ -34,4 +34,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README.md b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README.md index 9614a024ba45e..bb24d7cc21854 100644 --- a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README.md +++ b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README.md @@ -415,9 +415,7 @@ function deserialize(data: string): TreeNode | null { // } use std::rc::Rc; use std::cell::RefCell; -struct Codec { - -} +struct Codec {} /** * `&self` means the method takes an immutable reference. @@ -435,12 +433,7 @@ impl Codec { let mut node = root.as_ref().unwrap().borrow_mut(); let left = node.left.take(); let right = node.right.take(); - format!( - "{},{},{}", - self.serialize(right), - self.serialize(left), - node.val - ) + format!("{},{},{}", self.serialize(right), self.serialize(left), node.val) } fn deserialize(&self, data: String) -> Option>> { @@ -455,15 +448,17 @@ impl Codec { if val == "#" { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: val.parse().unwrap(), - left: Self::renew(vals), - right: Self::renew(vals), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: val.parse().unwrap(), + left: Self::renew(vals), + right: Self::renew(vals), + }) + ) + ) } -} - -/** +}/** * Your Codec object will be instantiated and called as such: * let obj = Codec::new(); * let data: String = obj.serialize(strs); diff --git a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md index d0ca4fb8d3c01..943a4ec3772b4 100644 --- a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md +++ b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/README_EN.md @@ -391,9 +391,7 @@ function deserialize(data: string): TreeNode | null { // } use std::rc::Rc; use std::cell::RefCell; -struct Codec { - -} +struct Codec {} /** * `&self` means the method takes an immutable reference. @@ -411,12 +409,7 @@ impl Codec { let mut node = root.as_ref().unwrap().borrow_mut(); let left = node.left.take(); let right = node.right.take(); - format!( - "{},{},{}", - self.serialize(right), - self.serialize(left), - node.val - ) + format!("{},{},{}", self.serialize(right), self.serialize(left), node.val) } fn deserialize(&self, data: String) -> Option>> { @@ -431,15 +424,17 @@ impl Codec { if val == "#" { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: val.parse().unwrap(), - left: Self::renew(vals), - right: Self::renew(vals), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: val.parse().unwrap(), + left: Self::renew(vals), + right: Self::renew(vals), + }) + ) + ) } -} - -/** +}/** * Your Codec object will be instantiated and called as such: * let obj = Codec::new(); * let data: String = obj.serialize(strs); diff --git a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.rs b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.rs index 3b594e3b0a4c1..53614ceafe9cc 100644 --- a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.rs +++ b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.rs @@ -18,9 +18,7 @@ // } use std::rc::Rc; use std::cell::RefCell; -struct Codec { - -} +struct Codec {} /** * `&self` means the method takes an immutable reference. @@ -38,12 +36,7 @@ impl Codec { let mut node = root.as_ref().unwrap().borrow_mut(); let left = node.left.take(); let right = node.right.take(); - format!( - "{},{},{}", - self.serialize(right), - self.serialize(left), - node.val - ) + format!("{},{},{}", self.serialize(right), self.serialize(left), node.val) } fn deserialize(&self, data: String) -> Option>> { @@ -58,17 +51,19 @@ impl Codec { if val == "#" { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: val.parse().unwrap(), - left: Self::renew(vals), - right: Self::renew(vals), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: val.parse().unwrap(), + left: Self::renew(vals), + right: Self::renew(vals), + }) + ) + ) } -} - -/** +}/** * Your Codec object will be instantiated and called as such: * let obj = Codec::new(); * let data: String = obj.serialize(strs); * let ans: Option>> = obj.deserialize(data); - */ \ No newline at end of file + */ diff --git a/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.rs b/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.rs index b90aaeaaa38fe..293340beabfb6 100644 --- a/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.rs +++ b/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.rs @@ -1,14 +1,14 @@ -impl Solution { - pub fn length_of_lis(nums: Vec) -> i32 { - let n = nums.len(); - let mut f = vec![1; n]; - for i in 1..n { - for j in 0..i { - if nums[j] < nums[i] { - f[i] = f[i].max(f[j] + 1); - } - } - } - *f.iter().max().unwrap() - } -} \ No newline at end of file +impl Solution { + pub fn length_of_lis(nums: Vec) -> i32 { + let n = nums.len(); + let mut f = vec![1; n]; + for i in 1..n { + for j in 0..i { + if nums[j] < nums[i] { + f[i] = f[i].max(f[j] + 1); + } + } + } + *f.iter().max().unwrap() + } +} diff --git a/solution/0300-0399/0303.Range Sum Query - Immutable/README.md b/solution/0300-0399/0303.Range Sum Query - Immutable/README.md index 406d3a6a1436d..f7e1b3fe77262 100644 --- a/solution/0300-0399/0303.Range Sum Query - Immutable/README.md +++ b/solution/0300-0399/0303.Range Sum Query - Immutable/README.md @@ -218,19 +218,17 @@ class NumArray { ```rust struct NumArray { - nums: Vec + nums: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumArray { - fn new(nums: Vec) -> Self { Self { - nums + nums, } } @@ -238,9 +236,7 @@ impl NumArray { let (left, right) = (left as usize, right as usize); self.nums[left..=right].iter().sum::() } -} - -/** +}/** * Your NumArray object will be instantiated and called as such: * let obj = NumArray::new(nums); * let ret_1: i32 = obj.sum_range(left, right); @@ -252,13 +248,11 @@ struct NumArray { sums: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumArray { - fn new(mut nums: Vec) -> Self { let n = nums.len(); let mut sums = vec![0; n + 1]; @@ -271,9 +265,7 @@ impl NumArray { fn sum_range(&self, left: i32, right: i32) -> i32 { self.sums[(right + 1) as usize] - self.sums[left as usize] } -} - -/** +}/** * Your NumArray object will be instantiated and called as such: * let obj = NumArray::new(nums); * let ret_1: i32 = obj.sum_range(left, right); diff --git a/solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md b/solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md index 450fc0bc0b9fc..3ae04765ad644 100644 --- a/solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md +++ b/solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md @@ -202,19 +202,17 @@ class NumArray { ```rust struct NumArray { - nums: Vec + nums: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumArray { - fn new(nums: Vec) -> Self { Self { - nums + nums, } } @@ -222,9 +220,7 @@ impl NumArray { let (left, right) = (left as usize, right as usize); self.nums[left..=right].iter().sum::() } -} - -/** +}/** * Your NumArray object will be instantiated and called as such: * let obj = NumArray::new(nums); * let ret_1: i32 = obj.sum_range(left, right); @@ -236,13 +232,11 @@ struct NumArray { sums: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumArray { - fn new(mut nums: Vec) -> Self { let n = nums.len(); let mut sums = vec![0; n + 1]; @@ -255,9 +249,7 @@ impl NumArray { fn sum_range(&self, left: i32, right: i32) -> i32 { self.sums[(right + 1) as usize] - self.sums[left as usize] } -} - -/** +}/** * Your NumArray object will be instantiated and called as such: * let obj = NumArray::new(nums); * let ret_1: i32 = obj.sum_range(left, right); diff --git a/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.rs b/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.rs index ea8def2147372..5d2a6cccb368e 100644 --- a/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.rs +++ b/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.rs @@ -2,7 +2,6 @@ struct NumArray { sums: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -20,9 +19,7 @@ impl NumArray { fn sum_range(&self, left: i32, right: i32) -> i32 { self.sums[(right + 1) as usize] - self.sums[left as usize] } -} - -/** +}/** * Your NumArray object will be instantiated and called as such: * let obj = NumArray::new(nums); * let ret_1: i32 = obj.sum_range(left, right); diff --git a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md index da09f28f20dbc..c140bb0b88dfa 100644 --- a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md +++ b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md @@ -183,13 +183,11 @@ struct NumMatrix { ref_vec: Vec>, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumMatrix { - fn new(matrix: Vec>) -> Self { NumMatrix { prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1], @@ -210,16 +208,21 @@ impl NumMatrix { let row2: usize = row2 as usize; let col2: usize = col2 as usize; // Return the value in O(1) - self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1] - - self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1] + self.prefix_vec[row2 + 1][col2 + 1] - + self.prefix_vec[row2 + 1][col1] - + self.prefix_vec[row1][col2 + 1] + + self.prefix_vec[row1][col1] } fn initialize_prefix_vec(&mut self) { // Initialize the prefix sum vector for i in 0..self.n { for j in 0..self.m { - self.prefix_vec[i + 1][j + 1] = - self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j]; + self.prefix_vec[i + 1][j + 1] = + self.prefix_vec[i][j + 1] + + self.prefix_vec[i + 1][j] - + self.prefix_vec[i][j] + + self.ref_vec[i][j]; } } self.is_initialized = true; diff --git a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md index c8aecb9a4f45b..bef073a8ed9ef 100644 --- a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md +++ b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md @@ -165,7 +165,7 @@ public: * let ret_1: i32 = obj.sum_region(row1, col1, row2, col2); */ - struct NumMatrix { +struct NumMatrix { // Of size (N + 1) * (M + 1) prefix_vec: Vec>, n: usize, @@ -174,13 +174,11 @@ public: ref_vec: Vec>, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumMatrix { - fn new(matrix: Vec>) -> Self { NumMatrix { prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1], @@ -201,16 +199,21 @@ impl NumMatrix { let row2: usize = row2 as usize; let col2: usize = col2 as usize; // Return the value in O(1) - self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1] - - self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1] + self.prefix_vec[row2 + 1][col2 + 1] - + self.prefix_vec[row2 + 1][col1] - + self.prefix_vec[row1][col2 + 1] + + self.prefix_vec[row1][col1] } fn initialize_prefix_vec(&mut self) { // Initialize the prefix sum vector for i in 0..self.n { for j in 0..self.m { - self.prefix_vec[i + 1][j + 1] = - self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j]; + self.prefix_vec[i + 1][j + 1] = + self.prefix_vec[i][j + 1] + + self.prefix_vec[i + 1][j] - + self.prefix_vec[i][j] + + self.ref_vec[i][j]; } } self.is_initialized = true; diff --git a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.rs b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.rs index 35432c4bcf797..7025b7611f24d 100644 --- a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.rs +++ b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.rs @@ -4,8 +4,8 @@ * let ret_1: i32 = obj.sum_region(row1, col1, row2, col2); */ - struct NumMatrix { - // Of size (N + 1) * (M + 1) +struct NumMatrix { + // Of size (N + 1) * (M + 1) prefix_vec: Vec>, n: usize, m: usize, @@ -13,15 +13,13 @@ ref_vec: Vec>, } - -/** +/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NumMatrix { - fn new(matrix: Vec>) -> Self { - NumMatrix { + NumMatrix { prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1], n: matrix.len(), m: matrix[0].len(), @@ -29,7 +27,7 @@ impl NumMatrix { ref_vec: matrix, } } - + fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 { if !self.is_initialized { self.initialize_prefix_vec(); @@ -40,18 +38,23 @@ impl NumMatrix { let row2: usize = row2 as usize; let col2: usize = col2 as usize; // Return the value in O(1) - self.prefix_vec[row2 + 1][col2 + 1] - self.prefix_vec[row2 + 1][col1] - - self.prefix_vec[row1][col2 + 1] + self.prefix_vec[row1][col1] + self.prefix_vec[row2 + 1][col2 + 1] - + self.prefix_vec[row2 + 1][col1] - + self.prefix_vec[row1][col2 + 1] + + self.prefix_vec[row1][col1] } fn initialize_prefix_vec(&mut self) { // Initialize the prefix sum vector for i in 0..self.n { for j in 0..self.m { - self.prefix_vec[i + 1][j + 1] = - self.prefix_vec[i][j + 1] + self.prefix_vec[i + 1][j] - self.prefix_vec[i][j] + self.ref_vec[i][j]; + self.prefix_vec[i + 1][j + 1] = + self.prefix_vec[i][j + 1] + + self.prefix_vec[i + 1][j] - + self.prefix_vec[i][j] + + self.ref_vec[i][j]; } } self.is_initialized = true; } -} \ No newline at end of file +} diff --git a/solution/0300-0399/0322.Coin Change/README.md b/solution/0300-0399/0322.Coin Change/README.md index 7adc1606acbb2..f87895b8f13c0 100644 --- a/solution/0300-0399/0322.Coin Change/README.md +++ b/solution/0300-0399/0322.Coin Change/README.md @@ -341,7 +341,7 @@ impl Solution { f[0] = 0; for &x in &coins { for j in x as usize..=n { - f[j] = f[j].min(f[j - x as usize] + 1); + f[j] = f[j].min(f[j - (x as usize)] + 1); } } if f[n] > n { diff --git a/solution/0300-0399/0322.Coin Change/README_EN.md b/solution/0300-0399/0322.Coin Change/README_EN.md index fad50a15fa6b8..115fe694aae5d 100644 --- a/solution/0300-0399/0322.Coin Change/README_EN.md +++ b/solution/0300-0399/0322.Coin Change/README_EN.md @@ -299,7 +299,7 @@ impl Solution { f[0] = 0; for &x in &coins { for j in x as usize..=n { - f[j] = f[j].min(f[j - x as usize] + 1); + f[j] = f[j].min(f[j - (x as usize)] + 1); } } if f[n] > n { diff --git a/solution/0300-0399/0322.Coin Change/Solution.rs b/solution/0300-0399/0322.Coin Change/Solution.rs index 957a7c189e7f0..4a2c22760aa07 100644 --- a/solution/0300-0399/0322.Coin Change/Solution.rs +++ b/solution/0300-0399/0322.Coin Change/Solution.rs @@ -1,17 +1,17 @@ -impl Solution { - pub fn coin_change(coins: Vec, amount: i32) -> i32 { - let n = amount as usize; - let mut f = vec![n + 1; n + 1]; - f[0] = 0; - for &x in &coins { - for j in x as usize..=n { - f[j] = f[j].min(f[j - x as usize] + 1); - } - } - if f[n] > n { - -1 - } else { - f[n] as i32 - } - } -} +impl Solution { + pub fn coin_change(coins: Vec, amount: i32) -> i32 { + let n = amount as usize; + let mut f = vec![n + 1; n + 1]; + f[0] = 0; + for &x in &coins { + for j in x as usize..=n { + f[j] = f[j].min(f[j - (x as usize)] + 1); + } + } + if f[n] > n { + -1 + } else { + f[n] as i32 + } + } +} diff --git a/solution/0300-0399/0341.Flatten Nested List Iterator/README.md b/solution/0300-0399/0341.Flatten Nested List Iterator/README.md index 395f5deadfc74..ed91c41d5b492 100644 --- a/solution/0300-0399/0341.Flatten Nested List Iterator/README.md +++ b/solution/0300-0399/0341.Flatten Nested List Iterator/README.md @@ -333,13 +333,11 @@ struct NestedIterator { vals: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NestedIterator { - fn dfs(nestedList: &Vec, vals: &mut Vec) { for ele in nestedList.iter() { match ele { @@ -367,9 +365,7 @@ impl NestedIterator { fn has_next(&self) -> bool { self.index < self.vals.len() } -} - -/** +}/** * Your NestedIterator object will be instantiated and called as such: * let obj = NestedIterator::new(nestedList); * let ret_1: i32 = obj.next(); diff --git a/solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md b/solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md index 7cfa4499afa56..69893080ba465 100644 --- a/solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md +++ b/solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md @@ -317,13 +317,11 @@ struct NestedIterator { vals: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NestedIterator { - fn dfs(nestedList: &Vec, vals: &mut Vec) { for ele in nestedList.iter() { match ele { @@ -351,9 +349,7 @@ impl NestedIterator { fn has_next(&self) -> bool { self.index < self.vals.len() } -} - -/** +}/** * Your NestedIterator object will be instantiated and called as such: * let obj = NestedIterator::new(nestedList); * let ret_1: i32 = obj.next(); diff --git a/solution/0300-0399/0341.Flatten Nested List Iterator/Solution.rs b/solution/0300-0399/0341.Flatten Nested List Iterator/Solution.rs index 62cab4d1033a5..f1d127cbb7045 100644 --- a/solution/0300-0399/0341.Flatten Nested List Iterator/Solution.rs +++ b/solution/0300-0399/0341.Flatten Nested List Iterator/Solution.rs @@ -8,13 +8,11 @@ struct NestedIterator { vals: Vec, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl NestedIterator { - fn dfs(nestedList: &Vec, vals: &mut Vec) { for ele in nestedList.iter() { match ele { @@ -42,11 +40,9 @@ impl NestedIterator { fn has_next(&self) -> bool { self.index < self.vals.len() } -} - -/** +}/** * Your NestedIterator object will be instantiated and called as such: * let obj = NestedIterator::new(nestedList); * let ret_1: i32 = obj.next(); * let ret_2: bool = obj.has_next(); - */ \ No newline at end of file + */ diff --git a/solution/0300-0399/0343.Integer Break/README.md b/solution/0300-0399/0343.Integer Break/README.md index 0ad6bf022b592..71ed2fe538ce0 100644 --- a/solution/0300-0399/0343.Integer Break/README.md +++ b/solution/0300-0399/0343.Integer Break/README.md @@ -207,7 +207,7 @@ impl Solution { return n - 1; } let count = (n - 2) / 3; - 3i32.pow(count as u32) * (n - count * 3) + (3i32).pow(count as u32) * (n - count * 3) } } ``` diff --git a/solution/0300-0399/0343.Integer Break/README_EN.md b/solution/0300-0399/0343.Integer Break/README_EN.md index ceffdc2cae349..71fc4632b5b9d 100644 --- a/solution/0300-0399/0343.Integer Break/README_EN.md +++ b/solution/0300-0399/0343.Integer Break/README_EN.md @@ -183,7 +183,7 @@ impl Solution { return n - 1; } let count = (n - 2) / 3; - 3i32.pow(count as u32) * (n - count * 3) + (3i32).pow(count as u32) * (n - count * 3) } } ``` diff --git a/solution/0300-0399/0343.Integer Break/Solution.rs b/solution/0300-0399/0343.Integer Break/Solution.rs index f1c14a81008bb..cbd12dbf0bbf0 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.rs +++ b/solution/0300-0399/0343.Integer Break/Solution.rs @@ -4,6 +4,6 @@ impl Solution { return n - 1; } let count = (n - 2) / 3; - 3i32.pow(count as u32) * (n - count * 3) + (3i32).pow(count as u32) * (n - count * 3) } } diff --git a/solution/0300-0399/0344.Reverse String/Solution.rs b/solution/0300-0399/0344.Reverse String/Solution.rs index e68688a9645bb..07dff26755f9a 100644 --- a/solution/0300-0399/0344.Reverse String/Solution.rs +++ b/solution/0300-0399/0344.Reverse String/Solution.rs @@ -1,11 +1,11 @@ -impl Solution { - pub fn reverse_string(s: &mut Vec) { - let mut i = 0; - let mut j = s.len() - 1; - while i < j { - s.swap(i, j); - i += 1; - j -= 1; - } - } -} +impl Solution { + pub fn reverse_string(s: &mut Vec) { + let mut i = 0; + let mut j = s.len() - 1; + while i < j { + s.swap(i, j); + i += 1; + j -= 1; + } + } +} diff --git a/solution/0300-0399/0345.Reverse Vowels of a String/Solution.rs b/solution/0300-0399/0345.Reverse Vowels of a String/Solution.rs index 04383cb7a035a..3b4dc476910cc 100644 --- a/solution/0300-0399/0345.Reverse Vowels of a String/Solution.rs +++ b/solution/0300-0399/0345.Reverse Vowels of a String/Solution.rs @@ -1,21 +1,21 @@ -impl Solution { - pub fn reverse_vowels(s: String) -> String { - let vowel = String::from("aeiouAEIOU"); - let mut data: Vec = s.chars().collect(); - let (mut i, mut j) = (0, s.len() - 1); - while i < j { - while i < j && !vowel.contains(data[i]) { - i += 1; - } - while i < j && !vowel.contains(data[j]) { - j -= 1; - } - if i < j { - data.swap(i, j); - i += 1; - j -= 1; - } - } - data.iter().collect() - } -} +impl Solution { + pub fn reverse_vowels(s: String) -> String { + let vowel = String::from("aeiouAEIOU"); + let mut data: Vec = s.chars().collect(); + let (mut i, mut j) = (0, s.len() - 1); + while i < j { + while i < j && !vowel.contains(data[i]) { + i += 1; + } + while i < j && !vowel.contains(data[j]) { + j -= 1; + } + if i < j { + data.swap(i, j); + i += 1; + j -= 1; + } + } + data.iter().collect() + } +} diff --git a/solution/0300-0399/0362.Design Hit Counter/README.md b/solution/0300-0399/0362.Design Hit Counter/README.md index c6bf6c4e88fe7..0f04086aa8a7d 100644 --- a/solution/0300-0399/0362.Design Hit Counter/README.md +++ b/solution/0300-0399/0362.Design Hit Counter/README.md @@ -98,7 +98,7 @@ class HitCounter: ### **Rust** ```rust -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; struct HitCounter { /// A min heap diff --git a/solution/0300-0399/0362.Design Hit Counter/README_EN.md b/solution/0300-0399/0362.Design Hit Counter/README_EN.md index 0a09a00cddf72..79ffc82e12160 100644 --- a/solution/0300-0399/0362.Design Hit Counter/README_EN.md +++ b/solution/0300-0399/0362.Design Hit Counter/README_EN.md @@ -87,7 +87,7 @@ class HitCounter: ### **Rust** ```rust -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; struct HitCounter { /// A min heap diff --git a/solution/0300-0399/0362.Design Hit Counter/Solution.rs b/solution/0300-0399/0362.Design Hit Counter/Solution.rs index c012fecc05a25..42fbbc6c55735 100644 --- a/solution/0300-0399/0362.Design Hit Counter/Solution.rs +++ b/solution/0300-0399/0362.Design Hit Counter/Solution.rs @@ -1,4 +1,4 @@ -use std::{collections::BinaryHeap, cmp::Reverse}; +use std::{ collections::BinaryHeap, cmp::Reverse }; struct HitCounter { /// A min heap @@ -27,4 +27,4 @@ impl HitCounter { self.pq.len() as i32 } -} \ No newline at end of file +} diff --git a/solution/0300-0399/0367.Valid Perfect Square/README.md b/solution/0300-0399/0367.Valid Perfect Square/README.md index 8d3cf26d49aad..68097692b6194 100644 --- a/solution/0300-0399/0367.Valid Perfect Square/README.md +++ b/solution/0300-0399/0367.Valid Perfect Square/README.md @@ -211,9 +211,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match (mid * mid).cmp(&num) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid - 1, - Ordering::Equal => return true, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid - 1; + } + Ordering::Equal => { + return true; + } } } left * left == num diff --git a/solution/0300-0399/0367.Valid Perfect Square/README_EN.md b/solution/0300-0399/0367.Valid Perfect Square/README_EN.md index 0167f60bf285e..02cd139a13935 100644 --- a/solution/0300-0399/0367.Valid Perfect Square/README_EN.md +++ b/solution/0300-0399/0367.Valid Perfect Square/README_EN.md @@ -206,9 +206,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match (mid * mid).cmp(&num) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid - 1, - Ordering::Equal => return true, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid - 1; + } + Ordering::Equal => { + return true; + } } } left * left == num diff --git a/solution/0300-0399/0367.Valid Perfect Square/Solution.rs b/solution/0300-0399/0367.Valid Perfect Square/Solution.rs index a3bf068fb5fd7..764668be9f320 100644 --- a/solution/0300-0399/0367.Valid Perfect Square/Solution.rs +++ b/solution/0300-0399/0367.Valid Perfect Square/Solution.rs @@ -7,9 +7,15 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match (mid * mid).cmp(&num) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid - 1, - Ordering::Equal => return true, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid - 1; + } + Ordering::Equal => { + return true; + } } } left * left == num diff --git a/solution/0300-0399/0374.Guess Number Higher or Lower/README.md b/solution/0300-0399/0374.Guess Number Higher or Lower/README.md index 98af5d11c83a9..8a2c19c94540e 100644 --- a/solution/0300-0399/0374.Guess Number Higher or Lower/README.md +++ b/solution/0300-0399/0374.Guess Number Higher or Lower/README.md @@ -288,9 +288,15 @@ impl Solution { loop { let mid = l + (r - l) / 2; match guess(mid) { - -1 => r = mid - 1, - 1 => l = mid + 1, - _ => break mid, + -1 => { + r = mid - 1; + } + 1 => { + l = mid + 1; + } + _ => { + break mid; + } } } } diff --git a/solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md b/solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md index 581a8a2ca02a4..dfbb2f45d4e9b 100644 --- a/solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md +++ b/solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md @@ -264,9 +264,15 @@ impl Solution { loop { let mid = l + (r - l) / 2; match guess(mid) { - -1 => r = mid - 1, - 1 => l = mid + 1, - _ => break mid, + -1 => { + r = mid - 1; + } + 1 => { + l = mid + 1; + } + _ => { + break mid; + } } } } diff --git a/solution/0300-0399/0374.Guess Number Higher or Lower/Solution.rs b/solution/0300-0399/0374.Guess Number Higher or Lower/Solution.rs index 4e4e4a2a65aa6..aaf5757e787af 100644 --- a/solution/0300-0399/0374.Guess Number Higher or Lower/Solution.rs +++ b/solution/0300-0399/0374.Guess Number Higher or Lower/Solution.rs @@ -14,9 +14,15 @@ impl Solution { loop { let mid = l + (r - l) / 2; match guess(mid) { - -1 => r = mid - 1, - 1 => l = mid + 1, - _ => break mid, + -1 => { + r = mid - 1; + } + 1 => { + l = mid + 1; + } + _ => { + break mid; + } } } } diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README.md b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README.md index 3641296591c95..b2eb77d4399c3 100644 --- a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README.md +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README.md @@ -301,19 +301,17 @@ use std::collections::HashSet; use rand::Rng; struct RandomizedSet { - list: HashSet + list: HashSet, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RandomizedSet { - fn new() -> Self { Self { - list: HashSet::new() + list: HashSet::new(), } } @@ -329,9 +327,7 @@ impl RandomizedSet { let i = rand::thread_rng().gen_range(0, self.list.len()); *self.list.iter().collect::>()[i] } -} - -/** +}/** * Your RandomizedSet object will be instantiated and called as such: * let obj = RandomizedSet::new(); * let ret_1: bool = obj.insert(val); diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README_EN.md b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README_EN.md index 1a132c3f78d01..d73d7a877e9ea 100644 --- a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README_EN.md +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/README_EN.md @@ -287,19 +287,17 @@ use std::collections::HashSet; use rand::Rng; struct RandomizedSet { - list: HashSet + list: HashSet, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RandomizedSet { - fn new() -> Self { Self { - list: HashSet::new() + list: HashSet::new(), } } @@ -315,9 +313,7 @@ impl RandomizedSet { let i = rand::thread_rng().gen_range(0, self.list.len()); *self.list.iter().collect::>()[i] } -} - -/** +}/** * Your RandomizedSet object will be instantiated and called as such: * let obj = RandomizedSet::new(); * let ret_1: bool = obj.insert(val); diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.rs b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.rs index e7c9d9795e3e8..284c2eb930dc5 100644 --- a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.rs +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.rs @@ -2,19 +2,17 @@ use std::collections::HashSet; use rand::Rng; struct RandomizedSet { - list: HashSet + list: HashSet, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RandomizedSet { - fn new() -> Self { Self { - list: HashSet::new() + list: HashSet::new(), } } @@ -30,12 +28,10 @@ impl RandomizedSet { let i = rand::thread_rng().gen_range(0, self.list.len()); *self.list.iter().collect::>()[i] } -} - -/** +}/** * Your RandomizedSet object will be instantiated and called as such: * let obj = RandomizedSet::new(); * let ret_1: bool = obj.insert(val); * let ret_2: bool = obj.remove(val); * let ret_3: i32 = obj.get_random(); - */ \ No newline at end of file + */ diff --git a/solution/0300-0399/0384.Shuffle an Array/README.md b/solution/0300-0399/0384.Shuffle an Array/README.md index 0488ccde99f88..5f649b7cf28fb 100644 --- a/solution/0300-0399/0384.Shuffle an Array/README.md +++ b/solution/0300-0399/0384.Shuffle an Array/README.md @@ -294,9 +294,7 @@ impl Solution { } res } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(nums); * let ret_1: Vec = obj.reset(); diff --git a/solution/0300-0399/0384.Shuffle an Array/README_EN.md b/solution/0300-0399/0384.Shuffle an Array/README_EN.md index 0f9f9bee98fe2..a1e4c7b5b0127 100644 --- a/solution/0300-0399/0384.Shuffle an Array/README_EN.md +++ b/solution/0300-0399/0384.Shuffle an Array/README_EN.md @@ -287,9 +287,7 @@ impl Solution { } res } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(nums); * let ret_1: Vec = obj.reset(); diff --git a/solution/0300-0399/0384.Shuffle an Array/Solution.rs b/solution/0300-0399/0384.Shuffle an Array/Solution.rs index db9e0f8f9e7b3..8863252b70ee4 100644 --- a/solution/0300-0399/0384.Shuffle an Array/Solution.rs +++ b/solution/0300-0399/0384.Shuffle an Array/Solution.rs @@ -25,11 +25,9 @@ impl Solution { } res } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(nums); * let ret_1: Vec = obj.reset(); * let ret_2: Vec = obj.shuffle(); - */ \ No newline at end of file + */ diff --git a/solution/0300-0399/0389.Find the Difference/README.md b/solution/0300-0399/0389.Find the Difference/README.md index d95617b8fb051..8c527105f05d5 100644 --- a/solution/0300-0399/0389.Find the Difference/README.md +++ b/solution/0300-0399/0389.Find the Difference/README.md @@ -181,7 +181,15 @@ impl Solution { count[(t[i] - b'a') as usize] -= 1; } count[(t[n] - b'a') as usize] -= 1; - char::from(b'a' + count.iter().position(|&v| v != 0).unwrap() as u8) + char::from( + b'a' + + ( + count + .iter() + .position(|&v| v != 0) + .unwrap() as u8 + ) + ) } } ``` diff --git a/solution/0300-0399/0389.Find the Difference/README_EN.md b/solution/0300-0399/0389.Find the Difference/README_EN.md index a0cdcddf09452..ff8812371017f 100644 --- a/solution/0300-0399/0389.Find the Difference/README_EN.md +++ b/solution/0300-0399/0389.Find the Difference/README_EN.md @@ -157,7 +157,15 @@ impl Solution { count[(t[i] - b'a') as usize] -= 1; } count[(t[n] - b'a') as usize] -= 1; - char::from(b'a' + count.iter().position(|&v| v != 0).unwrap() as u8) + char::from( + b'a' + + ( + count + .iter() + .position(|&v| v != 0) + .unwrap() as u8 + ) + ) } } ``` diff --git a/solution/0300-0399/0396.Rotate Function/README.md b/solution/0300-0399/0396.Rotate Function/README.md index cc7cb5ee0e23b..2db47ba893ea9 100644 --- a/solution/0300-0399/0396.Rotate Function/README.md +++ b/solution/0300-0399/0396.Rotate Function/README.md @@ -167,11 +167,15 @@ impl Solution { pub fn max_rotate_function(nums: Vec) -> i32 { let n = nums.len(); let sum: i32 = nums.iter().sum(); - let mut pre: i32 = nums.iter().enumerate().map(|(i, &v)| i as i32 * v).sum(); + let mut pre: i32 = nums + .iter() + .enumerate() + .map(|(i, &v)| (i as i32) * v) + .sum(); (0..n) .map(|i| { let res = pre; - pre = pre - (sum - nums[i]) + nums[i] * (n - 1) as i32; + pre = pre - (sum - nums[i]) + nums[i] * ((n - 1) as i32); res }) .max() diff --git a/solution/0300-0399/0396.Rotate Function/README_EN.md b/solution/0300-0399/0396.Rotate Function/README_EN.md index 4aebafbe49e2d..f1e2fc43139be 100644 --- a/solution/0300-0399/0396.Rotate Function/README_EN.md +++ b/solution/0300-0399/0396.Rotate Function/README_EN.md @@ -150,11 +150,15 @@ impl Solution { pub fn max_rotate_function(nums: Vec) -> i32 { let n = nums.len(); let sum: i32 = nums.iter().sum(); - let mut pre: i32 = nums.iter().enumerate().map(|(i, &v)| i as i32 * v).sum(); + let mut pre: i32 = nums + .iter() + .enumerate() + .map(|(i, &v)| (i as i32) * v) + .sum(); (0..n) .map(|i| { let res = pre; - pre = pre - (sum - nums[i]) + nums[i] * (n - 1) as i32; + pre = pre - (sum - nums[i]) + nums[i] * ((n - 1) as i32); res }) .max() diff --git a/solution/0300-0399/0396.Rotate Function/Solution.rs b/solution/0300-0399/0396.Rotate Function/Solution.rs index 90dbb80fdeeb9..6af8b42ed80d7 100644 --- a/solution/0300-0399/0396.Rotate Function/Solution.rs +++ b/solution/0300-0399/0396.Rotate Function/Solution.rs @@ -2,11 +2,15 @@ impl Solution { pub fn max_rotate_function(nums: Vec) -> i32 { let n = nums.len(); let sum: i32 = nums.iter().sum(); - let mut pre: i32 = nums.iter().enumerate().map(|(i, &v)| i as i32 * v).sum(); + let mut pre: i32 = nums + .iter() + .enumerate() + .map(|(i, &v)| (i as i32) * v) + .sum(); (0..n) .map(|i| { let res = pre; - pre = pre - (sum - nums[i]) + nums[i] * (n - 1) as i32; + pre = pre - (sum - nums[i]) + nums[i] * ((n - 1) as i32); res }) .max() diff --git a/solution/0300-0399/0399.Evaluate Division/README.md b/solution/0300-0399/0399.Evaluate Division/README.md index dc768fcd7600d..7c2406bd73c42 100644 --- a/solution/0300-0399/0399.Evaluate Division/README.md +++ b/solution/0300-0399/0399.Evaluate Division/README.md @@ -325,13 +325,10 @@ impl DisjointSetUnion { let mut nodes = HashMap::new(); for equation in equations.iter() { for iter in equation.iter() { - nodes.insert( - iter.clone(), - DSUNode { - parent: iter.clone(), - weight: 1.0, - }, - ); + nodes.insert(iter.clone(), DSUNode { + parent: iter.clone(), + weight: 1.0, + }); } } DisjointSetUnion { nodes } @@ -357,7 +354,7 @@ impl DisjointSetUnion { } let (wa, wb) = (self.nodes[a].weight, self.nodes[b].weight); self.nodes.get_mut(&pa).unwrap().parent = pb; - self.nodes.get_mut(&pa).unwrap().weight = wb * v / wa; + self.nodes.get_mut(&pa).unwrap().weight = (wb * v) / wa; } pub fn exist(&mut self, k: &String) -> bool { @@ -379,7 +376,7 @@ impl Solution { pub fn calc_equation( equations: Vec>, values: Vec, - queries: Vec>, + queries: Vec> ) -> Vec { let mut dsu = DisjointSetUnion::new(&equations); for (i, &v) in values.iter().enumerate() { diff --git a/solution/0300-0399/0399.Evaluate Division/README_EN.md b/solution/0300-0399/0399.Evaluate Division/README_EN.md index 73b9fb26f127d..69a035fac1e49 100644 --- a/solution/0300-0399/0399.Evaluate Division/README_EN.md +++ b/solution/0300-0399/0399.Evaluate Division/README_EN.md @@ -250,13 +250,10 @@ impl DisjointSetUnion { let mut nodes = HashMap::new(); for equation in equations.iter() { for iter in equation.iter() { - nodes.insert( - iter.clone(), - DSUNode { - parent: iter.clone(), - weight: 1.0, - }, - ); + nodes.insert(iter.clone(), DSUNode { + parent: iter.clone(), + weight: 1.0, + }); } } DisjointSetUnion { nodes } @@ -282,7 +279,7 @@ impl DisjointSetUnion { } let (wa, wb) = (self.nodes[a].weight, self.nodes[b].weight); self.nodes.get_mut(&pa).unwrap().parent = pb; - self.nodes.get_mut(&pa).unwrap().weight = wb * v / wa; + self.nodes.get_mut(&pa).unwrap().weight = (wb * v) / wa; } pub fn exist(&mut self, k: &String) -> bool { @@ -304,7 +301,7 @@ impl Solution { pub fn calc_equation( equations: Vec>, values: Vec, - queries: Vec>, + queries: Vec> ) -> Vec { let mut dsu = DisjointSetUnion::new(&equations); for (i, &v) in values.iter().enumerate() { diff --git a/solution/0300-0399/0399.Evaluate Division/Solution.rs b/solution/0300-0399/0399.Evaluate Division/Solution.rs index de2dc74e32ed6..ade43ec495f66 100644 --- a/solution/0300-0399/0399.Evaluate Division/Solution.rs +++ b/solution/0300-0399/0399.Evaluate Division/Solution.rs @@ -15,13 +15,10 @@ impl DisjointSetUnion { let mut nodes = HashMap::new(); for equation in equations.iter() { for iter in equation.iter() { - nodes.insert( - iter.clone(), - DSUNode { - parent: iter.clone(), - weight: 1.0, - }, - ); + nodes.insert(iter.clone(), DSUNode { + parent: iter.clone(), + weight: 1.0, + }); } } DisjointSetUnion { nodes } @@ -47,7 +44,7 @@ impl DisjointSetUnion { } let (wa, wb) = (self.nodes[a].weight, self.nodes[b].weight); self.nodes.get_mut(&pa).unwrap().parent = pb; - self.nodes.get_mut(&pa).unwrap().weight = wb * v / wa; + self.nodes.get_mut(&pa).unwrap().weight = (wb * v) / wa; } pub fn exist(&mut self, k: &String) -> bool { @@ -69,7 +66,7 @@ impl Solution { pub fn calc_equation( equations: Vec>, values: Vec, - queries: Vec>, + queries: Vec> ) -> Vec { let mut dsu = DisjointSetUnion::new(&equations); for (i, &v) in values.iter().enumerate() { diff --git a/solution/0400-0499/0403.Frog Jump/README.md b/solution/0400-0499/0403.Frog Jump/README.md index 3abce654ef28c..0ffba118de841 100644 --- a/solution/0400-0499/0403.Frog Jump/README.md +++ b/solution/0400-0499/0403.Frog Jump/README.md @@ -249,7 +249,14 @@ impl Solution { } #[allow(dead_code)] - fn dfs(record: &mut Vec>, i: usize, k: usize, n: usize, pos: &HashMap, stones: &Vec) -> bool { + fn dfs( + record: &mut Vec>, + i: usize, + k: usize, + n: usize, + pos: &HashMap, + stones: &Vec + ) -> bool { if i == n - 1 { return true; } @@ -260,7 +267,11 @@ impl Solution { let k = k as i32; for j in k - 1..=k + 1 { - if j > 0 && pos.contains_key(&(stones[i] + j)) && Self::dfs(record, pos[&(stones[i] + j)], j as usize, n, pos, stones) { + if + j > 0 && + pos.contains_key(&(stones[i] + j)) && + Self::dfs(record, pos[&(stones[i] + j)], j as usize, n, pos, stones) + { record[i][k as usize] = 1; return true; } diff --git a/solution/0400-0499/0403.Frog Jump/README_EN.md b/solution/0400-0499/0403.Frog Jump/README_EN.md index 02c3a28e7a9da..c33cf691112d2 100644 --- a/solution/0400-0499/0403.Frog Jump/README_EN.md +++ b/solution/0400-0499/0403.Frog Jump/README_EN.md @@ -241,7 +241,14 @@ impl Solution { } #[allow(dead_code)] - fn dfs(record: &mut Vec>, i: usize, k: usize, n: usize, pos: &HashMap, stones: &Vec) -> bool { + fn dfs( + record: &mut Vec>, + i: usize, + k: usize, + n: usize, + pos: &HashMap, + stones: &Vec + ) -> bool { if i == n - 1 { return true; } @@ -252,7 +259,11 @@ impl Solution { let k = k as i32; for j in k - 1..=k + 1 { - if j > 0 && pos.contains_key(&(stones[i] + j)) && Self::dfs(record, pos[&(stones[i] + j)], j as usize, n, pos, stones) { + if + j > 0 && + pos.contains_key(&(stones[i] + j)) && + Self::dfs(record, pos[&(stones[i] + j)], j as usize, n, pos, stones) + { record[i][k as usize] = 1; return true; } diff --git a/solution/0400-0499/0403.Frog Jump/Solution.rs b/solution/0400-0499/0403.Frog Jump/Solution.rs index e78f08b04cd2c..202892da3c328 100644 --- a/solution/0400-0499/0403.Frog Jump/Solution.rs +++ b/solution/0400-0499/0403.Frog Jump/Solution.rs @@ -23,4 +23,4 @@ impl Solution { false } -} \ No newline at end of file +} diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index b82d342e504fa..1c8560193ba52 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -196,7 +196,7 @@ impl Solution { res -= 1; } } - res + if has_odd { 1 } else { 0 } + res + (if has_odd { 1 } else { 0 }) } } ``` diff --git a/solution/0400-0499/0409.Longest Palindrome/README_EN.md b/solution/0400-0499/0409.Longest Palindrome/README_EN.md index 35829cb97c506..0628e416143cf 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README_EN.md +++ b/solution/0400-0499/0409.Longest Palindrome/README_EN.md @@ -168,7 +168,7 @@ impl Solution { res -= 1; } } - res + if has_odd { 1 } else { 0 } + res + (if has_odd { 1 } else { 0 }) } } ``` diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution.rs b/solution/0400-0499/0409.Longest Palindrome/Solution.rs index d883a42532dcd..781576a7af8c0 100644 --- a/solution/0400-0499/0409.Longest Palindrome/Solution.rs +++ b/solution/0400-0499/0409.Longest Palindrome/Solution.rs @@ -15,6 +15,6 @@ impl Solution { res -= 1; } } - res + if has_odd { 1 } else { 0 } + res + (if has_odd { 1 } else { 0 }) } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md index 8cbd66b5379f7..b1c53a7127388 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md @@ -217,11 +217,11 @@ impl Solution { // Begin the actual dp process for i in 1..=n { for j in 0..=m { - dp[i][j] = if nums[i - 1] as usize > j { + dp[i][j] = if (nums[i - 1] as usize) > j { dp[i - 1][j] } else { - dp[i - 1][j] || dp[i - 1][j - nums[i - 1] as usize] - } + dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] + }; } } @@ -257,7 +257,7 @@ impl Solution { // For every num in nums vector for i in (*e as usize..=m).rev() { // Update the current status - dp[i] |= dp[i - *e as usize]; + dp[i] |= dp[i - (*e as usize)]; } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md index f115e5c9ad7a9..2ddafdf4a6e83 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md @@ -190,11 +190,11 @@ impl Solution { // Begin the actual dp process for i in 1..=n { for j in 0..=m { - dp[i][j] = if nums[i - 1] as usize > j { + dp[i][j] = if (nums[i - 1] as usize) > j { dp[i - 1][j] } else { - dp[i - 1][j] || dp[i - 1][j - nums[i - 1] as usize] - } + dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] + }; } } @@ -230,7 +230,7 @@ impl Solution { // For every num in nums vector for i in (*e as usize..=m).rev() { // Update the current status - dp[i] |= dp[i - *e as usize]; + dp[i] |= dp[i - (*e as usize)]; } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs index aea6f4c7d9cc5..498f8f59c63ab 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs @@ -20,14 +20,14 @@ impl Solution { // Begin the actual dp process for i in 1..=n { for j in 0..=m { - dp[i][j] = if nums[i - 1] as usize > j { + dp[i][j] = if (nums[i - 1] as usize) > j { dp[i - 1][j] } else { - dp[i - 1][j] || dp[i - 1][j - nums[i - 1] as usize] - } + dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] + }; } } dp[n][m] } -} \ No newline at end of file +} diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md index 1d3a6b1414b63..b467ea592a31e 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md @@ -260,7 +260,7 @@ impl Trie { fn insert(&mut self, x: i32) { let mut node = self; for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; + let v = ((x >> i) & 1) as usize; if node.children[v].is_none() { node.children[v] = Some(Box::new(Trie::new())); } @@ -272,7 +272,7 @@ impl Trie { let mut node = self; let mut ans = 0; for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; + let v = ((x >> i) & 1) as usize; if let Some(child) = &node.children[v ^ 1] { ans |= 1 << i; node = child.as_ref(); diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md index 759856904f674..a0cc9e7459cde 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md @@ -237,7 +237,7 @@ impl Trie { fn insert(&mut self, x: i32) { let mut node = self; for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; + let v = ((x >> i) & 1) as usize; if node.children[v].is_none() { node.children[v] = Some(Box::new(Trie::new())); } @@ -249,7 +249,7 @@ impl Trie { let mut node = self; let mut ans = 0; for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; + let v = ((x >> i) & 1) as usize; if let Some(child) = &node.children[v ^ 1] { ans |= 1 << i; node = child.as_ref(); diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs index 6e48796b5c8fe..a48fbd230f6f5 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs @@ -1,49 +1,49 @@ -struct Trie { - children: [Option>; 2], -} - -impl Trie { - fn new() -> Trie { - Trie { - children: [None, None], - } - } - - fn insert(&mut self, x: i32) { - let mut node = self; - for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; - if node.children[v].is_none() { - node.children[v] = Some(Box::new(Trie::new())); - } - node = node.children[v].as_mut().unwrap(); - } - } - - fn search(&self, x: i32) -> i32 { - let mut node = self; - let mut ans = 0; - for i in (0..=30).rev() { - let v = (x >> i & 1) as usize; - if let Some(child) = &node.children[v ^ 1] { - ans |= 1 << i; - node = child.as_ref(); - } else { - node = node.children[v].as_ref().unwrap(); - } - } - ans - } -} - -impl Solution { - pub fn find_maximum_xor(nums: Vec) -> i32 { - let mut trie = Trie::new(); - let mut ans = 0; - for &x in nums.iter() { - trie.insert(x); - ans = ans.max(trie.search(x)); - } - ans - } -} \ No newline at end of file +struct Trie { + children: [Option>; 2], +} + +impl Trie { + fn new() -> Trie { + Trie { + children: [None, None], + } + } + + fn insert(&mut self, x: i32) { + let mut node = self; + for i in (0..=30).rev() { + let v = ((x >> i) & 1) as usize; + if node.children[v].is_none() { + node.children[v] = Some(Box::new(Trie::new())); + } + node = node.children[v].as_mut().unwrap(); + } + } + + fn search(&self, x: i32) -> i32 { + let mut node = self; + let mut ans = 0; + for i in (0..=30).rev() { + let v = ((x >> i) & 1) as usize; + if let Some(child) = &node.children[v ^ 1] { + ans |= 1 << i; + node = child.as_ref(); + } else { + node = node.children[v].as_ref().unwrap(); + } + } + ans + } +} + +impl Solution { + pub fn find_maximum_xor(nums: Vec) -> i32 { + let mut trie = Trie::new(); + let mut ans = 0; + for &x in nums.iter() { + trie.insert(x); + ans = ans.max(trie.search(x)); + } + ans + } +} diff --git a/solution/0400-0499/0438.Find All Anagrams in a String/Solution.rs b/solution/0400-0499/0438.Find All Anagrams in a String/Solution.rs index 4b889ed1db195..ab401c655c48a 100644 --- a/solution/0400-0499/0438.Find All Anagrams in a String/Solution.rs +++ b/solution/0400-0499/0438.Find All Anagrams in a String/Solution.rs @@ -1,27 +1,27 @@ -impl Solution { - pub fn find_anagrams(s: String, p: String) -> Vec { - let (s, p) = (s.as_bytes(), p.as_bytes()); - let (m, n) = (s.len(), p.len()); - let mut ans = vec![]; - if m < n { - return ans; - } - - let mut cnt = [0; 26]; - for i in 0..n { - cnt[(p[i] - b'a') as usize] += 1; - cnt[(s[i] - b'a') as usize] -= 1; - } - for i in n..m { - if cnt.iter().all(|&v| v == 0) { - ans.push((i - n) as i32); - } - cnt[(s[i] - b'a') as usize] -= 1; - cnt[(s[i - n] - b'a') as usize] += 1; - } - if cnt.iter().all(|&v| v == 0) { - ans.push((m - n) as i32); - } - ans - } -} +impl Solution { + pub fn find_anagrams(s: String, p: String) -> Vec { + let (s, p) = (s.as_bytes(), p.as_bytes()); + let (m, n) = (s.len(), p.len()); + let mut ans = vec![]; + if m < n { + return ans; + } + + let mut cnt = [0; 26]; + for i in 0..n { + cnt[(p[i] - b'a') as usize] += 1; + cnt[(s[i] - b'a') as usize] -= 1; + } + for i in n..m { + if cnt.iter().all(|&v| v == 0) { + ans.push((i - n) as i32); + } + cnt[(s[i] - b'a') as usize] -= 1; + cnt[(s[i - n] - b'a') as usize] += 1; + } + if cnt.iter().all(|&v| v == 0) { + ans.push((m - n) as i32); + } + ans + } +} diff --git a/solution/0400-0499/0443.String Compression/Solution.rs b/solution/0400-0499/0443.String Compression/Solution.rs index c8f4a8ca52fb1..9c8846a2e8776 100644 --- a/solution/0400-0499/0443.String Compression/Solution.rs +++ b/solution/0400-0499/0443.String Compression/Solution.rs @@ -1,23 +1,23 @@ -impl Solution { - pub fn compress(chars: &mut Vec) -> i32 { - let (mut i, mut k, n) = (0, 0, chars.len()); - while i < n { - let mut j = i + 1; - while j < n && chars[j] == chars[i] { - j += 1; - } - chars[k] = chars[i]; - k += 1; - - if j - i > 1 { - let cnt = (j - i).to_string(); - for c in cnt.chars() { - chars[k] = c; - k += 1; - } - } - i = j; - } - k as i32 - } -} +impl Solution { + pub fn compress(chars: &mut Vec) -> i32 { + let (mut i, mut k, n) = (0, 0, chars.len()); + while i < n { + let mut j = i + 1; + while j < n && chars[j] == chars[i] { + j += 1; + } + chars[k] = chars[i]; + k += 1; + + if j - i > 1 { + let cnt = (j - i).to_string(); + for c in cnt.chars() { + chars[k] = c; + k += 1; + } + } + i = j; + } + k as i32 + } +} diff --git a/solution/0400-0499/0445.Add Two Numbers II/README.md b/solution/0400-0499/0445.Add Two Numbers II/README.md index b0382b4d19966..3bc5301b9427b 100644 --- a/solution/0400-0499/0445.Add Two Numbers II/README.md +++ b/solution/0400-0499/0445.Add Two Numbers II/README.md @@ -292,7 +292,7 @@ impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { l1 = Self::reverse(l1); l2 = Self::reverse(l2); @@ -346,7 +346,7 @@ impl Solution { pub fn add_two_numbers( l1: Option>, - l2: Option>, + l2: Option> ) -> Option> { let mut s1 = Self::create_stack(l1); let mut s2 = Self::create_stack(l2); @@ -360,10 +360,12 @@ impl Solution { if let Some(val) = s2.pop() { carry += val; } - dummy.next = Some(Box::new(ListNode { - val: carry % 10, - next: dummy.next.take(), - })); + dummy.next = Some( + Box::new(ListNode { + val: carry % 10, + next: dummy.next.take(), + }) + ); carry /= 10; } dummy.next.take() diff --git a/solution/0400-0499/0445.Add Two Numbers II/README_EN.md b/solution/0400-0499/0445.Add Two Numbers II/README_EN.md index 9825c9a836af4..dbb30eb033ef1 100644 --- a/solution/0400-0499/0445.Add Two Numbers II/README_EN.md +++ b/solution/0400-0499/0445.Add Two Numbers II/README_EN.md @@ -265,7 +265,7 @@ impl Solution { pub fn add_two_numbers( mut l1: Option>, - mut l2: Option>, + mut l2: Option> ) -> Option> { l1 = Self::reverse(l1); l2 = Self::reverse(l2); @@ -319,7 +319,7 @@ impl Solution { pub fn add_two_numbers( l1: Option>, - l2: Option>, + l2: Option> ) -> Option> { let mut s1 = Self::create_stack(l1); let mut s2 = Self::create_stack(l2); @@ -333,10 +333,12 @@ impl Solution { if let Some(val) = s2.pop() { carry += val; } - dummy.next = Some(Box::new(ListNode { - val: carry % 10, - next: dummy.next.take(), - })); + dummy.next = Some( + Box::new(ListNode { + val: carry % 10, + next: dummy.next.take(), + }) + ); carry /= 10; } dummy.next.take() diff --git a/solution/0400-0499/0445.Add Two Numbers II/Solution.rs b/solution/0400-0499/0445.Add Two Numbers II/Solution.rs index f3994f0cf7da0..836b4f22bd12a 100644 --- a/solution/0400-0499/0445.Add Two Numbers II/Solution.rs +++ b/solution/0400-0499/0445.Add Two Numbers II/Solution.rs @@ -26,7 +26,7 @@ impl Solution { pub fn add_two_numbers( l1: Option>, - l2: Option>, + l2: Option> ) -> Option> { let mut s1 = Self::create_stack(l1); let mut s2 = Self::create_stack(l2); @@ -40,12 +40,14 @@ impl Solution { if let Some(val) = s2.pop() { carry += val; } - dummy.next = Some(Box::new(ListNode { - val: carry % 10, - next: dummy.next.take(), - })); + dummy.next = Some( + Box::new(ListNode { + val: carry % 10, + next: dummy.next.take(), + }) + ); carry /= 10; } dummy.next.take() } -} \ No newline at end of file +} diff --git a/solution/0400-0499/0450.Delete Node in a BST/README.md b/solution/0400-0499/0450.Delete Node in a BST/README.md index 568f31b6952a3..86ff1000debbc 100644 --- a/solution/0400-0499/0450.Delete Node in a BST/README.md +++ b/solution/0400-0499/0450.Delete Node in a BST/README.md @@ -329,7 +329,7 @@ impl Solution { pub fn delete_node( mut root: Option>>, - key: i32, + key: i32 ) -> Option>> { if root.is_some() { let mut node = root.as_mut().unwrap().borrow_mut(); @@ -342,9 +342,15 @@ impl Solution { } std::cmp::Ordering::Equal => { match (node.left.is_some(), node.right.is_some()) { - (false, false) => return None, - (true, false) => return node.left.take(), - (false, true) => return node.right.take(), + (false, false) => { + return None; + } + (true, false) => { + return node.left.take(); + } + (false, true) => { + return node.right.take(); + } (true, true) => { if node.right.as_ref().unwrap().borrow().left.is_none() { let mut r = node.right.take(); diff --git a/solution/0400-0499/0450.Delete Node in a BST/README_EN.md b/solution/0400-0499/0450.Delete Node in a BST/README_EN.md index c5b5814bc8d93..73e9b1b47958b 100644 --- a/solution/0400-0499/0450.Delete Node in a BST/README_EN.md +++ b/solution/0400-0499/0450.Delete Node in a BST/README_EN.md @@ -297,7 +297,7 @@ impl Solution { pub fn delete_node( mut root: Option>>, - key: i32, + key: i32 ) -> Option>> { if root.is_some() { let mut node = root.as_mut().unwrap().borrow_mut(); @@ -310,9 +310,15 @@ impl Solution { } std::cmp::Ordering::Equal => { match (node.left.is_some(), node.right.is_some()) { - (false, false) => return None, - (true, false) => return node.left.take(), - (false, true) => return node.right.take(), + (false, false) => { + return None; + } + (true, false) => { + return node.left.take(); + } + (false, true) => { + return node.right.take(); + } (true, true) => { if node.right.as_ref().unwrap().borrow().left.is_none() { let mut r = node.right.take(); diff --git a/solution/0400-0499/0450.Delete Node in a BST/Solution.rs b/solution/0400-0499/0450.Delete Node in a BST/Solution.rs index 412f6ae2a4707..f745a01e504c1 100644 --- a/solution/0400-0499/0450.Delete Node in a BST/Solution.rs +++ b/solution/0400-0499/0450.Delete Node in a BST/Solution.rs @@ -29,7 +29,7 @@ impl Solution { pub fn delete_node( mut root: Option>>, - key: i32, + key: i32 ) -> Option>> { if root.is_some() { let mut node = root.as_mut().unwrap().borrow_mut(); @@ -42,9 +42,15 @@ impl Solution { } std::cmp::Ordering::Equal => { match (node.left.is_some(), node.right.is_some()) { - (false, false) => return None, - (true, false) => return node.left.take(), - (false, true) => return node.right.take(), + (false, false) => { + return None; + } + (true, false) => { + return node.left.take(); + } + (false, true) => { + return node.right.take(); + } (true, true) => { if node.right.as_ref().unwrap().borrow().left.is_none() { let mut r = node.right.take(); diff --git a/solution/0400-0499/0456.132 Pattern/README.md b/solution/0400-0499/0456.132 Pattern/README.md index 6b5c9e42863de..18537a4c85e25 100644 --- a/solution/0400-0499/0456.132 Pattern/README.md +++ b/solution/0400-0499/0456.132 Pattern/README.md @@ -472,7 +472,7 @@ impl Solution { while !stk.is_empty() && stk.last().unwrap() < &nums[i] { vk = stk.pop().unwrap(); } - stk.push(nums[i]) + stk.push(nums[i]); } false } diff --git a/solution/0400-0499/0456.132 Pattern/README_EN.md b/solution/0400-0499/0456.132 Pattern/README_EN.md index 6aaa3284ce37b..7bc025a837410 100644 --- a/solution/0400-0499/0456.132 Pattern/README_EN.md +++ b/solution/0400-0499/0456.132 Pattern/README_EN.md @@ -442,7 +442,7 @@ impl Solution { while !stk.is_empty() && stk.last().unwrap() < &nums[i] { vk = stk.pop().unwrap(); } - stk.push(nums[i]) + stk.push(nums[i]); } false } diff --git a/solution/0400-0499/0456.132 Pattern/Solution.rs b/solution/0400-0499/0456.132 Pattern/Solution.rs index 96e28e601a939..3bd63c0ced69f 100644 --- a/solution/0400-0499/0456.132 Pattern/Solution.rs +++ b/solution/0400-0499/0456.132 Pattern/Solution.rs @@ -10,7 +10,7 @@ impl Solution { while !stk.is_empty() && stk.last().unwrap() < &nums[i] { vk = stk.pop().unwrap(); } - stk.push(nums[i]) + stk.push(nums[i]); } false } diff --git a/solution/0400-0499/0460.LFU Cache/README.md b/solution/0400-0499/0460.LFU Cache/README.md index df7eef25eadce..43d620a35d479 100644 --- a/solution/0400-0499/0460.LFU Cache/README.md +++ b/solution/0400-0499/0460.LFU Cache/README.md @@ -570,7 +570,6 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; - struct Node { key: i32, value: i32, @@ -579,7 +578,6 @@ struct Node { next: Option>>, } - impl Node { fn new(key: i32, value: i32) -> Self { Self { @@ -592,13 +590,11 @@ impl Node { } } - struct LinkedList { head: Option>>, tail: Option>>, } - impl LinkedList { fn new() -> Self { Self { @@ -607,7 +603,6 @@ impl LinkedList { } } - fn push_front(&mut self, node: &Rc>) { match self.head.take() { Some(head) => { @@ -625,7 +620,6 @@ impl LinkedList { }; } - fn remove(&mut self, node: &Rc>) { match (node.borrow().prev.as_ref(), node.borrow().next.as_ref()) { (None, None) => { @@ -647,7 +641,6 @@ impl LinkedList { }; } - fn pop_back(&mut self) -> Option>> { match self.tail.take() { Some(tail) => { @@ -658,13 +651,11 @@ impl LinkedList { } } - fn is_empty(&self) -> bool { self.head.is_none() } } - struct LFUCache { cache: HashMap>>, freq_map: HashMap, @@ -672,7 +663,6 @@ struct LFUCache { capacity: usize, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -687,13 +677,11 @@ impl LFUCache { } } - fn get(&mut self, key: i32) -> i32 { if self.capacity == 0 { return -1; } - match self.cache.get(&key) { Some(node) => { let node = Rc::clone(node); @@ -705,13 +693,11 @@ impl LFUCache { } } - fn put(&mut self, key: i32, value: i32) { if self.capacity == 0 { return; } - match self.cache.get(&key) { Some(node) => { let node = Rc::clone(node); @@ -731,7 +717,6 @@ impl LFUCache { }; } - fn incr_freq(&mut self, node: &Rc>) { let freq = node.borrow().freq; let list = self.freq_map.get_mut(&freq).unwrap(); @@ -746,7 +731,6 @@ impl LFUCache { self.add_node(node); } - fn add_node(&mut self, node: &Rc>) { let freq = node.borrow().freq; match self.freq_map.get_mut(&freq) { @@ -760,10 +744,7 @@ impl LFUCache { } }; } -} - - -/** +}/** * Your LFUCache object will be instantiated and called as such: * let obj = LFUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0400-0499/0460.LFU Cache/README_EN.md b/solution/0400-0499/0460.LFU Cache/README_EN.md index cbd00471142c8..5fef344b8ba54 100644 --- a/solution/0400-0499/0460.LFU Cache/README_EN.md +++ b/solution/0400-0499/0460.LFU Cache/README_EN.md @@ -541,7 +541,6 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; - struct Node { key: i32, value: i32, @@ -550,7 +549,6 @@ struct Node { next: Option>>, } - impl Node { fn new(key: i32, value: i32) -> Self { Self { @@ -563,13 +561,11 @@ impl Node { } } - struct LinkedList { head: Option>>, tail: Option>>, } - impl LinkedList { fn new() -> Self { Self { @@ -578,7 +574,6 @@ impl LinkedList { } } - fn push_front(&mut self, node: &Rc>) { match self.head.take() { Some(head) => { @@ -596,7 +591,6 @@ impl LinkedList { }; } - fn remove(&mut self, node: &Rc>) { match (node.borrow().prev.as_ref(), node.borrow().next.as_ref()) { (None, None) => { @@ -618,7 +612,6 @@ impl LinkedList { }; } - fn pop_back(&mut self) -> Option>> { match self.tail.take() { Some(tail) => { @@ -629,13 +622,11 @@ impl LinkedList { } } - fn is_empty(&self) -> bool { self.head.is_none() } } - struct LFUCache { cache: HashMap>>, freq_map: HashMap, @@ -643,7 +634,6 @@ struct LFUCache { capacity: usize, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -658,13 +648,11 @@ impl LFUCache { } } - fn get(&mut self, key: i32) -> i32 { if self.capacity == 0 { return -1; } - match self.cache.get(&key) { Some(node) => { let node = Rc::clone(node); @@ -676,13 +664,11 @@ impl LFUCache { } } - fn put(&mut self, key: i32, value: i32) { if self.capacity == 0 { return; } - match self.cache.get(&key) { Some(node) => { let node = Rc::clone(node); @@ -702,7 +688,6 @@ impl LFUCache { }; } - fn incr_freq(&mut self, node: &Rc>) { let freq = node.borrow().freq; let list = self.freq_map.get_mut(&freq).unwrap(); @@ -717,7 +702,6 @@ impl LFUCache { self.add_node(node); } - fn add_node(&mut self, node: &Rc>) { let freq = node.borrow().freq; match self.freq_map.get_mut(&freq) { @@ -731,10 +715,7 @@ impl LFUCache { } }; } -} - - -/** +}/** * Your LFUCache object will be instantiated and called as such: * let obj = LFUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0400-0499/0460.LFU Cache/Solution.rs b/solution/0400-0499/0460.LFU Cache/Solution.rs index 200eaec443a52..e3b2f8f44dfb0 100644 --- a/solution/0400-0499/0460.LFU Cache/Solution.rs +++ b/solution/0400-0499/0460.LFU Cache/Solution.rs @@ -176,9 +176,7 @@ impl LFUCache { } }; } -} - -/** +}/** * Your LFUCache object will be instantiated and called as such: * let obj = LFUCache::new(capacity); * let ret_1: i32 = obj.get(key); diff --git a/solution/0400-0499/0468.Validate IP Address/README.md b/solution/0400-0499/0468.Validate IP Address/README.md index b7ac66790bc7d..c2ebcbd2ad29c 100644 --- a/solution/0400-0499/0468.Validate IP Address/README.md +++ b/solution/0400-0499/0468.Validate IP Address/README.md @@ -159,7 +159,9 @@ impl Solution { } for s in ss { match s.parse::() { - Err(_) => return false, + Err(_) => { + return false; + } Ok(num) => { if num < 0 || num > 255 || num.to_string() != s.to_string() { return false; @@ -170,7 +172,6 @@ impl Solution { true } - fn is_IPv6(s: &String) -> bool { let ss = s.split(':').collect::>(); if ss.len() != 8 { @@ -181,7 +182,11 @@ impl Solution { return false; } for &c in s.as_bytes() { - if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' { + if + (c >= b'0' && c <= b'9') || + (c >= b'a' && c <= b'f') || + (c >= b'A' && c <= b'F') + { continue; } return false; @@ -190,7 +195,6 @@ impl Solution { true } - pub fn valid_ip_address(query_ip: String) -> String { if Self::is_IPv4(&query_ip) { return String::from("IPv4"); diff --git a/solution/0400-0499/0468.Validate IP Address/README_EN.md b/solution/0400-0499/0468.Validate IP Address/README_EN.md index d3e199ec15f95..89b91e1f10d6d 100644 --- a/solution/0400-0499/0468.Validate IP Address/README_EN.md +++ b/solution/0400-0499/0468.Validate IP Address/README_EN.md @@ -149,7 +149,9 @@ impl Solution { } for s in ss { match s.parse::() { - Err(_) => return false, + Err(_) => { + return false; + } Ok(num) => { if num < 0 || num > 255 || num.to_string() != s.to_string() { return false; @@ -160,7 +162,6 @@ impl Solution { true } - fn is_IPv6(s: &String) -> bool { let ss = s.split(':').collect::>(); if ss.len() != 8 { @@ -171,7 +172,11 @@ impl Solution { return false; } for &c in s.as_bytes() { - if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' { + if + (c >= b'0' && c <= b'9') || + (c >= b'a' && c <= b'f') || + (c >= b'A' && c <= b'F') + { continue; } return false; @@ -180,7 +185,6 @@ impl Solution { true } - pub fn valid_ip_address(query_ip: String) -> String { if Self::is_IPv4(&query_ip) { return String::from("IPv4"); diff --git a/solution/0400-0499/0468.Validate IP Address/Solution.rs b/solution/0400-0499/0468.Validate IP Address/Solution.rs index 2327672e8bb24..aac137f99e74e 100644 --- a/solution/0400-0499/0468.Validate IP Address/Solution.rs +++ b/solution/0400-0499/0468.Validate IP Address/Solution.rs @@ -6,7 +6,9 @@ impl Solution { } for s in ss { match s.parse::() { - Err(_) => return false, + Err(_) => { + return false; + } Ok(num) => { if num < 0 || num > 255 || num.to_string() != s.to_string() { return false; @@ -27,7 +29,11 @@ impl Solution { return false; } for &c in s.as_bytes() { - if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' { + if + (c >= b'0' && c <= b'9') || + (c >= b'a' && c <= b'f') || + (c >= b'A' && c <= b'F') + { continue; } return false; diff --git a/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.rs b/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.rs index fe7f06d309cce..7361ab205d885 100644 --- a/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.rs +++ b/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.rs @@ -1,10 +1,10 @@ -/** +/** * The rand7() API is already defined for you. * @return a random integer in the range 1 to 7 * fn rand7() -> i32; */ - impl Solution { +impl Solution { pub fn rand10() -> i32 { loop { let i = rand7() - 1; diff --git a/solution/0400-0499/0481.Magical String/README.md b/solution/0400-0499/0481.Magical String/README.md index af22cf99abd28..6a17dd58f483d 100644 --- a/solution/0400-0499/0481.Magical String/README.md +++ b/solution/0400-0499/0481.Magical String/README.md @@ -199,7 +199,11 @@ impl Solution { } i += 1; } - s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32 + s + .as_bytes() + [0..n].iter() + .filter(|&v| v == &b'1') + .count() as i32 } } ``` diff --git a/solution/0400-0499/0481.Magical String/README_EN.md b/solution/0400-0499/0481.Magical String/README_EN.md index 7fb725a30e56f..b51d82da1c9a9 100644 --- a/solution/0400-0499/0481.Magical String/README_EN.md +++ b/solution/0400-0499/0481.Magical String/README_EN.md @@ -154,7 +154,11 @@ impl Solution { } i += 1; } - s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32 + s + .as_bytes() + [0..n].iter() + .filter(|&v| v == &b'1') + .count() as i32 } } ``` diff --git a/solution/0400-0499/0481.Magical String/Solution.rs b/solution/0400-0499/0481.Magical String/Solution.rs index e9b0b628cb9c5..b728e4f643050 100644 --- a/solution/0400-0499/0481.Magical String/Solution.rs +++ b/solution/0400-0499/0481.Magical String/Solution.rs @@ -11,6 +11,10 @@ impl Solution { } i += 1; } - s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32 + s + .as_bytes() + [0..n].iter() + .filter(|&v| v == &b'1') + .count() as i32 } } diff --git a/solution/0400-0499/0486.Predict the Winner/README.md b/solution/0400-0499/0486.Predict the Winner/README.md index 918bf202bdaca..2824a7c56c90b 100644 --- a/solution/0400-0499/0486.Predict the Winner/README.md +++ b/solution/0400-0499/0486.Predict the Winner/README.md @@ -217,12 +217,12 @@ impl Solution { // Begin the dp process for i in (0..n - 1).rev() { - for j in (i + 1)..n { + for j in i + 1..n { dp[i][j] = std::cmp::max( // Take i-th num nums[i] - dp[i + 1][j], // Take j-th num - nums[j] - dp[i][j - 1], + nums[j] - dp[i][j - 1] ); } } diff --git a/solution/0400-0499/0486.Predict the Winner/README_EN.md b/solution/0400-0499/0486.Predict the Winner/README_EN.md index 8c2d5253a8e58..2b466ec6766d2 100644 --- a/solution/0400-0499/0486.Predict the Winner/README_EN.md +++ b/solution/0400-0499/0486.Predict the Winner/README_EN.md @@ -173,12 +173,12 @@ impl Solution { // Begin the dp process for i in (0..n - 1).rev() { - for j in (i + 1)..n { + for j in i + 1..n { dp[i][j] = std::cmp::max( // Take i-th num nums[i] - dp[i + 1][j], // Take j-th num - nums[j] - dp[i][j - 1], + nums[j] - dp[i][j - 1] ); } } diff --git a/solution/0400-0499/0486.Predict the Winner/Solution.rs b/solution/0400-0499/0486.Predict the Winner/Solution.rs index 2f10af2708d11..f82e1e740ebd9 100644 --- a/solution/0400-0499/0486.Predict the Winner/Solution.rs +++ b/solution/0400-0499/0486.Predict the Winner/Solution.rs @@ -11,16 +11,16 @@ impl Solution { // Begin the dp process for i in (0..n - 1).rev() { - for j in (i + 1)..n { + for j in i + 1..n { dp[i][j] = std::cmp::max( // Take i-th num nums[i] - dp[i + 1][j], // Take j-th num - nums[j] - dp[i][j - 1], + nums[j] - dp[i][j - 1] ); } } dp[0][n - 1] >= 0 } -} \ No newline at end of file +} diff --git a/solution/0400-0499/0494.Target Sum/README.md b/solution/0400-0499/0494.Target Sum/README.md index 659d0f3055a40..b996c3cb4ac33 100644 --- a/solution/0400-0499/0494.Target Sum/README.md +++ b/solution/0400-0499/0494.Target Sum/README.md @@ -240,9 +240,9 @@ impl Solution { for j in 0..=m as usize { // nums[i - 1] is not included dp[i][j] = dp[i - 1][j]; - if nums[i - 1] <= j as i32 { + if nums[i - 1] <= (j as i32) { // nums[i - 1] is included - dp[i][j] += dp[i - 1][j - nums[i - 1] as usize]; + dp[i][j] += dp[i - 1][j - (nums[i - 1] as usize)]; } } } @@ -275,7 +275,7 @@ impl Solution { // Begin the actual dp phase for e in &nums { for i in (*e as usize..=n).rev() { - dp[i] += dp[i - *e as usize]; + dp[i] += dp[i - (*e as usize)]; } } diff --git a/solution/0400-0499/0494.Target Sum/README_EN.md b/solution/0400-0499/0494.Target Sum/README_EN.md index 035b49c60a2f7..2456d64c8ce61 100644 --- a/solution/0400-0499/0494.Target Sum/README_EN.md +++ b/solution/0400-0499/0494.Target Sum/README_EN.md @@ -224,9 +224,9 @@ impl Solution { for j in 0..=m as usize { // nums[i - 1] is not included dp[i][j] = dp[i - 1][j]; - if nums[i - 1] <= j as i32 { + if nums[i - 1] <= (j as i32) { // nums[i - 1] is included - dp[i][j] += dp[i - 1][j - nums[i - 1] as usize]; + dp[i][j] += dp[i - 1][j - (nums[i - 1] as usize)]; } } } @@ -259,7 +259,7 @@ impl Solution { // Begin the actual dp phase for e in &nums { for i in (*e as usize..=n).rev() { - dp[i] += dp[i - *e as usize]; + dp[i] += dp[i - (*e as usize)]; } } diff --git a/solution/0400-0499/0494.Target Sum/Solution.rs b/solution/0400-0499/0494.Target Sum/Solution.rs index d508f5be07f08..919c419e567f2 100644 --- a/solution/0400-0499/0494.Target Sum/Solution.rs +++ b/solution/0400-0499/0494.Target Sum/Solution.rs @@ -24,13 +24,13 @@ impl Solution { for j in 0..=m as usize { // nums[i - 1] is not included dp[i][j] = dp[i - 1][j]; - if nums[i - 1] <= j as i32 { + if nums[i - 1] <= (j as i32) { // nums[i - 1] is included - dp[i][j] += dp[i - 1][j - nums[i - 1] as usize]; + dp[i][j] += dp[i - 1][j - (nums[i - 1] as usize)]; } } } dp[n][m as usize] } -} \ No newline at end of file +} diff --git a/solution/0400-0499/0496.Next Greater Element I/README.md b/solution/0400-0499/0496.Next Greater Element I/README.md index 4b2a0f74f868e..c29eb0d0a6009 100644 --- a/solution/0400-0499/0496.Next Greater Element I/README.md +++ b/solution/0400-0499/0496.Next Greater Element I/README.md @@ -327,18 +327,21 @@ impl Solution { ```rust impl Solution { pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { - nums1.iter().map(|target| { - let mut res = -1; - for num in nums2.iter().rev() { - if num == target { - break; - } - if num > target { - res = *num; + nums1 + .iter() + .map(|target| { + let mut res = -1; + for num in nums2.iter().rev() { + if num == target { + break; + } + if num > target { + res = *num; + } } - } - res - }).collect::>() + res + }) + .collect::>() } } ``` diff --git a/solution/0400-0499/0496.Next Greater Element I/README_EN.md b/solution/0400-0499/0496.Next Greater Element I/README_EN.md index 57d9bae88d23a..5185bb5a92017 100644 --- a/solution/0400-0499/0496.Next Greater Element I/README_EN.md +++ b/solution/0400-0499/0496.Next Greater Element I/README_EN.md @@ -301,18 +301,21 @@ impl Solution { ```rust impl Solution { pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { - nums1.iter().map(|target| { - let mut res = -1; - for num in nums2.iter().rev() { - if num == target { - break; - } - if num > target { - res = *num; + nums1 + .iter() + .map(|target| { + let mut res = -1; + for num in nums2.iter().rev() { + if num == target { + break; + } + if num > target { + res = *num; + } } - } - res - }).collect::>() + res + }) + .collect::>() } } ``` diff --git a/solution/0500-0599/0508.Most Frequent Subtree Sum/README.md b/solution/0500-0599/0508.Most Frequent Subtree Sum/README.md index 4adaac116f7d9..cd37e6d73098b 100644 --- a/solution/0500-0599/0508.Most Frequent Subtree Sum/README.md +++ b/solution/0500-0599/0508.Most Frequent Subtree Sum/README.md @@ -272,7 +272,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - max: &mut i32, + max: &mut i32 ) -> i32 { if root.is_none() { return 0; diff --git a/solution/0500-0599/0508.Most Frequent Subtree Sum/README_EN.md b/solution/0500-0599/0508.Most Frequent Subtree Sum/README_EN.md index eca19b147d279..171a92c0047e3 100644 --- a/solution/0500-0599/0508.Most Frequent Subtree Sum/README_EN.md +++ b/solution/0500-0599/0508.Most Frequent Subtree Sum/README_EN.md @@ -256,7 +256,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - max: &mut i32, + max: &mut i32 ) -> i32 { if root.is_none() { return 0; diff --git a/solution/0500-0599/0508.Most Frequent Subtree Sum/Solution.rs b/solution/0500-0599/0508.Most Frequent Subtree Sum/Solution.rs index 37f4325ccb095..9c4b85e9332e4 100644 --- a/solution/0500-0599/0508.Most Frequent Subtree Sum/Solution.rs +++ b/solution/0500-0599/0508.Most Frequent Subtree Sum/Solution.rs @@ -23,7 +23,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - max: &mut i32, + max: &mut i32 ) -> i32 { if root.is_none() { return 0; diff --git a/solution/0500-0599/0509.Fibonacci Number/README.md b/solution/0500-0599/0509.Fibonacci Number/README.md index 9983c734e367e..7c9e545bc55cf 100644 --- a/solution/0500-0599/0509.Fibonacci Number/README.md +++ b/solution/0500-0599/0509.Fibonacci Number/README.md @@ -166,7 +166,7 @@ impl Solution { for _ in 0..n { let t = b; b = a + b; - a = t + a = t; } a } diff --git a/solution/0500-0599/0509.Fibonacci Number/README_EN.md b/solution/0500-0599/0509.Fibonacci Number/README_EN.md index d55b06d6cdc87..add12450d3c03 100644 --- a/solution/0500-0599/0509.Fibonacci Number/README_EN.md +++ b/solution/0500-0599/0509.Fibonacci Number/README_EN.md @@ -156,7 +156,7 @@ impl Solution { for _ in 0..n { let t = b; b = a + b; - a = t + a = t; } a } diff --git a/solution/0500-0599/0509.Fibonacci Number/Solution.rs b/solution/0500-0599/0509.Fibonacci Number/Solution.rs index 898aed33a273b..dec03ef79004b 100644 --- a/solution/0500-0599/0509.Fibonacci Number/Solution.rs +++ b/solution/0500-0599/0509.Fibonacci Number/Solution.rs @@ -5,7 +5,7 @@ impl Solution { for _ in 0..n { let t = b; b = a + b; - a = t + a = t; } a } diff --git a/solution/0500-0599/0513.Find Bottom Left Tree Value/README.md b/solution/0500-0599/0513.Find Bottom Left Tree Value/README.md index 62c65f9b9ec06..4c1b782aba420 100644 --- a/solution/0500-0599/0513.Find Bottom Left Tree Value/README.md +++ b/solution/0500-0599/0513.Find Bottom Left Tree Value/README.md @@ -466,7 +466,7 @@ impl Solution { pub fn find_bottom_left_value(root: Option>>) -> i32 { let mut max = 0; let mut res = 0; - Self::dfs(&root, 1, &mut max,&mut res); + Self::dfs(&root, 1, &mut max, &mut res); res } } diff --git a/solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md b/solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md index 082fb817fa8e0..8f59443bf30dc 100644 --- a/solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md +++ b/solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md @@ -442,7 +442,7 @@ impl Solution { pub fn find_bottom_left_value(root: Option>>) -> i32 { let mut max = 0; let mut res = 0; - Self::dfs(&root, 1, &mut max,&mut res); + Self::dfs(&root, 1, &mut max, &mut res); res } } diff --git a/solution/0500-0599/0528.Random Pick with Weight/README.md b/solution/0500-0599/0528.Random Pick with Weight/README.md index d3725423e7422..d2ff0042993f9 100644 --- a/solution/0500-0599/0528.Random Pick with Weight/README.md +++ b/solution/0500-0599/0528.Random Pick with Weight/README.md @@ -258,7 +258,7 @@ Solution.prototype.pickIndex = function () { ### **Rust** ```rust -use rand::{thread_rng, Rng}; +use rand::{ thread_rng, Rng }; struct Solution { sum: Vec, @@ -291,9 +291,7 @@ impl Solution { } (left - 1) as i32 } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(w); * let ret_1: i32 = obj.pick_index(); diff --git a/solution/0500-0599/0528.Random Pick with Weight/README_EN.md b/solution/0500-0599/0528.Random Pick with Weight/README_EN.md index 629cd201b8a51..22ebd5a16f90b 100644 --- a/solution/0500-0599/0528.Random Pick with Weight/README_EN.md +++ b/solution/0500-0599/0528.Random Pick with Weight/README_EN.md @@ -247,7 +247,7 @@ Solution.prototype.pickIndex = function () { ### **Rust** ```rust -use rand::{thread_rng, Rng}; +use rand::{ thread_rng, Rng }; struct Solution { sum: Vec, @@ -280,9 +280,7 @@ impl Solution { } (left - 1) as i32 } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(w); * let ret_1: i32 = obj.pick_index(); diff --git a/solution/0500-0599/0528.Random Pick with Weight/Solution.rs b/solution/0500-0599/0528.Random Pick with Weight/Solution.rs index 82918b9eb4448..5a6dcfa641990 100644 --- a/solution/0500-0599/0528.Random Pick with Weight/Solution.rs +++ b/solution/0500-0599/0528.Random Pick with Weight/Solution.rs @@ -1,4 +1,4 @@ -use rand::{thread_rng, Rng}; +use rand::{ thread_rng, Rng }; struct Solution { sum: Vec, @@ -31,9 +31,7 @@ impl Solution { } (left - 1) as i32 } -} - -/** +}/** * Your Solution object will be instantiated and called as such: * let obj = Solution::new(w); * let ret_1: i32 = obj.pick_index(); diff --git a/solution/0500-0599/0530.Minimum Absolute Difference in BST/Solution.rs b/solution/0500-0599/0530.Minimum Absolute Difference in BST/Solution.rs index cf47bfcad27bb..7b551f40c79ab 100644 --- a/solution/0500-0599/0530.Minimum Absolute Difference in BST/Solution.rs +++ b/solution/0500-0599/0530.Minimum Absolute Difference in BST/Solution.rs @@ -5,7 +5,7 @@ // pub left: Option>>, // pub right: Option>>, // } -// +// // impl TreeNode { // #[inline] // pub fn new(val: i32) -> Self { @@ -41,4 +41,4 @@ impl Solution { Self::traverse(right.clone(), prev, ans); } } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0540.Single Element in a Sorted Array/README.md b/solution/0500-0599/0540.Single Element in a Sorted Array/README.md index b857ca2e1f486..95ebb3c67d4fa 100644 --- a/solution/0500-0599/0540.Single Element in a Sorted Array/README.md +++ b/solution/0500-0599/0540.Single Element in a Sorted Array/README.md @@ -204,7 +204,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == nums[mid ^ 1] { l = mid + 1; } else { diff --git a/solution/0500-0599/0540.Single Element in a Sorted Array/README_EN.md b/solution/0500-0599/0540.Single Element in a Sorted Array/README_EN.md index 3d12a57b12833..7b48f732ad962 100644 --- a/solution/0500-0599/0540.Single Element in a Sorted Array/README_EN.md +++ b/solution/0500-0599/0540.Single Element in a Sorted Array/README_EN.md @@ -149,7 +149,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == nums[mid ^ 1] { l = mid + 1; } else { diff --git a/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.rs b/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.rs index 596753ad2a977..45cedbf2956d2 100644 --- a/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.rs +++ b/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.rs @@ -3,7 +3,7 @@ impl Solution { let mut l = 0; let mut r = nums.len() - 1; while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; if nums[mid] == nums[mid ^ 1] { l = mid + 1; } else { diff --git a/solution/0500-0599/0542.01 Matrix/README.md b/solution/0500-0599/0542.01 Matrix/README.md index b659c09744b68..3943b18297a26 100644 --- a/solution/0500-0599/0542.01 Matrix/README.md +++ b/solution/0500-0599/0542.01 Matrix/README.md @@ -185,9 +185,12 @@ impl Solution { let (x, y) = the_q.front().unwrap().clone(); the_q.pop_front(); for pair in &traverse_vec { - let cur_x = pair.0 + x as i32; - let cur_y = pair.1 + y as i32; - if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 { + let cur_x = pair.0 + (x as i32); + let cur_y = pair.1 + (y as i32); + if + Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && + ret_vec[cur_x as usize][cur_y as usize] == -1 + { // The current cell has not be updated yet, and is also in bound ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1; the_q.push_back((cur_x as usize, cur_y as usize)); diff --git a/solution/0500-0599/0542.01 Matrix/README_EN.md b/solution/0500-0599/0542.01 Matrix/README_EN.md index 5f28ce1916bad..a64d858de769e 100644 --- a/solution/0500-0599/0542.01 Matrix/README_EN.md +++ b/solution/0500-0599/0542.01 Matrix/README_EN.md @@ -177,9 +177,12 @@ impl Solution { let (x, y) = the_q.front().unwrap().clone(); the_q.pop_front(); for pair in &traverse_vec { - let cur_x = pair.0 + x as i32; - let cur_y = pair.1 + y as i32; - if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 { + let cur_x = pair.0 + (x as i32); + let cur_y = pair.1 + (y as i32); + if + Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && + ret_vec[cur_x as usize][cur_y as usize] == -1 + { // The current cell has not be updated yet, and is also in bound ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1; the_q.push_back((cur_x as usize, cur_y as usize)); diff --git a/solution/0500-0599/0542.01 Matrix/Solution.rs b/solution/0500-0599/0542.01 Matrix/Solution.rs index 82226cc1c23bb..1c90d9882f87a 100644 --- a/solution/0500-0599/0542.01 Matrix/Solution.rs +++ b/solution/0500-0599/0542.01 Matrix/Solution.rs @@ -26,9 +26,12 @@ impl Solution { let (x, y) = the_q.front().unwrap().clone(); the_q.pop_front(); for pair in &traverse_vec { - let cur_x = pair.0 + x as i32; - let cur_y = pair.1 + y as i32; - if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 { + let cur_x = pair.0 + (x as i32); + let cur_y = pair.1 + (y as i32); + if + Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && + ret_vec[cur_x as usize][cur_y as usize] == -1 + { // The current cell has not be updated yet, and is also in bound ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1; the_q.push_back((cur_x as usize, cur_y as usize)); @@ -43,4 +46,4 @@ impl Solution { pub fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { i >= 0 && i < n && j >= 0 && j < m } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0567.Permutation in String/README.md b/solution/0500-0599/0567.Permutation in String/README.md index d9a02f0151f64..989be39087096 100644 --- a/solution/0500-0599/0567.Permutation in String/README.md +++ b/solution/0500-0599/0567.Permutation in String/README.md @@ -391,7 +391,6 @@ function checkInclusion(s1: string, s2: string): boolean { ```rust use std::collections::HashMap; - impl Solution { // 测试两个哈希表是否匹配 fn is_match(m1: &HashMap, m2: &HashMap) -> bool { diff --git a/solution/0500-0599/0567.Permutation in String/README_EN.md b/solution/0500-0599/0567.Permutation in String/README_EN.md index bda50fba72aad..6a738cd9bd7c8 100644 --- a/solution/0500-0599/0567.Permutation in String/README_EN.md +++ b/solution/0500-0599/0567.Permutation in String/README_EN.md @@ -364,7 +364,6 @@ function checkInclusion(s1: string, s2: string): boolean { ```rust use std::collections::HashMap; - impl Solution { fn is_match(m1: &HashMap, m2: &HashMap) -> bool { for (k, v) in m1.iter() { diff --git a/solution/0500-0599/0572.Subtree of Another Tree/README.md b/solution/0500-0599/0572.Subtree of Another Tree/README.md index 407ce2c7b6594..55584eeaeb997 100644 --- a/solution/0500-0599/0572.Subtree of Another Tree/README.md +++ b/solution/0500-0599/0572.Subtree of Another Tree/README.md @@ -281,26 +281,26 @@ impl Solution { } let root = root.as_ref().unwrap().borrow(); let sub_root = sub_root.as_ref().unwrap().borrow(); - root.val == sub_root.val - && Self::dfs(&root.left, &sub_root.left) - && Self::dfs(&root.right, &sub_root.right) + root.val == sub_root.val && + Self::dfs(&root.left, &sub_root.left) && + Self::dfs(&root.right, &sub_root.right) } fn help( root: &Option>>, - sub_root: &Option>>, + sub_root: &Option>> ) -> bool { if root.is_none() { return false; } - Self::dfs(root, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().left, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().right, sub_root) + Self::dfs(root, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().left, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().right, sub_root) } pub fn is_subtree( root: Option>>, - sub_root: Option>>, + sub_root: Option>> ) -> bool { Self::help(&root, &sub_root) } diff --git a/solution/0500-0599/0572.Subtree of Another Tree/README_EN.md b/solution/0500-0599/0572.Subtree of Another Tree/README_EN.md index e733c1c15dcc3..4f63dac747f8d 100644 --- a/solution/0500-0599/0572.Subtree of Another Tree/README_EN.md +++ b/solution/0500-0599/0572.Subtree of Another Tree/README_EN.md @@ -267,26 +267,26 @@ impl Solution { } let root = root.as_ref().unwrap().borrow(); let sub_root = sub_root.as_ref().unwrap().borrow(); - root.val == sub_root.val - && Self::dfs(&root.left, &sub_root.left) - && Self::dfs(&root.right, &sub_root.right) + root.val == sub_root.val && + Self::dfs(&root.left, &sub_root.left) && + Self::dfs(&root.right, &sub_root.right) } fn help( root: &Option>>, - sub_root: &Option>>, + sub_root: &Option>> ) -> bool { if root.is_none() { return false; } - Self::dfs(root, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().left, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().right, sub_root) + Self::dfs(root, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().left, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().right, sub_root) } pub fn is_subtree( root: Option>>, - sub_root: Option>>, + sub_root: Option>> ) -> bool { Self::help(&root, &sub_root) } diff --git a/solution/0500-0599/0572.Subtree of Another Tree/Solution.rs b/solution/0500-0599/0572.Subtree of Another Tree/Solution.rs index 7299b06677497..60b1ea1c5b8dd 100644 --- a/solution/0500-0599/0572.Subtree of Another Tree/Solution.rs +++ b/solution/0500-0599/0572.Subtree of Another Tree/Solution.rs @@ -28,26 +28,26 @@ impl Solution { } let root = root.as_ref().unwrap().borrow(); let sub_root = sub_root.as_ref().unwrap().borrow(); - root.val == sub_root.val - && Self::dfs(&root.left, &sub_root.left) - && Self::dfs(&root.right, &sub_root.right) + root.val == sub_root.val && + Self::dfs(&root.left, &sub_root.left) && + Self::dfs(&root.right, &sub_root.right) } fn help( root: &Option>>, - sub_root: &Option>>, + sub_root: &Option>> ) -> bool { if root.is_none() { return false; } - Self::dfs(root, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().left, sub_root) - || Self::help(&root.as_ref().unwrap().borrow().right, sub_root) + Self::dfs(root, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().left, sub_root) || + Self::help(&root.as_ref().unwrap().borrow().right, sub_root) } pub fn is_subtree( root: Option>>, - sub_root: Option>>, + sub_root: Option>> ) -> bool { Self::help(&root, &sub_root) } diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md index 9fc4c23349880..818415d5c1940 100644 --- a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md @@ -268,7 +268,7 @@ impl Solution { arr.sort(); let mut l = 0; - let mut r = arr.len() as i32 - 1; + let mut r = (arr.len() as i32) - 1; while l <= r && nums[l as usize] == arr[l as usize] { l += 1; diff --git a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs index b1dff650ab5df..bc3aa08117ca2 100644 --- a/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs +++ b/solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/Solution.rs @@ -1,30 +1,30 @@ -impl Solution { - pub fn find_unsorted_subarray(nums: Vec) -> i32 { - let inf = 1 << 30; - let n = nums.len(); - let mut l = -1; - let mut r = -1; - let mut mi = inf; - let mut mx = -inf; - - for i in 0..n { - if mx > nums[i] { - r = i as i32; - } else { - mx = nums[i]; - } - - if mi < nums[n - i - 1] { - l = (n - i - 1) as i32; - } else { - mi = nums[n - i - 1]; - } - } - - if r == -1 { - 0 - } else { - r - l + 1 - } - } -} \ No newline at end of file +impl Solution { + pub fn find_unsorted_subarray(nums: Vec) -> i32 { + let inf = 1 << 30; + let n = nums.len(); + let mut l = -1; + let mut r = -1; + let mut mi = inf; + let mut mx = -inf; + + for i in 0..n { + if mx > nums[i] { + r = i as i32; + } else { + mx = nums[i]; + } + + if mi < nums[n - i - 1] { + l = (n - i - 1) as i32; + } else { + mi = nums[n - i - 1]; + } + } + + if r == -1 { + 0 + } else { + r - l + 1 + } + } +} diff --git a/solution/0500-0599/0582.Kill Process/Solution.rs b/solution/0500-0599/0582.Kill Process/Solution.rs index 808e1087ddaca..e1136de043e38 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.rs +++ b/solution/0500-0599/0582.Kill Process/Solution.rs @@ -1,25 +1,25 @@ -use std::collections::HashMap; - -impl Solution { - pub fn kill_process(pid: Vec, ppid: Vec, kill: i32) -> Vec { - let mut g: HashMap> = HashMap::new(); - let mut ans: Vec = Vec::new(); - - let n = pid.len(); - for i in 0..n { - g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]); - } - - Self::dfs(&mut ans, &g, kill); - ans - } - - fn dfs(ans: &mut Vec, g: &HashMap>, i: i32) { - ans.push(i); - if let Some(children) = g.get(&i) { - for &j in children { - Self::dfs(ans, g, j); - } - } - } -} \ No newline at end of file +use std::collections::HashMap; + +impl Solution { + pub fn kill_process(pid: Vec, ppid: Vec, kill: i32) -> Vec { + let mut g: HashMap> = HashMap::new(); + let mut ans: Vec = Vec::new(); + + let n = pid.len(); + for i in 0..n { + g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]); + } + + Self::dfs(&mut ans, &g, kill); + ans + } + + fn dfs(ans: &mut Vec, g: &HashMap>, i: i32) { + ans.push(i); + if let Some(children) = g.get(&i) { + for &j in children { + Self::dfs(ans, g, j); + } + } + } +} diff --git a/solution/0500-0599/0583.Delete Operation for Two Strings/README.md b/solution/0500-0599/0583.Delete Operation for Two Strings/README.md index b830d925636ef..e3f6562a0e0a4 100644 --- a/solution/0500-0599/0583.Delete Operation for Two Strings/README.md +++ b/solution/0500-0599/0583.Delete Operation for Two Strings/README.md @@ -186,11 +186,11 @@ impl Solution { dp[i - 1][j - 1] + 1 } else { dp[i - 1][j].max(dp[i][j - 1]) - } + }; } } let max = dp[m][n]; - ((m - max) + (n - max)) as i32 + (m - max + (n - max)) as i32 } } ``` diff --git a/solution/0500-0599/0583.Delete Operation for Two Strings/README_EN.md b/solution/0500-0599/0583.Delete Operation for Two Strings/README_EN.md index ba4fc087a3e12..4516be1eb325c 100644 --- a/solution/0500-0599/0583.Delete Operation for Two Strings/README_EN.md +++ b/solution/0500-0599/0583.Delete Operation for Two Strings/README_EN.md @@ -169,11 +169,11 @@ impl Solution { dp[i - 1][j - 1] + 1 } else { dp[i - 1][j].max(dp[i][j - 1]) - } + }; } } let max = dp[m][n]; - ((m - max) + (n - max)) as i32 + (m - max + (n - max)) as i32 } } ``` diff --git a/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.rs b/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.rs index 2f6081f0514c3..bab2b3ca87e4c 100644 --- a/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.rs +++ b/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.rs @@ -9,10 +9,10 @@ impl Solution { dp[i - 1][j - 1] + 1 } else { dp[i - 1][j].max(dp[i][j - 1]) - } + }; } } let max = dp[m][n]; - ((m - max) + (n - max)) as i32 + (m - max + (n - max)) as i32 } } diff --git a/solution/0500-0599/0591.Tag Validator/README.md b/solution/0500-0599/0591.Tag Validator/README.md index ae3a5e93b51ba..73ec94e140173 100644 --- a/solution/0500-0599/0591.Tag Validator/README.md +++ b/solution/0500-0599/0591.Tag Validator/README.md @@ -311,7 +311,12 @@ impl Solution { pub fn is_valid(code: String) -> bool { fn check(tag: &str) -> bool { let n = tag.len(); - n >= 1 && n <= 9 && tag.as_bytes().iter().all(|b| b.is_ascii_uppercase()) + n >= 1 && + n <= 9 && + tag + .as_bytes() + .iter() + .all(|b| b.is_ascii_uppercase()) } let mut stk = Vec::new(); @@ -322,8 +327,12 @@ impl Solution { } if code[i..].starts_with("") { - Some(n) => i += n + 11, - None => return false, + Some(n) => { + i += n + 11; + } + None => { + return false; + } }; } else if code[i..].starts_with(" return false, + None => { + return false; + } }; } else if code[i..].starts_with("<") { let j = i + 1; @@ -347,7 +358,9 @@ impl Solution { } stk.push(t); } - None => return false, + None => { + return false; + } }; } i += 1; diff --git a/solution/0500-0599/0591.Tag Validator/README_EN.md b/solution/0500-0599/0591.Tag Validator/README_EN.md index 2eba00a59714b..3163a8b8644b0 100644 --- a/solution/0500-0599/0591.Tag Validator/README_EN.md +++ b/solution/0500-0599/0591.Tag Validator/README_EN.md @@ -277,7 +277,12 @@ impl Solution { pub fn is_valid(code: String) -> bool { fn check(tag: &str) -> bool { let n = tag.len(); - n >= 1 && n <= 9 && tag.as_bytes().iter().all(|b| b.is_ascii_uppercase()) + n >= 1 && + n <= 9 && + tag + .as_bytes() + .iter() + .all(|b| b.is_ascii_uppercase()) } let mut stk = Vec::new(); @@ -288,8 +293,12 @@ impl Solution { } if code[i..].starts_with("") { - Some(n) => i += n + 11, - None => return false, + Some(n) => { + i += n + 11; + } + None => { + return false; + } }; } else if code[i..].starts_with(" return false, + None => { + return false; + } }; } else if code[i..].starts_with("<") { let j = i + 1; @@ -313,7 +324,9 @@ impl Solution { } stk.push(t); } - None => return false, + None => { + return false; + } }; } i += 1; diff --git a/solution/0500-0599/0591.Tag Validator/Solution.rs b/solution/0500-0599/0591.Tag Validator/Solution.rs index 289722a88ac0b..68b6e48525bb5 100644 --- a/solution/0500-0599/0591.Tag Validator/Solution.rs +++ b/solution/0500-0599/0591.Tag Validator/Solution.rs @@ -2,7 +2,12 @@ impl Solution { pub fn is_valid(code: String) -> bool { fn check(tag: &str) -> bool { let n = tag.len(); - n >= 1 && n <= 9 && tag.as_bytes().iter().all(|b| b.is_ascii_uppercase()) + n >= 1 && + n <= 9 && + tag + .as_bytes() + .iter() + .all(|b| b.is_ascii_uppercase()) } let mut stk = Vec::new(); @@ -13,8 +18,12 @@ impl Solution { } if code[i..].starts_with("") { - Some(n) => i += n + 11, - None => return false, + Some(n) => { + i += n + 11; + } + None => { + return false; + } }; } else if code[i..].starts_with(" return false, + None => { + return false; + } }; } else if code[i..].starts_with("<") { let j = i + 1; @@ -38,7 +49,9 @@ impl Solution { } stk.push(t); } - None => return false, + None => { + return false; + } }; } i += 1; diff --git a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README.md b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README.md index efcfc5c31f7b7..120f33c05e7eb 100644 --- a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README.md +++ b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README.md @@ -192,18 +192,21 @@ impl Solution { let map: HashMap = HashMap::from_iter(list1.into_iter().zip(0..)); let mut res = vec![]; let mut min_i = usize::MAX; - list2.into_iter().enumerate().for_each(|(i, key)| { - if map.contains_key(&key) { - let sum_i = map.get(&key).unwrap() + i; - if sum_i <= min_i { - if (sum_i < min_i) { - min_i = sum_i; - res.clear(); + list2 + .into_iter() + .enumerate() + .for_each(|(i, key)| { + if map.contains_key(&key) { + let sum_i = map.get(&key).unwrap() + i; + if sum_i <= min_i { + if sum_i < min_i { + min_i = sum_i; + res.clear(); + } + res.push(key); } - res.push(key); } - } - }); + }); res } } diff --git a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README_EN.md b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README_EN.md index 70dd0a86266ab..fb3f73b9f9f03 100644 --- a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README_EN.md +++ b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/README_EN.md @@ -193,18 +193,21 @@ impl Solution { let map: HashMap = HashMap::from_iter(list1.into_iter().zip(0..)); let mut res = vec![]; let mut min_i = usize::MAX; - list2.into_iter().enumerate().for_each(|(i, key)| { - if map.contains_key(&key) { - let sum_i = map.get(&key).unwrap() + i; - if sum_i <= min_i { - if (sum_i < min_i) { - min_i = sum_i; - res.clear(); + list2 + .into_iter() + .enumerate() + .for_each(|(i, key)| { + if map.contains_key(&key) { + let sum_i = map.get(&key).unwrap() + i; + if sum_i <= min_i { + if sum_i < min_i { + min_i = sum_i; + res.clear(); + } + res.push(key); } - res.push(key); } - } - }); + }); res } } diff --git a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.rs b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.rs index 6b662b7142ce7..334be333aefaf 100644 --- a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.rs +++ b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.rs @@ -6,18 +6,21 @@ impl Solution { let map: HashMap = HashMap::from_iter(list1.into_iter().zip(0..)); let mut res = vec![]; let mut min_i = usize::MAX; - list2.into_iter().enumerate().for_each(|(i, key)| { - if map.contains_key(&key) { - let sum_i = map.get(&key).unwrap() + i; - if sum_i <= min_i { - if (sum_i < min_i) { - min_i = sum_i; - res.clear(); + list2 + .into_iter() + .enumerate() + .for_each(|(i, key)| { + if map.contains_key(&key) { + let sum_i = map.get(&key).unwrap() + i; + if sum_i <= min_i { + if sum_i < min_i { + min_i = sum_i; + res.clear(); + } + res.push(key); } - res.push(key); } - } - }); + }); res } } diff --git a/solution/0600-0699/0605.Can Place Flowers/Solution.rs b/solution/0600-0699/0605.Can Place Flowers/Solution.rs index 80379b50d757e..7bce13cd2e7ec 100644 --- a/solution/0600-0699/0605.Can Place Flowers/Solution.rs +++ b/solution/0600-0699/0605.Can Place Flowers/Solution.rs @@ -1,16 +1,16 @@ -impl Solution { - pub fn can_place_flowers(flowerbed: Vec, n: i32) -> bool { - let (mut flowers, mut cnt) = (vec![0], 0); - flowers.append(&mut flowerbed.clone()); - flowers.push(0); - - for i in 1..flowers.len() - 1 { - let (l, r) = (flowers[i - 1], flowers[i + 1]); - if l + flowers[i] + r == 0 { - flowers[i] = 1; - cnt += 1; - } - } - cnt >= n - } -} +impl Solution { + pub fn can_place_flowers(flowerbed: Vec, n: i32) -> bool { + let (mut flowers, mut cnt) = (vec![0], 0); + flowers.append(&mut flowerbed.clone()); + flowers.push(0); + + for i in 1..flowers.len() - 1 { + let (l, r) = (flowers[i - 1], flowers[i + 1]); + if l + flowers[i] + r == 0 { + flowers[i] = 1; + cnt += 1; + } + } + cnt >= n + } +} diff --git a/solution/0600-0699/0617.Merge Two Binary Trees/README.md b/solution/0600-0699/0617.Merge Two Binary Trees/README.md index 9014e51e9c277..4338087371931 100644 --- a/solution/0600-0699/0617.Merge Two Binary Trees/README.md +++ b/solution/0600-0699/0617.Merge Two Binary Trees/README.md @@ -220,7 +220,7 @@ use std::cell::RefCell; impl Solution { pub fn merge_trees( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Option>> { match (root1.is_some(), root2.is_some()) { (false, false) => None, diff --git a/solution/0600-0699/0617.Merge Two Binary Trees/README_EN.md b/solution/0600-0699/0617.Merge Two Binary Trees/README_EN.md index 69e4c95f7f20f..4d8ea23f827b3 100644 --- a/solution/0600-0699/0617.Merge Two Binary Trees/README_EN.md +++ b/solution/0600-0699/0617.Merge Two Binary Trees/README_EN.md @@ -201,7 +201,7 @@ use std::cell::RefCell; impl Solution { pub fn merge_trees( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Option>> { match (root1.is_some(), root2.is_some()) { (false, false) => None, diff --git a/solution/0600-0699/0617.Merge Two Binary Trees/Solution.rs b/solution/0600-0699/0617.Merge Two Binary Trees/Solution.rs index 9b35abdc4500b..3e9db476b91d6 100644 --- a/solution/0600-0699/0617.Merge Two Binary Trees/Solution.rs +++ b/solution/0600-0699/0617.Merge Two Binary Trees/Solution.rs @@ -21,7 +21,7 @@ use std::cell::RefCell; impl Solution { pub fn merge_trees( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Option>> { match (root1.is_some(), root2.is_some()) { (false, false) => None, diff --git a/solution/0600-0699/0622.Design Circular Queue/README.md b/solution/0600-0699/0622.Design Circular Queue/README.md index 3075bf844bec1..41d5fed99acf2 100644 --- a/solution/0600-0699/0622.Design Circular Queue/README.md +++ b/solution/0600-0699/0622.Design Circular Queue/README.md @@ -443,8 +443,7 @@ impl MyCircularQueue { fn is_full(&self) -> bool { self.right - self.left == self.capacity } -} -/** +}/** * Your MyCircularQueue object will be instantiated and called as such: * let obj = MyCircularQueue::new(k); * let ret_1: bool = obj.en_queue(value); @@ -454,7 +453,6 @@ impl MyCircularQueue { * let ret_5: bool = obj.is_empty(); * let ret_6: bool = obj.is_full(); */ - */ ``` ### **...** diff --git a/solution/0600-0699/0622.Design Circular Queue/README_EN.md b/solution/0600-0699/0622.Design Circular Queue/README_EN.md index 38e0210d669e5..b5c2f2ff632d1 100644 --- a/solution/0600-0699/0622.Design Circular Queue/README_EN.md +++ b/solution/0600-0699/0622.Design Circular Queue/README_EN.md @@ -442,8 +442,7 @@ impl MyCircularQueue { fn is_full(&self) -> bool { self.right - self.left == self.capacity } -} -/** +}/** * Your MyCircularQueue object will be instantiated and called as such: * let obj = MyCircularQueue::new(k); * let ret_1: bool = obj.en_queue(value); @@ -453,7 +452,6 @@ impl MyCircularQueue { * let ret_5: bool = obj.is_empty(); * let ret_6: bool = obj.is_full(); */ - */ ``` ### **...** diff --git a/solution/0600-0699/0622.Design Circular Queue/Solution.rs b/solution/0600-0699/0622.Design Circular Queue/Solution.rs index aa6a6c9a7fdbf..1182ee831c768 100644 --- a/solution/0600-0699/0622.Design Circular Queue/Solution.rs +++ b/solution/0600-0699/0622.Design Circular Queue/Solution.rs @@ -58,8 +58,7 @@ impl MyCircularQueue { fn is_full(&self) -> bool { self.right - self.left == self.capacity } -} -/** +}/** * Your MyCircularQueue object will be instantiated and called as such: * let obj = MyCircularQueue::new(k); * let ret_1: bool = obj.en_queue(value); @@ -69,4 +68,3 @@ impl MyCircularQueue { * let ret_5: bool = obj.is_empty(); * let ret_6: bool = obj.is_full(); */ - */ \ No newline at end of file diff --git a/solution/0600-0699/0633.Sum of Square Numbers/README.md b/solution/0600-0699/0633.Sum of Square Numbers/README.md index d36db7483ebbf..0326b85456c9d 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/README.md +++ b/solution/0600-0699/0633.Sum of Square Numbers/README.md @@ -159,9 +159,15 @@ impl Solution { while left <= right { let num = left * left + right * right; match num.cmp(&c) { - Ordering::Less => left += 1, - Ordering::Greater => right -= 1, - Ordering::Equal => return true, + Ordering::Less => { + left += 1; + } + Ordering::Greater => { + right -= 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md b/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md index 9e80a1db7855a..51cde72d049f3 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md +++ b/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md @@ -149,9 +149,15 @@ impl Solution { while left <= right { let num = left * left + right * right; match num.cmp(&c) { - Ordering::Less => left += 1, - Ordering::Greater => right -= 1, - Ordering::Equal => return true, + Ordering::Less => { + left += 1; + } + Ordering::Greater => { + right -= 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs b/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs index 1fdf5f0433212..111c5313663f0 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs +++ b/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs @@ -7,9 +7,15 @@ impl Solution { while left <= right { let num = left * left + right * right; match num.cmp(&c) { - Ordering::Less => left += 1, - Ordering::Greater => right -= 1, - Ordering::Equal => return true, + Ordering::Less => { + left += 1; + } + Ordering::Greater => { + right -= 1; + } + Ordering::Equal => { + return true; + } } } false diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md b/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md index 3da29303a4aa8..37a4d0880359c 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md @@ -465,7 +465,7 @@ impl Solution { q.push_back(Rc::clone(node.borrow().right.as_ref().unwrap())); } } - ans.push(sum / n as f64); + ans.push(sum / (n as f64)); } ans } diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md b/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md index 124b1fd248e8e..41ec84557555a 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md @@ -447,7 +447,7 @@ impl Solution { q.push_back(Rc::clone(node.borrow().right.as_ref().unwrap())); } } - ans.push(sum / n as f64); + ans.push(sum / (n as f64)); } ans } diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.rs b/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.rs index d0d321f0ae436..511514fd613d5 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.rs +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.rs @@ -41,7 +41,7 @@ impl Solution { q.push_back(Rc::clone(node.borrow().right.as_ref().unwrap())); } } - ans.push(sum / n as f64); + ans.push(sum / (n as f64)); } ans } diff --git a/solution/0600-0699/0645.Set Mismatch/README.md b/solution/0600-0699/0645.Set Mismatch/README.md index accfae3864fdf..44716a4689027 100644 --- a/solution/0600-0699/0645.Set Mismatch/README.md +++ b/solution/0600-0699/0645.Set Mismatch/README.md @@ -333,7 +333,7 @@ use std::collections::HashSet; impl Solution { pub fn find_error_nums(nums: Vec) -> Vec { let n = nums.len() as i32; - let s1 = (1 + n) * n / 2; + let s1 = ((1 + n) * n) / 2; let s2 = nums.iter().cloned().collect::>().iter().sum::(); let s: i32 = nums.iter().sum(); vec![s - s2, s1 - s2] @@ -346,15 +346,15 @@ impl Solution { pub fn find_error_nums(nums: Vec) -> Vec { let mut xs = 0; for (i, x) in nums.iter().enumerate() { - xs ^= (i + 1) as i32 ^ x; + xs ^= ((i + 1) as i32) ^ x; } let mut a = 0; let lb = xs & -xs; for (i, x) in nums.iter().enumerate() { - if (i + 1) as i32 & lb != 0 { + if (((i + 1) as i32) & lb) != 0 { a ^= (i + 1) as i32; } - if *x & lb != 0 { + if (*x & lb) != 0 { a ^= *x; } } diff --git a/solution/0600-0699/0645.Set Mismatch/README_EN.md b/solution/0600-0699/0645.Set Mismatch/README_EN.md index 70950cf8bfc87..0e60dda1fa707 100644 --- a/solution/0600-0699/0645.Set Mismatch/README_EN.md +++ b/solution/0600-0699/0645.Set Mismatch/README_EN.md @@ -316,7 +316,7 @@ use std::collections::HashSet; impl Solution { pub fn find_error_nums(nums: Vec) -> Vec { let n = nums.len() as i32; - let s1 = (1 + n) * n / 2; + let s1 = ((1 + n) * n) / 2; let s2 = nums.iter().cloned().collect::>().iter().sum::(); let s: i32 = nums.iter().sum(); vec![s - s2, s1 - s2] @@ -329,15 +329,15 @@ impl Solution { pub fn find_error_nums(nums: Vec) -> Vec { let mut xs = 0; for (i, x) in nums.iter().enumerate() { - xs ^= (i + 1) as i32 ^ x; + xs ^= ((i + 1) as i32) ^ x; } let mut a = 0; let lb = xs & -xs; for (i, x) in nums.iter().enumerate() { - if (i + 1) as i32 & lb != 0 { + if (((i + 1) as i32) & lb) != 0 { a ^= (i + 1) as i32; } - if *x & lb != 0 { + if (*x & lb) != 0 { a ^= *x; } } diff --git a/solution/0600-0699/0645.Set Mismatch/Solution.rs b/solution/0600-0699/0645.Set Mismatch/Solution.rs index 4e8385d344274..88cd7e6875d3e 100644 --- a/solution/0600-0699/0645.Set Mismatch/Solution.rs +++ b/solution/0600-0699/0645.Set Mismatch/Solution.rs @@ -2,15 +2,15 @@ impl Solution { pub fn find_error_nums(nums: Vec) -> Vec { let mut xs = 0; for (i, x) in nums.iter().enumerate() { - xs ^= (i + 1) as i32 ^ x; + xs ^= ((i + 1) as i32) ^ x; } let mut a = 0; let lb = xs & -xs; for (i, x) in nums.iter().enumerate() { - if (i + 1) as i32 & lb != 0 { + if (((i + 1) as i32) & lb) != 0 { a ^= (i + 1) as i32; } - if *x & lb != 0 { + if (*x & lb) != 0 { a ^= *x; } } @@ -22,4 +22,4 @@ impl Solution { } vec![b, a] } -} \ No newline at end of file +} diff --git a/solution/0600-0699/0649.Dota2 Senate/Solution.rs b/solution/0600-0699/0649.Dota2 Senate/Solution.rs index 4040d500aedca..e0f8ff34ac3e5 100644 --- a/solution/0600-0699/0649.Dota2 Senate/Solution.rs +++ b/solution/0600-0699/0649.Dota2 Senate/Solution.rs @@ -1,30 +1,30 @@ -impl Solution { - pub fn predict_party_victory(senate: String) -> String { - let mut qr = std::collections::VecDeque::new(); - let mut qd = std::collections::VecDeque::new(); - let n = senate.len(); - for i in 0..n { - if let Some(char) = senate.chars().nth(i) { - if char == 'R' { - qr.push_back(i); - } else { - qd.push_back(i); - } - } - } - - while !qr.is_empty() && !qd.is_empty() { - let front_qr = qr.pop_front().unwrap(); - let front_qd = qd.pop_front().unwrap(); - if front_qr < front_qd { - qr.push_back(front_qr + n); - } else { - qd.push_back(front_qd + n); - } - } - if qr.is_empty() { - return "Dire".to_string(); - } - "Radiant".to_string() - } -} +impl Solution { + pub fn predict_party_victory(senate: String) -> String { + let mut qr = std::collections::VecDeque::new(); + let mut qd = std::collections::VecDeque::new(); + let n = senate.len(); + for i in 0..n { + if let Some(char) = senate.chars().nth(i) { + if char == 'R' { + qr.push_back(i); + } else { + qd.push_back(i); + } + } + } + + while !qr.is_empty() && !qd.is_empty() { + let front_qr = qr.pop_front().unwrap(); + let front_qd = qd.pop_front().unwrap(); + if front_qr < front_qd { + qr.push_back(front_qr + n); + } else { + qd.push_back(front_qd + n); + } + } + if qr.is_empty() { + return "Dire".to_string(); + } + "Radiant".to_string() + } +} diff --git a/solution/0600-0699/0652.Find Duplicate Subtrees/README.md b/solution/0600-0699/0652.Find Duplicate Subtrees/README.md index a283310e761a2..810419a2cc6ab 100644 --- a/solution/0600-0699/0652.Find Duplicate Subtrees/README.md +++ b/solution/0600-0699/0652.Find Duplicate Subtrees/README.md @@ -262,7 +262,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - res: &mut Vec>>>, + res: &mut Vec>>> ) -> String { if root.is_none() { return String::from('#'); @@ -285,7 +285,7 @@ impl Solution { } pub fn find_duplicate_subtrees( - root: Option>>, + root: Option>> ) -> Vec>>> { let mut map = HashMap::new(); let mut res = Vec::new(); diff --git a/solution/0600-0699/0652.Find Duplicate Subtrees/README_EN.md b/solution/0600-0699/0652.Find Duplicate Subtrees/README_EN.md index 4caf387c0d28f..d269b14006752 100644 --- a/solution/0600-0699/0652.Find Duplicate Subtrees/README_EN.md +++ b/solution/0600-0699/0652.Find Duplicate Subtrees/README_EN.md @@ -245,7 +245,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - res: &mut Vec>>>, + res: &mut Vec>>> ) -> String { if root.is_none() { return String::from('#'); @@ -268,7 +268,7 @@ impl Solution { } pub fn find_duplicate_subtrees( - root: Option>>, + root: Option>> ) -> Vec>>> { let mut map = HashMap::new(); let mut res = Vec::new(); diff --git a/solution/0600-0699/0652.Find Duplicate Subtrees/Solution.rs b/solution/0600-0699/0652.Find Duplicate Subtrees/Solution.rs index 6ff0619d2c43a..40d6976b7e419 100644 --- a/solution/0600-0699/0652.Find Duplicate Subtrees/Solution.rs +++ b/solution/0600-0699/0652.Find Duplicate Subtrees/Solution.rs @@ -23,7 +23,7 @@ impl Solution { fn dfs( root: &Option>>, map: &mut HashMap, - res: &mut Vec>>>, + res: &mut Vec>>> ) -> String { if root.is_none() { return String::from('#'); @@ -46,7 +46,7 @@ impl Solution { } pub fn find_duplicate_subtrees( - root: Option>>, + root: Option>> ) -> Vec>>> { let mut map = HashMap::new(); let mut res = Vec::new(); diff --git a/solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md b/solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md index 82d69fe26c2ea..b7d1ceb4ad7e7 100644 --- a/solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md +++ b/solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md @@ -406,7 +406,7 @@ function findTarget(root: TreeNode | null, k: number): boolean { // } use std::rc::Rc; use std::cell::RefCell; -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn find_target(root: Option>>, k: i32) -> bool { let mut set = HashSet::new(); diff --git a/solution/0600-0699/0653.Two Sum IV - Input is a BST/README_EN.md b/solution/0600-0699/0653.Two Sum IV - Input is a BST/README_EN.md index bcd78dca6f528..1be43ca96ede0 100644 --- a/solution/0600-0699/0653.Two Sum IV - Input is a BST/README_EN.md +++ b/solution/0600-0699/0653.Two Sum IV - Input is a BST/README_EN.md @@ -384,7 +384,7 @@ function findTarget(root: TreeNode | null, k: number): boolean { // } use std::rc::Rc; use std::cell::RefCell; -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn find_target(root: Option>>, k: i32) -> bool { let mut set = HashSet::new(); diff --git a/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.rs b/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.rs index 297c6e59e38a4..99e11ed72e66d 100644 --- a/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.rs +++ b/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.rs @@ -17,7 +17,7 @@ // } // } use std::cell::RefCell; -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; use std::rc::Rc; impl Solution { pub fn find_target(root: Option>>, k: i32) -> bool { diff --git a/solution/0600-0699/0654.Maximum Binary Tree/README.md b/solution/0600-0699/0654.Maximum Binary Tree/README.md index 0f1304e08535c..2c0cccfb4257b 100644 --- a/solution/0600-0699/0654.Maximum Binary Tree/README.md +++ b/solution/0600-0699/0654.Maximum Binary Tree/README.md @@ -778,11 +778,15 @@ impl Solution { max_val = nums[i]; } } - Some(Rc::new(RefCell::new(TreeNode { - val: max_val, - left: Self::construct(nums, start, idx), - right: Self::construct(nums, idx + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: max_val, + left: Self::construct(nums, start, idx), + right: Self::construct(nums, idx + 1, end), + }) + ) + ) } pub fn construct_maximum_binary_tree(nums: Vec) -> Option>> { diff --git a/solution/0600-0699/0654.Maximum Binary Tree/README_EN.md b/solution/0600-0699/0654.Maximum Binary Tree/README_EN.md index 38dd196cbd3d5..9f1b2c4120548 100644 --- a/solution/0600-0699/0654.Maximum Binary Tree/README_EN.md +++ b/solution/0600-0699/0654.Maximum Binary Tree/README_EN.md @@ -742,11 +742,15 @@ impl Solution { max_val = nums[i]; } } - Some(Rc::new(RefCell::new(TreeNode { - val: max_val, - left: Self::construct(nums, start, idx), - right: Self::construct(nums, idx + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: max_val, + left: Self::construct(nums, start, idx), + right: Self::construct(nums, idx + 1, end), + }) + ) + ) } pub fn construct_maximum_binary_tree(nums: Vec) -> Option>> { diff --git a/solution/0600-0699/0654.Maximum Binary Tree/Solution.rs b/solution/0600-0699/0654.Maximum Binary Tree/Solution.rs index 0a6d452e2a3a8..512faa57f3e4f 100644 --- a/solution/0600-0699/0654.Maximum Binary Tree/Solution.rs +++ b/solution/0600-0699/0654.Maximum Binary Tree/Solution.rs @@ -31,11 +31,15 @@ impl Solution { max_val = nums[i]; } } - Some(Rc::new(RefCell::new(TreeNode { - val: max_val, - left: Self::construct(nums, start, idx), - right: Self::construct(nums, idx + 1, end), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: max_val, + left: Self::construct(nums, start, idx), + right: Self::construct(nums, idx + 1, end), + }) + ) + ) } pub fn construct_maximum_binary_tree(nums: Vec) -> Option>> { diff --git a/solution/0600-0699/0655.Print Binary Tree/README.md b/solution/0600-0699/0655.Print Binary Tree/README.md index 467059c88d0fa..5e313dd1a77e5 100644 --- a/solution/0600-0699/0655.Print Binary Tree/README.md +++ b/solution/0600-0699/0655.Print Binary Tree/README.md @@ -546,33 +546,21 @@ impl Solution { i: usize, j: usize, res: &mut Vec>, - height: u32, + height: u32 ) { if root.is_none() { return; } let node = root.as_ref().unwrap().borrow(); res[i][j] = node.val.to_string(); - Self::dfs( - &node.left, - i + 1, - j - 2usize.pow(height - (i as u32) - 1), - res, - height, - ); - Self::dfs( - &node.right, - i + 1, - j + 2usize.pow(height - (i as u32) - 1), - res, - height, - ); + Self::dfs(&node.left, i + 1, j - (2usize).pow(height - (i as u32) - 1), res, height); + Self::dfs(&node.right, i + 1, j + (2usize).pow(height - (i as u32) - 1), res, height); } pub fn print_tree(root: Option>>) -> Vec> { let height = Self::get_height(&root, 0); let m = (height + 1) as usize; - let n = 2usize.pow(height + 1) - 1; + let n = (2usize).pow(height + 1) - 1; let mut res = vec![vec![String::new(); n]; m]; Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height); res diff --git a/solution/0600-0699/0655.Print Binary Tree/README_EN.md b/solution/0600-0699/0655.Print Binary Tree/README_EN.md index bc3e8965e8363..ee288a40e56e5 100644 --- a/solution/0600-0699/0655.Print Binary Tree/README_EN.md +++ b/solution/0600-0699/0655.Print Binary Tree/README_EN.md @@ -518,33 +518,21 @@ impl Solution { i: usize, j: usize, res: &mut Vec>, - height: u32, + height: u32 ) { if root.is_none() { return; } let node = root.as_ref().unwrap().borrow(); res[i][j] = node.val.to_string(); - Self::dfs( - &node.left, - i + 1, - j - 2usize.pow(height - (i as u32) - 1), - res, - height, - ); - Self::dfs( - &node.right, - i + 1, - j + 2usize.pow(height - (i as u32) - 1), - res, - height, - ); + Self::dfs(&node.left, i + 1, j - (2usize).pow(height - (i as u32) - 1), res, height); + Self::dfs(&node.right, i + 1, j + (2usize).pow(height - (i as u32) - 1), res, height); } pub fn print_tree(root: Option>>) -> Vec> { let height = Self::get_height(&root, 0); let m = (height + 1) as usize; - let n = 2usize.pow(height + 1) - 1; + let n = (2usize).pow(height + 1) - 1; let mut res = vec![vec![String::new(); n]; m]; Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height); res diff --git a/solution/0600-0699/0655.Print Binary Tree/Solution.rs b/solution/0600-0699/0655.Print Binary Tree/Solution.rs index c4a8aaec25ced..9b5ef7f11b5fe 100644 --- a/solution/0600-0699/0655.Print Binary Tree/Solution.rs +++ b/solution/0600-0699/0655.Print Binary Tree/Solution.rs @@ -32,33 +32,21 @@ impl Solution { i: usize, j: usize, res: &mut Vec>, - height: u32, + height: u32 ) { if root.is_none() { return; } let node = root.as_ref().unwrap().borrow(); res[i][j] = node.val.to_string(); - Self::dfs( - &node.left, - i + 1, - j - 2usize.pow(height - (i as u32) - 1), - res, - height, - ); - Self::dfs( - &node.right, - i + 1, - j + 2usize.pow(height - (i as u32) - 1), - res, - height, - ); + Self::dfs(&node.left, i + 1, j - (2usize).pow(height - (i as u32) - 1), res, height); + Self::dfs(&node.right, i + 1, j + (2usize).pow(height - (i as u32) - 1), res, height); } pub fn print_tree(root: Option>>) -> Vec> { let height = Self::get_height(&root, 0); let m = (height + 1) as usize; - let n = 2usize.pow(height + 1) - 1; + let n = (2usize).pow(height + 1) - 1; let mut res = vec![vec![String::new(); n]; m]; Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height); res diff --git a/solution/0600-0699/0658.Find K Closest Elements/README.md b/solution/0600-0699/0658.Find K Closest Elements/README.md index 070878eb01e7a..1b8a0612ecf76 100644 --- a/solution/0600-0699/0658.Find K Closest Elements/README.md +++ b/solution/0600-0699/0658.Find K Closest Elements/README.md @@ -284,7 +284,7 @@ impl Solution { let n = arr.len(); let mut l = 0; let mut r = n; - while r - l != k as usize { + while r - l != (k as usize) { if x - arr[l] <= arr[r - 1] - x { r -= 1; } else { diff --git a/solution/0600-0699/0658.Find K Closest Elements/README_EN.md b/solution/0600-0699/0658.Find K Closest Elements/README_EN.md index b12b1eeb2a45f..50382c8948587 100644 --- a/solution/0600-0699/0658.Find K Closest Elements/README_EN.md +++ b/solution/0600-0699/0658.Find K Closest Elements/README_EN.md @@ -251,7 +251,7 @@ impl Solution { let n = arr.len(); let mut l = 0; let mut r = n; - while r - l != k as usize { + while r - l != (k as usize) { if x - arr[l] <= arr[r - 1] - x { r -= 1; } else { diff --git a/solution/0600-0699/0661.Image Smoother/README.md b/solution/0600-0699/0661.Image Smoother/README.md index daed6c1197a06..42dea7284bcc0 100644 --- a/solution/0600-0699/0661.Image Smoother/README.md +++ b/solution/0600-0699/0661.Image Smoother/README.md @@ -173,9 +173,9 @@ impl Solution { let mut sum = 0; let mut count = 0; for [y, x] in locations.iter() { - let i = i as i32 + y; - let j = j as i32 + x; - if i < 0 || i == m as i32 || j < 0 || j == n as i32 { + let i = (i as i32) + y; + let j = (j as i32) + x; + if i < 0 || i == (m as i32) || j < 0 || j == (n as i32) { continue; } count += 1; diff --git a/solution/0600-0699/0661.Image Smoother/README_EN.md b/solution/0600-0699/0661.Image Smoother/README_EN.md index 428cb52c797d3..b9bd2adf62b03 100644 --- a/solution/0600-0699/0661.Image Smoother/README_EN.md +++ b/solution/0600-0699/0661.Image Smoother/README_EN.md @@ -155,9 +155,9 @@ impl Solution { let mut sum = 0; let mut count = 0; for [y, x] in locations.iter() { - let i = i as i32 + y; - let j = j as i32 + x; - if i < 0 || i == m as i32 || j < 0 || j == n as i32 { + let i = (i as i32) + y; + let j = (j as i32) + x; + if i < 0 || i == (m as i32) || j < 0 || j == (n as i32) { continue; } count += 1; diff --git a/solution/0600-0699/0661.Image Smoother/Solution.rs b/solution/0600-0699/0661.Image Smoother/Solution.rs index 69edeaa7381c6..4580eaad990a9 100644 --- a/solution/0600-0699/0661.Image Smoother/Solution.rs +++ b/solution/0600-0699/0661.Image Smoother/Solution.rs @@ -21,9 +21,9 @@ impl Solution { let mut sum = 0; let mut count = 0; for [y, x] in locations.iter() { - let i = i as i32 + y; - let j = j as i32 + x; - if i < 0 || i == m as i32 || j < 0 || j == n as i32 { + let i = (i as i32) + y; + let j = (j as i32) + x; + if i < 0 || i == (m as i32) || j < 0 || j == (n as i32) { continue; } count += 1; diff --git a/solution/0600-0699/0669.Trim a Binary Search Tree/README.md b/solution/0600-0699/0669.Trim a Binary Search Tree/README.md index f599770a04259..96ee8c44f1228 100644 --- a/solution/0600-0699/0669.Trim a Binary Search Tree/README.md +++ b/solution/0600-0699/0669.Trim a Binary Search Tree/README.md @@ -517,7 +517,7 @@ impl Solution { pub fn trim_bst( mut root: Option>>, low: i32, - high: i32, + high: i32 ) -> Option>> { if root.is_none() { return root; diff --git a/solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md b/solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md index d77bb5e5d82a4..d8390b8d23cc8 100644 --- a/solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md +++ b/solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md @@ -479,7 +479,7 @@ impl Solution { pub fn trim_bst( mut root: Option>>, low: i32, - high: i32, + high: i32 ) -> Option>> { if root.is_none() { return root; diff --git a/solution/0600-0699/0669.Trim a Binary Search Tree/Solution.rs b/solution/0600-0699/0669.Trim a Binary Search Tree/Solution.rs index 6dc32184c98de..8f5fa3de377e5 100644 --- a/solution/0600-0699/0669.Trim a Binary Search Tree/Solution.rs +++ b/solution/0600-0699/0669.Trim a Binary Search Tree/Solution.rs @@ -22,7 +22,7 @@ impl Solution { pub fn trim_bst( mut root: Option>>, low: i32, - high: i32, + high: i32 ) -> Option>> { if root.is_none() { return root; diff --git a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md index e383d6086b7b1..68037d7fba86b 100644 --- a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md +++ b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md @@ -144,11 +144,7 @@ impl Solution { // Let's dp for i in 1..n { - dp[i] = if nums[i] > nums[i - 1] { - dp[i - 1] + 1 - } else { - 1 - }; + dp[i] = if nums[i] > nums[i - 1] { dp[i - 1] + 1 } else { 1 }; ret = std::cmp::max(ret, dp[i]); } diff --git a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README_EN.md b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README_EN.md index e0b5b02f20232..a78bcc9e4e60f 100644 --- a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README_EN.md +++ b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README_EN.md @@ -128,11 +128,7 @@ impl Solution { // Let's dp for i in 1..n { - dp[i] = if nums[i] > nums[i - 1] { - dp[i - 1] + 1 - } else { - 1 - }; + dp[i] = if nums[i] > nums[i - 1] { dp[i - 1] + 1 } else { 1 }; ret = std::cmp::max(ret, dp[i]); } diff --git a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.rs b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.rs index 1c40a0a6c306c..c1076f8787eb4 100644 --- a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.rs +++ b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.rs @@ -8,14 +8,10 @@ impl Solution { // Let's dp for i in 1..n { - dp[i] = if nums[i] > nums[i - 1] { - dp[i - 1] + 1 - } else { - 1 - }; + dp[i] = if nums[i] > nums[i - 1] { dp[i - 1] + 1 } else { 1 }; ret = std::cmp::max(ret, dp[i]); } ret } -} \ No newline at end of file +} diff --git a/solution/0600-0699/0675.Cut Off Trees for Golf Event/README.md b/solution/0600-0699/0675.Cut Off Trees for Golf Event/README.md index 80ca52506c094..71103bdc750d2 100644 --- a/solution/0600-0699/0675.Cut Off Trees for Golf Event/README.md +++ b/solution/0600-0699/0675.Cut Off Trees for Golf Event/README.md @@ -325,7 +325,12 @@ func cutOffTree(forest [][]int) int { use std::collections::HashSet; use std::collections::VecDeque; -const DIRS: [[i32; 2]; 4] = [[-1, 0], [1, 0], [0, -1], [0, 1]]; +const DIRS: [[i32; 2]; 4] = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], +]; impl Solution { pub fn cut_off_tree(forest: Vec>) -> i32 { @@ -346,14 +351,15 @@ impl Solution { } for k in 0..4 { let x = state / col + DIRS[k][0]; - let y = state % col + DIRS[k][1]; + let y = (state % col) + DIRS[k][1]; let nxt = x * col + y; - if x >= 0 - && x < row - && y >= 0 - && y < col - && forest[x as usize][y as usize] != 0 - && !vis.contains(&nxt) + if + x >= 0 && + x < row && + y >= 0 && + y < col && + forest[x as usize][y as usize] != 0 && + !vis.contains(&nxt) { queue.push_back(nxt); vis.insert(nxt); diff --git a/solution/0600-0699/0675.Cut Off Trees for Golf Event/README_EN.md b/solution/0600-0699/0675.Cut Off Trees for Golf Event/README_EN.md index ed710d1549d84..e49005551c6dd 100644 --- a/solution/0600-0699/0675.Cut Off Trees for Golf Event/README_EN.md +++ b/solution/0600-0699/0675.Cut Off Trees for Golf Event/README_EN.md @@ -303,7 +303,12 @@ func cutOffTree(forest [][]int) int { use std::collections::HashSet; use std::collections::VecDeque; -const DIRS: [[i32; 2]; 4] = [[-1, 0], [1, 0], [0, -1], [0, 1]]; +const DIRS: [[i32; 2]; 4] = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], +]; impl Solution { pub fn cut_off_tree(forest: Vec>) -> i32 { @@ -324,14 +329,15 @@ impl Solution { } for k in 0..4 { let x = state / col + DIRS[k][0]; - let y = state % col + DIRS[k][1]; + let y = (state % col) + DIRS[k][1]; let nxt = x * col + y; - if x >= 0 - && x < row - && y >= 0 - && y < col - && forest[x as usize][y as usize] != 0 - && !vis.contains(&nxt) + if + x >= 0 && + x < row && + y >= 0 && + y < col && + forest[x as usize][y as usize] != 0 && + !vis.contains(&nxt) { queue.push_back(nxt); vis.insert(nxt); diff --git a/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.rs b/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.rs index 58e1ca615fc1d..29c56b572d508 100644 --- a/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.rs +++ b/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.rs @@ -1,7 +1,12 @@ use std::collections::HashSet; use std::collections::VecDeque; -const DIRS: [[i32; 2]; 4] = [[-1, 0], [1, 0], [0, -1], [0, 1]]; +const DIRS: [[i32; 2]; 4] = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], +]; impl Solution { pub fn cut_off_tree(forest: Vec>) -> i32 { @@ -22,14 +27,15 @@ impl Solution { } for k in 0..4 { let x = state / col + DIRS[k][0]; - let y = state % col + DIRS[k][1]; + let y = (state % col) + DIRS[k][1]; let nxt = x * col + y; - if x >= 0 - && x < row - && y >= 0 - && y < col - && forest[x as usize][y as usize] != 0 - && !vis.contains(&nxt) + if + x >= 0 && + x < row && + y >= 0 && + y < col && + forest[x as usize][y as usize] != 0 && + !vis.contains(&nxt) { queue.push_back(nxt); vis.insert(nxt); diff --git a/solution/0600-0699/0687.Longest Univalue Path/README.md b/solution/0600-0699/0687.Longest Univalue Path/README.md index 086189a599ade..7d9bf02e5301c 100644 --- a/solution/0600-0699/0687.Longest Univalue Path/README.md +++ b/solution/0600-0699/0687.Longest Univalue Path/README.md @@ -351,11 +351,7 @@ impl Solution { } let mut res = 0; - Self::dfs( - &root, - root.as_ref().unwrap().as_ref().borrow().val, - &mut res, - ); + Self::dfs(&root, root.as_ref().unwrap().as_ref().borrow().val, &mut res); res } } diff --git a/solution/0600-0699/0687.Longest Univalue Path/README_EN.md b/solution/0600-0699/0687.Longest Univalue Path/README_EN.md index 670fd06e4c699..d2e4fa26aef27 100644 --- a/solution/0600-0699/0687.Longest Univalue Path/README_EN.md +++ b/solution/0600-0699/0687.Longest Univalue Path/README_EN.md @@ -337,11 +337,7 @@ impl Solution { } let mut res = 0; - Self::dfs( - &root, - root.as_ref().unwrap().as_ref().borrow().val, - &mut res, - ); + Self::dfs(&root, root.as_ref().unwrap().as_ref().borrow().val, &mut res); res } } diff --git a/solution/0600-0699/0687.Longest Univalue Path/Solution.rs b/solution/0600-0699/0687.Longest Univalue Path/Solution.rs index 0b57f28e06406..c22ef18bed7bb 100644 --- a/solution/0600-0699/0687.Longest Univalue Path/Solution.rs +++ b/solution/0600-0699/0687.Longest Univalue Path/Solution.rs @@ -40,11 +40,7 @@ impl Solution { } let mut res = 0; - Self::dfs( - &root, - root.as_ref().unwrap().as_ref().borrow().val, - &mut res, - ); + Self::dfs(&root, root.as_ref().unwrap().as_ref().borrow().val, &mut res); res } } diff --git a/solution/0600-0699/0688.Knight Probability in Chessboard/README.md b/solution/0600-0699/0688.Knight Probability in Chessboard/README.md index 32bc50898e77a..28e198ec48101 100644 --- a/solution/0600-0699/0688.Knight Probability in Chessboard/README.md +++ b/solution/0600-0699/0688.Knight Probability in Chessboard/README.md @@ -157,7 +157,16 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)]; +const DIR: [(i32, i32); 8] = [ + (-2, -1), + (2, -1), + (-1, -2), + (1, -2), + (2, 1), + (-2, 1), + (1, 2), + (-1, 2), +]; const P: f64 = 1.0 / 8.0; impl Solution { @@ -165,7 +174,8 @@ impl Solution { pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 { // Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board // Starts from row: `j`, column: `k` - let mut dp: Vec>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; + let mut dp: Vec>> = + vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; // Initialize the dp vector, since dp[0][j][k] should be 1 for j in 0..n as usize { @@ -182,7 +192,8 @@ impl Solution { let x = j + dx; let y = k + dy; if Self::check_bounds(x, y, n, n) { - dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize]; + dp[i as usize][j as usize][k as usize] += + P * dp[(i as usize) - 1][x as usize][y as usize]; } } } diff --git a/solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md b/solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md index 25f28fa354511..63f551d567913 100644 --- a/solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md +++ b/solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md @@ -145,7 +145,16 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)]; +const DIR: [(i32, i32); 8] = [ + (-2, -1), + (2, -1), + (-1, -2), + (1, -2), + (2, 1), + (-2, 1), + (1, 2), + (-1, 2), +]; const P: f64 = 1.0 / 8.0; impl Solution { @@ -153,7 +162,8 @@ impl Solution { pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 { // Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board // Starts from row: `j`, column: `k` - let mut dp: Vec>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; + let mut dp: Vec>> = + vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; // Initialize the dp vector, since dp[0][j][k] should be 1 for j in 0..n as usize { @@ -170,7 +180,8 @@ impl Solution { let x = j + dx; let y = k + dy; if Self::check_bounds(x, y, n, n) { - dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize]; + dp[i as usize][j as usize][k as usize] += + P * dp[(i as usize) - 1][x as usize][y as usize]; } } } diff --git a/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.rs b/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.rs index f0091182e63c8..8db9b5ae851c3 100644 --- a/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.rs +++ b/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.rs @@ -1,4 +1,13 @@ -const DIR: [(i32, i32); 8] = [(-2, -1), (2, -1), (-1, -2), (1, -2), (2, 1), (-2, 1), (1, 2), (-1, 2)]; +const DIR: [(i32, i32); 8] = [ + (-2, -1), + (2, -1), + (-1, -2), + (1, -2), + (2, 1), + (-2, 1), + (1, 2), + (-1, 2), +]; const P: f64 = 1.0 / 8.0; impl Solution { @@ -6,7 +15,8 @@ impl Solution { pub fn knight_probability(n: i32, k: i32, row: i32, column: i32) -> f64 { // Here dp[i][j][k] represents through `i` steps, the probability that the knight stays on the board // Starts from row: `j`, column: `k` - let mut dp: Vec>> = vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; + let mut dp: Vec>> = + vec![vec![vec![0 as f64; n as usize]; n as usize]; k as usize + 1]; // Initialize the dp vector, since dp[0][j][k] should be 1 for j in 0..n as usize { @@ -23,7 +33,8 @@ impl Solution { let x = j + dx; let y = k + dy; if Self::check_bounds(x, y, n, n) { - dp[i as usize][j as usize][k as usize] += P * dp[i as usize - 1][x as usize][y as usize]; + dp[i as usize][j as usize][k as usize] += + P * dp[(i as usize) - 1][x as usize][y as usize]; } } } @@ -37,4 +48,4 @@ impl Solution { fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { i >= 0 && i < n && j >= 0 && j < m } -} \ No newline at end of file +} diff --git a/solution/0600-0699/0691.Stickers to Spell Word/README.md b/solution/0600-0699/0691.Stickers to Spell Word/README.md index 63d5f170499bd..8a1ebdcc7f45f 100644 --- a/solution/0600-0699/0691.Stickers to Spell Word/README.md +++ b/solution/0600-0699/0691.Stickers to Spell Word/README.md @@ -214,7 +214,7 @@ func minStickers(stickers []string, target string) int { ### **Rust** ```rust -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn min_stickers(stickers: Vec, target: String) -> i32 { diff --git a/solution/0600-0699/0691.Stickers to Spell Word/README_EN.md b/solution/0600-0699/0691.Stickers to Spell Word/README_EN.md index f1ee72ac8efd4..089445b57eafb 100644 --- a/solution/0600-0699/0691.Stickers to Spell Word/README_EN.md +++ b/solution/0600-0699/0691.Stickers to Spell Word/README_EN.md @@ -206,7 +206,7 @@ func minStickers(stickers []string, target string) int { ### **Rust** ```rust -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn min_stickers(stickers: Vec, target: String) -> i32 { diff --git a/solution/0600-0699/0691.Stickers to Spell Word/Solution.rs b/solution/0600-0699/0691.Stickers to Spell Word/Solution.rs index f55fb0563c5d9..c743c820b699d 100644 --- a/solution/0600-0699/0691.Stickers to Spell Word/Solution.rs +++ b/solution/0600-0699/0691.Stickers to Spell Word/Solution.rs @@ -1,4 +1,4 @@ -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; impl Solution { pub fn min_stickers(stickers: Vec, target: String) -> i32 { diff --git a/solution/0600-0699/0693.Binary Number with Alternating Bits/README.md b/solution/0600-0699/0693.Binary Number with Alternating Bits/README.md index 931b241c5138c..5fa8455f1916d 100644 --- a/solution/0600-0699/0693.Binary Number with Alternating Bits/README.md +++ b/solution/0600-0699/0693.Binary Number with Alternating Bits/README.md @@ -149,7 +149,7 @@ impl Solution { } while n != 0 { if (n & 3) != u { - return false + return false; } n >>= 2; } diff --git a/solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md b/solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md index 3161bc090a80d..ced719eb18a68 100644 --- a/solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md +++ b/solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md @@ -129,7 +129,7 @@ impl Solution { } while n != 0 { if (n & 3) != u { - return false + return false; } n >>= 2; } diff --git a/solution/0600-0699/0695.Max Area of Island/README.md b/solution/0600-0699/0695.Max Area of Island/README.md index a395008e9a48a..e7bee536181e1 100644 --- a/solution/0600-0699/0695.Max Area of Island/README.md +++ b/solution/0600-0699/0695.Max Area of Island/README.md @@ -223,10 +223,10 @@ impl Solution { grid[i][j] = 0; let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); if i != 0 { - res += Self::dfs(grid, i - 1, j) + res += Self::dfs(grid, i - 1, j); } if j != 0 { - res += Self::dfs(grid, i, j - 1) + res += Self::dfs(grid, i, j - 1); } res } @@ -237,7 +237,7 @@ impl Solution { let mut res = 0; for i in 0..m { for j in 0..n { - res = res.max(Self::dfs(&mut grid, i, j)) + res = res.max(Self::dfs(&mut grid, i, j)); } } res diff --git a/solution/0600-0699/0695.Max Area of Island/README_EN.md b/solution/0600-0699/0695.Max Area of Island/README_EN.md index 2c40e3c303919..5c13a84dfd177 100644 --- a/solution/0600-0699/0695.Max Area of Island/README_EN.md +++ b/solution/0600-0699/0695.Max Area of Island/README_EN.md @@ -205,10 +205,10 @@ impl Solution { grid[i][j] = 0; let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); if i != 0 { - res += Self::dfs(grid, i - 1, j) + res += Self::dfs(grid, i - 1, j); } if j != 0 { - res += Self::dfs(grid, i, j - 1) + res += Self::dfs(grid, i, j - 1); } res } @@ -219,7 +219,7 @@ impl Solution { let mut res = 0; for i in 0..m { for j in 0..n { - res = res.max(Self::dfs(&mut grid, i, j)) + res = res.max(Self::dfs(&mut grid, i, j)); } } res diff --git a/solution/0600-0699/0695.Max Area of Island/Solution.rs b/solution/0600-0699/0695.Max Area of Island/Solution.rs index c6623a665a6e3..a1882b0146834 100644 --- a/solution/0600-0699/0695.Max Area of Island/Solution.rs +++ b/solution/0600-0699/0695.Max Area of Island/Solution.rs @@ -6,10 +6,10 @@ impl Solution { grid[i][j] = 0; let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1); if i != 0 { - res += Self::dfs(grid, i - 1, j) + res += Self::dfs(grid, i - 1, j); } if j != 0 { - res += Self::dfs(grid, i, j - 1) + res += Self::dfs(grid, i, j - 1); } res } @@ -20,7 +20,7 @@ impl Solution { let mut res = 0; for i in 0..m { for j in 0..n { - res = res.max(Self::dfs(&mut grid, i, j)) + res = res.max(Self::dfs(&mut grid, i, j)); } } res diff --git a/solution/0700-0799/0704.Binary Search/README.md b/solution/0700-0799/0704.Binary Search/README.md index e2114b4c1f533..7e190c209aa4f 100644 --- a/solution/0700-0799/0704.Binary Search/README.md +++ b/solution/0700-0799/0704.Binary Search/README.md @@ -151,11 +151,17 @@ impl Solution { let mut l = 0; let mut r = nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; match nums[mid].cmp(&target) { - Ordering::Less => l = mid + 1, - Ordering::Greater => r = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + l = mid + 1; + } + Ordering::Greater => { + r = mid; + } + Ordering::Equal => { + return mid as i32; + } } } -1 @@ -173,7 +179,7 @@ impl Solution { if l == r { return if nums[l] == target { l as i32 } else { -1 }; } - let mid = l + r >> 1; + let mid = (l + r) >> 1; match nums[mid].cmp(&target) { Ordering::Less => Self::binary_search(nums, target, mid + 1, r), Ordering::Greater => Self::binary_search(nums, target, l, mid), diff --git a/solution/0700-0799/0704.Binary Search/README_EN.md b/solution/0700-0799/0704.Binary Search/README_EN.md index 574d4930f67b1..0ccc59e5717fd 100644 --- a/solution/0700-0799/0704.Binary Search/README_EN.md +++ b/solution/0700-0799/0704.Binary Search/README_EN.md @@ -147,11 +147,17 @@ impl Solution { let mut l = 0; let mut r = nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; match nums[mid].cmp(&target) { - Ordering::Less => l = mid + 1, - Ordering::Greater => r = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + l = mid + 1; + } + Ordering::Greater => { + r = mid; + } + Ordering::Equal => { + return mid as i32; + } } } -1 @@ -169,7 +175,7 @@ impl Solution { if l == r { return if nums[l] == target { l as i32 } else { -1 }; } - let mid = l + r >> 1; + let mid = (l + r) >> 1; match nums[mid].cmp(&target) { Ordering::Less => Self::binary_search(nums, target, mid + 1, r), Ordering::Greater => Self::binary_search(nums, target, l, mid), diff --git a/solution/0700-0799/0704.Binary Search/Solution.rs b/solution/0700-0799/0704.Binary Search/Solution.rs index 3ad2cb551cca4..6dba2beb26b30 100644 --- a/solution/0700-0799/0704.Binary Search/Solution.rs +++ b/solution/0700-0799/0704.Binary Search/Solution.rs @@ -5,11 +5,17 @@ impl Solution { let mut l = 0; let mut r = nums.len(); while l < r { - let mid = l + r >> 1; + let mid = (l + r) >> 1; match nums[mid].cmp(&target) { - Ordering::Less => l = mid + 1, - Ordering::Greater => r = mid, - Ordering::Equal => return mid as i32, + Ordering::Less => { + l = mid + 1; + } + Ordering::Greater => { + r = mid; + } + Ordering::Equal => { + return mid as i32; + } } } -1 diff --git a/solution/0700-0799/0707.Design Linked List/README.md b/solution/0700-0799/0707.Design Linked List/README.md index 3c309b1f064c3..b49378dc65629 100644 --- a/solution/0700-0799/0707.Design Linked List/README.md +++ b/solution/0700-0799/0707.Design Linked List/README.md @@ -865,7 +865,9 @@ impl MyLinkedList { let mut cur = self.head.as_ref().unwrap(); while index > 0 { match cur.next { - None => return -1, + None => { + return -1; + } Some(ref next) => { cur = next; index -= 1; @@ -876,10 +878,12 @@ impl MyLinkedList { } fn add_at_head(&mut self, val: i32) { - self.head = Some(Box::new(ListNode { - val, - next: self.head.take(), - })); + self.head = Some( + Box::new(ListNode { + val, + next: self.head.take(), + }) + ); } fn add_at_tail(&mut self, val: i32) { @@ -908,10 +912,12 @@ impl MyLinkedList { cur = cur.next.as_mut().unwrap(); index -= 1; } - cur.next = Some(Box::new(ListNode { - val, - next: cur.next.take(), - })); + cur.next = Some( + Box::new(ListNode { + val, + next: cur.next.take(), + }) + ); self.head = dummy.next; } @@ -930,10 +936,7 @@ impl MyLinkedList { cur.next = cur.next.take().and_then(|n| n.next); self.head = dummy.next; } -} - - -/** +}/** * Your MyLinkedList object will be instantiated and called as such: * let obj = MyLinkedList::new(); * let ret_1: i32 = obj.get(index); diff --git a/solution/0700-0799/0707.Design Linked List/README_EN.md b/solution/0700-0799/0707.Design Linked List/README_EN.md index 49a71101aed59..1d6b3125d27ff 100644 --- a/solution/0700-0799/0707.Design Linked List/README_EN.md +++ b/solution/0700-0799/0707.Design Linked List/README_EN.md @@ -821,7 +821,9 @@ impl MyLinkedList { let mut cur = self.head.as_ref().unwrap(); while index > 0 { match cur.next { - None => return -1, + None => { + return -1; + } Some(ref next) => { cur = next; index -= 1; @@ -832,10 +834,12 @@ impl MyLinkedList { } fn add_at_head(&mut self, val: i32) { - self.head = Some(Box::new(ListNode { - val, - next: self.head.take(), - })); + self.head = Some( + Box::new(ListNode { + val, + next: self.head.take(), + }) + ); } fn add_at_tail(&mut self, val: i32) { @@ -864,10 +868,12 @@ impl MyLinkedList { cur = cur.next.as_mut().unwrap(); index -= 1; } - cur.next = Some(Box::new(ListNode { - val, - next: cur.next.take(), - })); + cur.next = Some( + Box::new(ListNode { + val, + next: cur.next.take(), + }) + ); self.head = dummy.next; } @@ -886,10 +892,7 @@ impl MyLinkedList { cur.next = cur.next.take().and_then(|n| n.next); self.head = dummy.next; } -} - - -/** +}/** * Your MyLinkedList object will be instantiated and called as such: * let obj = MyLinkedList::new(); * let ret_1: i32 = obj.get(index); diff --git a/solution/0700-0799/0707.Design Linked List/Solution.rs b/solution/0700-0799/0707.Design Linked List/Solution.rs index 0642175b90e0e..c61734e279289 100644 --- a/solution/0700-0799/0707.Design Linked List/Solution.rs +++ b/solution/0700-0799/0707.Design Linked List/Solution.rs @@ -19,7 +19,9 @@ impl MyLinkedList { let mut cur = self.head.as_ref().unwrap(); while index > 0 { match cur.next { - None => return -1, + None => { + return -1; + } Some(ref next) => { cur = next; index -= 1; @@ -30,10 +32,12 @@ impl MyLinkedList { } fn add_at_head(&mut self, val: i32) { - self.head = Some(Box::new(ListNode { - val, - next: self.head.take(), - })); + self.head = Some( + Box::new(ListNode { + val, + next: self.head.take(), + }) + ); } fn add_at_tail(&mut self, val: i32) { @@ -62,10 +66,12 @@ impl MyLinkedList { cur = cur.next.as_mut().unwrap(); index -= 1; } - cur.next = Some(Box::new(ListNode { - val, - next: cur.next.take(), - })); + cur.next = Some( + Box::new(ListNode { + val, + next: cur.next.take(), + }) + ); self.head = dummy.next; } @@ -84,10 +90,7 @@ impl MyLinkedList { cur.next = cur.next.take().and_then(|n| n.next); self.head = dummy.next; } -} - - -/** +}/** * Your MyLinkedList object will be instantiated and called as such: * let obj = MyLinkedList::new(); * let ret_1: i32 = obj.get(index); @@ -95,4 +98,4 @@ impl MyLinkedList { * obj.add_at_tail(val); * obj.add_at_index(index, val); * obj.delete_at_index(index); - */ \ No newline at end of file + */ diff --git a/solution/0700-0799/0713.Subarray Product Less Than K/README.md b/solution/0700-0799/0713.Subarray Product Less Than K/README.md index f5790453c91ba..140be49362d7a 100644 --- a/solution/0700-0799/0713.Subarray Product Less Than K/README.md +++ b/solution/0700-0799/0713.Subarray Product Less Than K/README.md @@ -160,14 +160,16 @@ impl Solution { let mut res = 0; let mut product = 1; let mut i = 0; - nums.iter().enumerate().for_each(|(j, v)| { - product *= v; - while product >= k { - product /= nums[i]; - i += 1; - } - res += j - i + 1; - }); + nums.iter() + .enumerate() + .for_each(|(j, v)| { + product *= v; + while product >= k { + product /= nums[i]; + i += 1; + } + res += j - i + 1; + }); res as i32 } } diff --git a/solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md b/solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md index b1c5fa2bd31bc..aad4de8ce1377 100644 --- a/solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md +++ b/solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md @@ -131,14 +131,16 @@ impl Solution { let mut res = 0; let mut product = 1; let mut i = 0; - nums.iter().enumerate().for_each(|(j, v)| { - product *= v; - while product >= k { - product /= nums[i]; - i += 1; - } - res += j - i + 1; - }); + nums.iter() + .enumerate() + .for_each(|(j, v)| { + product *= v; + while product >= k { + product /= nums[i]; + i += 1; + } + res += j - i + 1; + }); res as i32 } } diff --git a/solution/0700-0799/0713.Subarray Product Less Than K/Solution.rs b/solution/0700-0799/0713.Subarray Product Less Than K/Solution.rs index 4514c5f215eee..652789fa30e70 100644 --- a/solution/0700-0799/0713.Subarray Product Less Than K/Solution.rs +++ b/solution/0700-0799/0713.Subarray Product Less Than K/Solution.rs @@ -7,14 +7,16 @@ impl Solution { let mut res = 0; let mut product = 1; let mut i = 0; - nums.iter().enumerate().for_each(|(j, v)| { - product *= v; - while product >= k { - product /= nums[i]; - i += 1; - } - res += j - i + 1; - }); + nums.iter() + .enumerate() + .for_each(|(j, v)| { + product *= v; + while product >= k { + product /= nums[i]; + i += 1; + } + res += j - i + 1; + }); res as i32 } } diff --git a/solution/0700-0799/0722.Remove Comments/Solution.rs b/solution/0700-0799/0722.Remove Comments/Solution.rs index aa575cf64f3c3..0fe2d40687dd0 100644 --- a/solution/0700-0799/0722.Remove Comments/Solution.rs +++ b/solution/0700-0799/0722.Remove Comments/Solution.rs @@ -1,37 +1,37 @@ -impl Solution { - pub fn remove_comments(source: Vec) -> Vec { - let mut ans: Vec = Vec::new(); - let mut t: Vec = Vec::new(); - let mut blockComment = false; - - for s in &source { - let m = s.len(); - let mut i = 0; - while i < m { - if blockComment { - if i + 1 < m && &s[i..i + 2] == "*/" { - blockComment = false; - i += 2; - } else { - i += 1; - } - } else { - if i + 1 < m && &s[i..i + 2] == "/*" { - blockComment = true; - i += 2; - } else if i + 1 < m && &s[i..i + 2] == "//" { - break; - } else { - t.push(s.chars().nth(i).unwrap().to_string()); - i += 1; - } - } - } - if !blockComment && !t.is_empty() { - ans.push(t.join("")); - t.clear(); - } - } - ans - } -} +impl Solution { + pub fn remove_comments(source: Vec) -> Vec { + let mut ans: Vec = Vec::new(); + let mut t: Vec = Vec::new(); + let mut blockComment = false; + + for s in &source { + let m = s.len(); + let mut i = 0; + while i < m { + if blockComment { + if i + 1 < m && &s[i..i + 2] == "*/" { + blockComment = false; + i += 2; + } else { + i += 1; + } + } else { + if i + 1 < m && &s[i..i + 2] == "/*" { + blockComment = true; + i += 2; + } else if i + 1 < m && &s[i..i + 2] == "//" { + break; + } else { + t.push(s.chars().nth(i).unwrap().to_string()); + i += 1; + } + } + } + if !blockComment && !t.is_empty() { + ans.push(t.join("")); + t.clear(); + } + } + ans + } +} diff --git a/solution/0700-0799/0724.Find Pivot Index/Solution.rs b/solution/0700-0799/0724.Find Pivot Index/Solution.rs index 2fe7b7661061e..a125f993e868b 100644 --- a/solution/0700-0799/0724.Find Pivot Index/Solution.rs +++ b/solution/0700-0799/0724.Find Pivot Index/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn pivot_index(nums: Vec) -> i32 { - let (mut left, mut right): (i32, i32) = (0, nums.iter().sum()); - for i in 0..nums.len() { - right -= nums[i]; - if left == right { - return i as i32; - } - left += nums[i]; - } - -1 - } -} +impl Solution { + pub fn pivot_index(nums: Vec) -> i32 { + let (mut left, mut right): (i32, i32) = (0, nums.iter().sum()); + for i in 0..nums.len() { + right -= nums[i]; + if left == right { + return i as i32; + } + left += nums[i]; + } + -1 + } +} diff --git a/solution/0700-0799/0728.Self Dividing Numbers/README.md b/solution/0700-0799/0728.Self Dividing Numbers/README.md index 60fecbfb69539..bf8fd6126f298 100644 --- a/solution/0700-0799/0728.Self Dividing Numbers/README.md +++ b/solution/0700-0799/0728.Self Dividing Numbers/README.md @@ -96,16 +96,18 @@ impl Solution { let mut res = vec![]; for i in left..=right { let mut num = i; - if loop { - if num == 0 { - break true; + if ( + loop { + if num == 0 { + break true; + } + let j = num % 10; + if j == 0 || i % j != 0 { + break false; + } + num /= 10; } - let j = num % 10; - if j == 0 || i % j != 0 { - break false; - } - num /= 10; - } { + ) { res.push(i); } } diff --git a/solution/0700-0799/0728.Self Dividing Numbers/README_EN.md b/solution/0700-0799/0728.Self Dividing Numbers/README_EN.md index a7db40fe1c1bb..2366c132f4aec 100644 --- a/solution/0700-0799/0728.Self Dividing Numbers/README_EN.md +++ b/solution/0700-0799/0728.Self Dividing Numbers/README_EN.md @@ -79,16 +79,18 @@ impl Solution { let mut res = vec![]; for i in left..=right { let mut num = i; - if loop { - if num == 0 { - break true; + if ( + loop { + if num == 0 { + break true; + } + let j = num % 10; + if j == 0 || i % j != 0 { + break false; + } + num /= 10; } - let j = num % 10; - if j == 0 || i % j != 0 { - break false; - } - num /= 10; - } { + ) { res.push(i); } } diff --git a/solution/0700-0799/0728.Self Dividing Numbers/Solution.rs b/solution/0700-0799/0728.Self Dividing Numbers/Solution.rs index ee720d2db4334..a45b74235777f 100644 --- a/solution/0700-0799/0728.Self Dividing Numbers/Solution.rs +++ b/solution/0700-0799/0728.Self Dividing Numbers/Solution.rs @@ -3,16 +3,18 @@ impl Solution { let mut res = vec![]; for i in left..=right { let mut num = i; - if loop { - if num == 0 { - break true; + if ( + loop { + if num == 0 { + break true; + } + let j = num % 10; + if j == 0 || i % j != 0 { + break false; + } + num /= 10; } - let j = num % 10; - if j == 0 || i % j != 0 { - break false; - } - num /= 10; - } { + ) { res.push(i); } } diff --git a/solution/0700-0799/0729.My Calendar I/README.md b/solution/0700-0799/0729.My Calendar I/README.md index d24064111a39e..ecd5be5e996eb 100644 --- a/solution/0700-0799/0729.My Calendar I/README.md +++ b/solution/0700-0799/0729.My Calendar I/README.md @@ -239,9 +239,7 @@ impl MyCalendar { self.bt.insert(start, end); true } -} - -/** +}/** * Your MyCalendar object will be instantiated and called as such: * let obj = MyCalendar::new(); * let ret_1: bool = obj.book(start, end); diff --git a/solution/0700-0799/0729.My Calendar I/README_EN.md b/solution/0700-0799/0729.My Calendar I/README_EN.md index 37a33de2cb3ea..54d5bfc8b6262 100644 --- a/solution/0700-0799/0729.My Calendar I/README_EN.md +++ b/solution/0700-0799/0729.My Calendar I/README_EN.md @@ -229,9 +229,7 @@ impl MyCalendar { self.bt.insert(start, end); true } -} - -/** +}/** * Your MyCalendar object will be instantiated and called as such: * let obj = MyCalendar::new(); * let ret_1: bool = obj.book(start, end); diff --git a/solution/0700-0799/0729.My Calendar I/Solution.rs b/solution/0700-0799/0729.My Calendar I/Solution.rs index 42ea7cde8c94b..8867c57bb1a4c 100644 --- a/solution/0700-0799/0729.My Calendar I/Solution.rs +++ b/solution/0700-0799/0729.My Calendar I/Solution.rs @@ -30,9 +30,7 @@ impl MyCalendar { self.bt.insert(start, end); true } -} - -/** +}/** * Your MyCalendar object will be instantiated and called as such: * let obj = MyCalendar::new(); * let ret_1: bool = obj.book(start, end); diff --git a/solution/0700-0799/0733.Flood Fill/README.md b/solution/0700-0799/0733.Flood Fill/README.md index b6142abe18f8c..91b22210ac109 100644 --- a/solution/0700-0799/0733.Flood Fill/README.md +++ b/solution/0700-0799/0733.Flood Fill/README.md @@ -317,7 +317,7 @@ function floodFill(image: number[][], sr: number, sc: number, newColor: number): ```rust impl Solution { fn dfs(image: &mut Vec>, sr: i32, sc: i32, new_color: i32, target: i32) { - if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 { + if sr < 0 || sr == (image.len() as i32) || sc < 0 || sc == (image[0].len() as i32) { return; } let sr = sr as usize; diff --git a/solution/0700-0799/0733.Flood Fill/README_EN.md b/solution/0700-0799/0733.Flood Fill/README_EN.md index c82b46abe080a..1b3ae43541946 100644 --- a/solution/0700-0799/0733.Flood Fill/README_EN.md +++ b/solution/0700-0799/0733.Flood Fill/README_EN.md @@ -302,7 +302,7 @@ function floodFill(image: number[][], sr: number, sc: number, newColor: number): ```rust impl Solution { fn dfs(image: &mut Vec>, sr: i32, sc: i32, new_color: i32, target: i32) { - if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 { + if sr < 0 || sr == (image.len() as i32) || sc < 0 || sc == (image[0].len() as i32) { return; } let sr = sr as usize; diff --git a/solution/0700-0799/0733.Flood Fill/Solution.rs b/solution/0700-0799/0733.Flood Fill/Solution.rs index be0b3c7defaa8..81a4f802a623f 100644 --- a/solution/0700-0799/0733.Flood Fill/Solution.rs +++ b/solution/0700-0799/0733.Flood Fill/Solution.rs @@ -1,6 +1,6 @@ impl Solution { fn dfs(image: &mut Vec>, sr: i32, sc: i32, new_color: i32, target: i32) { - if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 { + if sr < 0 || sr == (image.len() as i32) || sc < 0 || sc == (image[0].len() as i32) { return; } let sr = sr as usize; diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.rs b/solution/0700-0799/0735.Asteroid Collision/Solution.rs index bd3c7e88457d9..a92f4decd6bd0 100644 --- a/solution/0700-0799/0735.Asteroid Collision/Solution.rs +++ b/solution/0700-0799/0735.Asteroid Collision/Solution.rs @@ -1,21 +1,21 @@ -impl Solution { - #[allow(dead_code)] - pub fn asteroid_collision(asteroids: Vec) -> Vec { - let mut stk = Vec::new(); - for &x in &asteroids { - if x > 0 { - stk.push(x); - } else { - while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x { - stk.pop(); - } - if !stk.is_empty() && *stk.last().unwrap() == -x { - stk.pop(); - } else if stk.is_empty() || *stk.last().unwrap() < 0 { - stk.push(x); - } - } - } - stk - } -} \ No newline at end of file +impl Solution { + #[allow(dead_code)] + pub fn asteroid_collision(asteroids: Vec) -> Vec { + let mut stk = Vec::new(); + for &x in &asteroids { + if x > 0 { + stk.push(x); + } else { + while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x { + stk.pop(); + } + if !stk.is_empty() && *stk.last().unwrap() == -x { + stk.pop(); + } else if stk.is_empty() || *stk.last().unwrap() < 0 { + stk.push(x); + } + } + } + stk + } +} diff --git a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md index 54106b88c59f8..8868e2bc6c49b 100644 --- a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md +++ b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md @@ -177,7 +177,10 @@ func nextGreatestLetter(letters []byte, target byte) byte { ```rust impl Solution { pub fn next_greatest_letter(letters: Vec, target: char) -> char { - *letters.iter().find(|&&c| c > target).unwrap_or(&letters[0]) + *letters + .iter() + .find(|&&c| c > target) + .unwrap_or(&letters[0]) } } ``` diff --git a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md index 8c0f2b60d9295..36e44a793e83a 100644 --- a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md +++ b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md @@ -143,7 +143,10 @@ func nextGreatestLetter(letters []byte, target byte) byte { ```rust impl Solution { pub fn next_greatest_letter(letters: Vec, target: char) -> char { - *letters.iter().find(|&&c| c > target).unwrap_or(&letters[0]) + *letters + .iter() + .find(|&&c| c > target) + .unwrap_or(&letters[0]) } } ``` diff --git a/solution/0700-0799/0763.Partition Labels/Solution.rs b/solution/0700-0799/0763.Partition Labels/Solution.rs index 28ccaca0d630d..14fd327b4c27b 100644 --- a/solution/0700-0799/0763.Partition Labels/Solution.rs +++ b/solution/0700-0799/0763.Partition Labels/Solution.rs @@ -1,21 +1,21 @@ -impl Solution { - pub fn partition_labels(s: String) -> Vec { - let n = s.len(); - let bytes = s.as_bytes(); - let mut last = [0; 26]; - for i in 0..n { - last[(bytes[i] - b'a') as usize] = i; - } - let mut ans = vec![]; - let mut j = 0; - let mut mx = 0; - for i in 0..n { - mx = mx.max(last[(bytes[i] - b'a') as usize]); - if mx == i { - ans.push((i - j + 1) as i32); - j = i + 1; - } - } - ans - } -} +impl Solution { + pub fn partition_labels(s: String) -> Vec { + let n = s.len(); + let bytes = s.as_bytes(); + let mut last = [0; 26]; + for i in 0..n { + last[(bytes[i] - b'a') as usize] = i; + } + let mut ans = vec![]; + let mut j = 0; + let mut mx = 0; + for i in 0..n { + mx = mx.max(last[(bytes[i] - b'a') as usize]); + if mx == i { + ans.push((i - j + 1) as i32); + j = i + 1; + } + } + ans + } +} diff --git a/solution/0700-0799/0767.Reorganize String/README.md b/solution/0700-0799/0767.Reorganize String/README.md index 506379fcf5b8e..f2c012433e8ef 100644 --- a/solution/0700-0799/0767.Reorganize String/README.md +++ b/solution/0700-0799/0767.Reorganize String/README.md @@ -263,7 +263,7 @@ public: ### **Rust** ```rust -use std::collections::{HashMap, BinaryHeap, VecDeque}; +use std::collections::{ HashMap, BinaryHeap, VecDeque }; impl Solution { #[allow(dead_code)] @@ -276,15 +276,16 @@ impl Solution { // Initialize the HashMap for c in s.chars() { - map - .entry(c) - .and_modify(|e| *e += 1) + map.entry(c) + .and_modify(|e| { + *e += 1; + }) .or_insert(1); } // Initialize the binary heap for (k, v) in map.iter() { - if 2 * *v - 1 > n { + if 2 * *v - 1 > n { return "".to_string(); } else { pq.push((*v, *k)); @@ -303,7 +304,11 @@ impl Solution { } } - if ret.len() == n { ret } else { "".to_string() } + if ret.len() == n { + ret + } else { + "".to_string() + } } } ``` diff --git a/solution/0700-0799/0767.Reorganize String/README_EN.md b/solution/0700-0799/0767.Reorganize String/README_EN.md index fd38a73342088..57b8527629c9e 100644 --- a/solution/0700-0799/0767.Reorganize String/README_EN.md +++ b/solution/0700-0799/0767.Reorganize String/README_EN.md @@ -223,7 +223,7 @@ public: ### **Rust** ```rust -use std::collections::{HashMap, BinaryHeap, VecDeque}; +use std::collections::{ HashMap, BinaryHeap, VecDeque }; impl Solution { #[allow(dead_code)] @@ -236,15 +236,16 @@ impl Solution { // Initialize the HashMap for c in s.chars() { - map - .entry(c) - .and_modify(|e| *e += 1) + map.entry(c) + .and_modify(|e| { + *e += 1; + }) .or_insert(1); } // Initialize the binary heap for (k, v) in map.iter() { - if 2 * *v - 1 > n { + if 2 * *v - 1 > n { return "".to_string(); } else { pq.push((*v, *k)); @@ -263,7 +264,11 @@ impl Solution { } } - if ret.len() == n { ret } else { "".to_string() } + if ret.len() == n { + ret + } else { + "".to_string() + } } } ``` diff --git a/solution/0700-0799/0767.Reorganize String/Solution.rs b/solution/0700-0799/0767.Reorganize String/Solution.rs index 3c75da0250df7..cfc639e028ae5 100644 --- a/solution/0700-0799/0767.Reorganize String/Solution.rs +++ b/solution/0700-0799/0767.Reorganize String/Solution.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, BinaryHeap, VecDeque}; +use std::collections::{ HashMap, BinaryHeap, VecDeque }; impl Solution { #[allow(dead_code)] @@ -11,15 +11,16 @@ impl Solution { // Initialize the HashMap for c in s.chars() { - map - .entry(c) - .and_modify(|e| *e += 1) + map.entry(c) + .and_modify(|e| { + *e += 1; + }) .or_insert(1); } // Initialize the binary heap for (k, v) in map.iter() { - if 2 * *v - 1 > n { + if 2 * *v - 1 > n { return "".to_string(); } else { pq.push((*v, *k)); @@ -38,6 +39,10 @@ impl Solution { } } - if ret.len() == n { ret } else { "".to_string() } + if ret.len() == n { + ret + } else { + "".to_string() + } } -} \ No newline at end of file +} diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md index d4c39b0ee1902..f1a2495404562 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md @@ -172,7 +172,7 @@ impl Solution { while !stack.is_empty() && num < stack.last().unwrap() { stack.pop(); } - stack.push(max) + stack.push(max); } else { stack.push(*num); } diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md index 224d1b02c9f31..662082afa7370 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md @@ -157,7 +157,7 @@ impl Solution { while !stack.is_empty() && num < stack.last().unwrap() { stack.pop(); } - stack.push(max) + stack.push(max); } else { stack.push(*num); } diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs index 65c98f326c89e..503c3e28cd3f3 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.rs @@ -7,7 +7,7 @@ impl Solution { while !stack.is_empty() && num < stack.last().unwrap() { stack.pop(); } - stack.push(max) + stack.push(max); } else { stack.push(*num); } diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md index 7dc62667dbc0d..c2640a4950a72 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README.md @@ -253,7 +253,7 @@ impl Solution { let mut max = 0; for i in 0..arr.len() { max = max.max(arr[i]); - if max == i as i32 { + if max == (i as i32) { res += 1; } } diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md index f219e3a459af8..1c3315bd61417 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md @@ -226,7 +226,7 @@ impl Solution { let mut max = 0; for i in 0..arr.len() { max = max.max(arr[i]); - if max == i as i32 { + if max == (i as i32) { res += 1; } } diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs index 5fb63875622b4..d57cd9a5e60a8 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.rs @@ -4,7 +4,7 @@ impl Solution { let mut max = 0; for i in 0..arr.len() { max = max.max(arr[i]); - if max == i as i32 { + if max == (i as i32) { res += 1; } } diff --git a/solution/0700-0799/0778.Swim in Rising Water/README.md b/solution/0700-0799/0778.Swim in Rising Water/README.md index dd2be2e5afb27..06f0becb9a911 100644 --- a/solution/0700-0799/0778.Swim in Rising Water/README.md +++ b/solution/0700-0799/0778.Swim in Rising Water/README.md @@ -271,7 +271,12 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)]; +const DIR: [(i32, i32); 4] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), +]; impl Solution { #[allow(dead_code)] @@ -309,10 +314,13 @@ impl Solution { } // Otherwise, let's union the square with its neighbors for (dx, dy) in DIR { - let x = dx + i as i32; - let y = dy + j as i32; - if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time { - Self::union(i * m + j, x as usize * m + y as usize, d_set); + let x = dx + (i as i32); + let y = dy + (j as i32); + if + Self::check_bounds(x, y, n as i32, m as i32) && + grid[x as usize][y as usize] <= cur_time + { + Self::union(i * m + j, (x as usize) * m + (y as usize), d_set); } } } diff --git a/solution/0700-0799/0778.Swim in Rising Water/README_EN.md b/solution/0700-0799/0778.Swim in Rising Water/README_EN.md index 91acb8bd8cd36..1ed2a2dbf963c 100644 --- a/solution/0700-0799/0778.Swim in Rising Water/README_EN.md +++ b/solution/0700-0799/0778.Swim in Rising Water/README_EN.md @@ -191,7 +191,12 @@ public: ### **Rust** ```rust -const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)]; +const DIR: [(i32, i32); 4] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), +]; impl Solution { #[allow(dead_code)] @@ -229,10 +234,13 @@ impl Solution { } // Otherwise, let's union the square with its neighbors for (dx, dy) in DIR { - let x = dx + i as i32; - let y = dy + j as i32; - if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time { - Self::union(i * m + j, x as usize * m + y as usize, d_set); + let x = dx + (i as i32); + let y = dy + (j as i32); + if + Self::check_bounds(x, y, n as i32, m as i32) && + grid[x as usize][y as usize] <= cur_time + { + Self::union(i * m + j, (x as usize) * m + (y as usize), d_set); } } } diff --git a/solution/0700-0799/0778.Swim in Rising Water/Solution.rs b/solution/0700-0799/0778.Swim in Rising Water/Solution.rs index 0d78908102b51..ebf2ec7183916 100644 --- a/solution/0700-0799/0778.Swim in Rising Water/Solution.rs +++ b/solution/0700-0799/0778.Swim in Rising Water/Solution.rs @@ -1,4 +1,9 @@ -const DIR: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)]; +const DIR: [(i32, i32); 4] = [ + (-1, 0), + (1, 0), + (0, -1), + (0, 1), +]; impl Solution { #[allow(dead_code)] @@ -36,10 +41,13 @@ impl Solution { } // Otherwise, let's union the square with its neighbors for (dx, dy) in DIR { - let x = dx + i as i32; - let y = dy + j as i32; - if Self::check_bounds(x, y, n as i32, m as i32) && grid[x as usize][y as usize] <= cur_time { - Self::union(i * m + j, x as usize * m + y as usize, d_set); + let x = dx + (i as i32); + let y = dy + (j as i32); + if + Self::check_bounds(x, y, n as i32, m as i32) && + grid[x as usize][y as usize] <= cur_time + { + Self::union(i * m + j, (x as usize) * m + (y as usize), d_set); } } } @@ -67,4 +75,4 @@ impl Solution { fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { i >= 0 && i < n && j >= 0 && j < m } -} \ No newline at end of file +} diff --git a/solution/0700-0799/0785.Is Graph Bipartite/README.md b/solution/0700-0799/0785.Is Graph Bipartite/README.md index 98e489ee4dbba..a72ef40f456cc 100644 --- a/solution/0700-0799/0785.Is Graph Bipartite/README.md +++ b/solution/0700-0799/0785.Is Graph Bipartite/README.md @@ -321,7 +321,12 @@ impl Solution { } #[allow(dead_code)] - fn traverse(v: usize, color: usize, color_vec: &mut Vec, graph: &mut Vec>) -> bool { + fn traverse( + v: usize, + color: usize, + color_vec: &mut Vec, + graph: &mut Vec> + ) -> bool { color_vec[v] = color; for n in graph[v].clone() { if color_vec[n as usize] == 0 { diff --git a/solution/0700-0799/0785.Is Graph Bipartite/README_EN.md b/solution/0700-0799/0785.Is Graph Bipartite/README_EN.md index 82f1a18410e98..b5e982e513378 100644 --- a/solution/0700-0799/0785.Is Graph Bipartite/README_EN.md +++ b/solution/0700-0799/0785.Is Graph Bipartite/README_EN.md @@ -240,7 +240,12 @@ impl Solution { } #[allow(dead_code)] - fn traverse(v: usize, color: usize, color_vec: &mut Vec, graph: &mut Vec>) -> bool { + fn traverse( + v: usize, + color: usize, + color_vec: &mut Vec, + graph: &mut Vec> + ) -> bool { color_vec[v] = color; for n in graph[v].clone() { if color_vec[n as usize] == 0 { diff --git a/solution/0700-0799/0785.Is Graph Bipartite/Solution.rs b/solution/0700-0799/0785.Is Graph Bipartite/Solution.rs index ed599ac90a6c1..851fd0f028ad6 100644 --- a/solution/0700-0799/0785.Is Graph Bipartite/Solution.rs +++ b/solution/0700-0799/0785.Is Graph Bipartite/Solution.rs @@ -13,7 +13,12 @@ impl Solution { } #[allow(dead_code)] - fn traverse(v: usize, color: usize, color_vec: &mut Vec, graph: &mut Vec>) -> bool { + fn traverse( + v: usize, + color: usize, + color_vec: &mut Vec, + graph: &mut Vec> + ) -> bool { color_vec[v] = color; for n in graph[v].clone() { if color_vec[n as usize] == 0 { @@ -28,4 +33,4 @@ impl Solution { } true } -} \ No newline at end of file +} diff --git a/solution/0700-0799/0791.Custom Sort String/README.md b/solution/0700-0799/0791.Custom Sort String/README.md index ee0b2d8b7210c..9ec50eeee4fca 100644 --- a/solution/0700-0799/0791.Custom Sort String/README.md +++ b/solution/0700-0799/0791.Custom Sort String/README.md @@ -242,7 +242,9 @@ impl Solution { d[(c - b'a') as usize] = i; } let mut ans = s.chars().collect::>(); - ans.sort_by(|&a, &b| d[(a as u8 - 'a' as u8) as usize].cmp(&d[(b as u8 - 'a' as u8) as usize])); + ans.sort_by(|&a, &b| + d[((a as u8) - ('a' as u8)) as usize].cmp(&d[((b as u8) - ('a' as u8)) as usize]) + ); ans.into_iter().collect() } } @@ -264,7 +266,7 @@ impl Solution { } for i in 0..count.len() { for _ in 0..count[i] { - ans.push(char::from(b'a' + i as u8)); + ans.push(char::from(b'a' + (i as u8))); } } ans diff --git a/solution/0700-0799/0791.Custom Sort String/README_EN.md b/solution/0700-0799/0791.Custom Sort String/README_EN.md index c9fa02ae9ce5e..c25a13000998c 100644 --- a/solution/0700-0799/0791.Custom Sort String/README_EN.md +++ b/solution/0700-0799/0791.Custom Sort String/README_EN.md @@ -219,7 +219,9 @@ impl Solution { d[(c - b'a') as usize] = i; } let mut ans = s.chars().collect::>(); - ans.sort_by(|&a, &b| d[(a as u8 - 'a' as u8) as usize].cmp(&d[(b as u8 - 'a' as u8) as usize])); + ans.sort_by(|&a, &b| + d[((a as u8) - ('a' as u8)) as usize].cmp(&d[((b as u8) - ('a' as u8)) as usize]) + ); ans.into_iter().collect() } } @@ -241,7 +243,7 @@ impl Solution { } for i in 0..count.len() { for _ in 0..count[i] { - ans.push(char::from(b'a' + i as u8)); + ans.push(char::from(b'a' + (i as u8))); } } ans diff --git a/solution/0700-0799/0791.Custom Sort String/Solution.rs b/solution/0700-0799/0791.Custom Sort String/Solution.rs index 795056ddb3aad..6ec7d3f7e68ea 100644 --- a/solution/0700-0799/0791.Custom Sort String/Solution.rs +++ b/solution/0700-0799/0791.Custom Sort String/Solution.rs @@ -13,7 +13,7 @@ impl Solution { } for i in 0..count.len() { for _ in 0..count[i] { - ans.push(char::from(b'a' + i as u8)); + ans.push(char::from(b'a' + (i as u8))); } } ans diff --git a/solution/0700-0799/0797.All Paths From Source to Target/README.md b/solution/0700-0799/0797.All Paths From Source to Target/README.md index dc92ddbadab99..af7948935127a 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/README.md +++ b/solution/0700-0799/0797.All Paths From Source to Target/README.md @@ -257,7 +257,7 @@ impl Solution { res.push(path.clone()); } for j in graph[i].iter() { - Self::dfs(*j as usize, path, res, graph) + Self::dfs(*j as usize, path, res, graph); } path.pop(); } diff --git a/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md b/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md index b84eb35256656..3d97022674661 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md +++ b/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md @@ -241,7 +241,7 @@ impl Solution { res.push(path.clone()); } for j in graph[i].iter() { - Self::dfs(*j as usize, path, res, graph) + Self::dfs(*j as usize, path, res, graph); } path.pop(); } diff --git a/solution/0700-0799/0797.All Paths From Source to Target/Solution.rs b/solution/0700-0799/0797.All Paths From Source to Target/Solution.rs index 63392435135ab..064662cf2c22c 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/Solution.rs +++ b/solution/0700-0799/0797.All Paths From Source to Target/Solution.rs @@ -5,7 +5,7 @@ impl Solution { res.push(path.clone()); } for j in graph[i].iter() { - Self::dfs(*j as usize, path, res, graph) + Self::dfs(*j as usize, path, res, graph); } path.pop(); } diff --git a/solution/0700-0799/0799.Champagne Tower/README.md b/solution/0700-0799/0799.Champagne Tower/README.md index 64f12c440a110..8b21bc4cede4d 100644 --- a/solution/0700-0799/0799.Champagne Tower/README.md +++ b/solution/0700-0799/0799.Champagne Tower/README.md @@ -266,7 +266,7 @@ impl Solution { } row = next_row; } - 1f64.min(row[query_glass]) + (1f64).min(row[query_glass]) } } ``` diff --git a/solution/0700-0799/0799.Champagne Tower/README_EN.md b/solution/0700-0799/0799.Champagne Tower/README_EN.md index 5d09b2098d137..9e0bfa94a95f0 100644 --- a/solution/0700-0799/0799.Champagne Tower/README_EN.md +++ b/solution/0700-0799/0799.Champagne Tower/README_EN.md @@ -244,7 +244,7 @@ impl Solution { } row = next_row; } - 1f64.min(row[query_glass]) + (1f64).min(row[query_glass]) } } ``` diff --git a/solution/0700-0799/0799.Champagne Tower/Solution.rs b/solution/0700-0799/0799.Champagne Tower/Solution.rs index c1d4b2b962184..c4c5ff176c607 100644 --- a/solution/0700-0799/0799.Champagne Tower/Solution.rs +++ b/solution/0700-0799/0799.Champagne Tower/Solution.rs @@ -13,6 +13,6 @@ impl Solution { } row = next_row; } - 1f64.min(row[query_glass]) + (1f64).min(row[query_glass]) } } diff --git a/solution/0800-0899/0804.Unique Morse Code Words/README.md b/solution/0800-0899/0804.Unique Morse Code Words/README.md index b9cf58d1e027e..94813f3005af8 100644 --- a/solution/0800-0899/0804.Unique Morse Code Words/README.md +++ b/solution/0800-0899/0804.Unique Morse Code Words/README.md @@ -186,9 +186,32 @@ use std::collections::HashSet; impl Solution { pub fn unique_morse_representations(words: Vec) -> i32 { const codes: [&str; 26] = [ - ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", - "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", - "-.--", "--..", + ".-", + "-...", + "-.-.", + "-..", + ".", + "..-.", + "--.", + "....", + "..", + ".---", + "-.-", + ".-..", + "--", + "-.", + "---", + ".--.", + "--.-", + ".-.", + "...", + "-", + "..-", + "...-", + ".--", + "-..-", + "-.--", + "--..", ]; words .iter() diff --git a/solution/0800-0899/0804.Unique Morse Code Words/README_EN.md b/solution/0800-0899/0804.Unique Morse Code Words/README_EN.md index dd08e3511a5de..a5aa3f7bb7493 100644 --- a/solution/0800-0899/0804.Unique Morse Code Words/README_EN.md +++ b/solution/0800-0899/0804.Unique Morse Code Words/README_EN.md @@ -168,9 +168,32 @@ use std::collections::HashSet; impl Solution { pub fn unique_morse_representations(words: Vec) -> i32 { const codes: [&str; 26] = [ - ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", - "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", - "-.--", "--..", + ".-", + "-...", + "-.-.", + "-..", + ".", + "..-.", + "--.", + "....", + "..", + ".---", + "-.-", + ".-..", + "--", + "-.", + "---", + ".--.", + "--.-", + ".-.", + "...", + "-", + "..-", + "...-", + ".--", + "-..-", + "-.--", + "--..", ]; words .iter() diff --git a/solution/0800-0899/0804.Unique Morse Code Words/Solution.rs b/solution/0800-0899/0804.Unique Morse Code Words/Solution.rs index 11d434d341207..5fb35e4365e59 100644 --- a/solution/0800-0899/0804.Unique Morse Code Words/Solution.rs +++ b/solution/0800-0899/0804.Unique Morse Code Words/Solution.rs @@ -2,9 +2,32 @@ use std::collections::HashSet; impl Solution { pub fn unique_morse_representations(words: Vec) -> i32 { const codes: [&str; 26] = [ - ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", - "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", - "-.--", "--..", + ".-", + "-...", + "-.-.", + "-..", + ".", + "..-.", + "--.", + "....", + "..", + ".---", + "-.-", + ".-..", + "--", + "-.", + "---", + ".--.", + "--.-", + ".-.", + "...", + "-", + "..-", + "...-", + ".--", + "-..-", + "-.--", + "--..", ]; words .iter() diff --git a/solution/0800-0899/0819.Most Common Word/README.md b/solution/0800-0899/0819.Most Common Word/README.md index 289416860c80f..2b69ee38258f3 100644 --- a/solution/0800-0899/0819.Most Common Word/README.md +++ b/solution/0800-0899/0819.Most Common Word/README.md @@ -121,7 +121,7 @@ function mostCommonWord(paragraph: string, banned: string[]): string { ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn most_common_word(mut paragraph: String, banned: Vec) -> String { paragraph.make_ascii_lowercase(); @@ -137,8 +137,7 @@ impl Solution { map.into_iter() .max_by_key(|&(_, v)| v) .unwrap() - .0 - .to_string() + .0.to_string() } } ``` diff --git a/solution/0800-0899/0819.Most Common Word/README_EN.md b/solution/0800-0899/0819.Most Common Word/README_EN.md index f22e8381d5f21..7162cc5094d53 100644 --- a/solution/0800-0899/0819.Most Common Word/README_EN.md +++ b/solution/0800-0899/0819.Most Common Word/README_EN.md @@ -176,7 +176,7 @@ function mostCommonWord(paragraph: string, banned: string[]): string { ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn most_common_word(mut paragraph: String, banned: Vec) -> String { paragraph.make_ascii_lowercase(); @@ -192,8 +192,7 @@ impl Solution { map.into_iter() .max_by_key(|&(_, v)| v) .unwrap() - .0 - .to_string() + .0.to_string() } } ``` diff --git a/solution/0800-0899/0819.Most Common Word/Solution.rs b/solution/0800-0899/0819.Most Common Word/Solution.rs index 3d029a2d3e01b..648405c9d045a 100644 --- a/solution/0800-0899/0819.Most Common Word/Solution.rs +++ b/solution/0800-0899/0819.Most Common Word/Solution.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn most_common_word(mut paragraph: String, banned: Vec) -> String { paragraph.make_ascii_lowercase(); @@ -14,7 +14,6 @@ impl Solution { map.into_iter() .max_by_key(|&(_, v)| v) .unwrap() - .0 - .to_string() + .0.to_string() } } diff --git a/solution/0800-0899/0821.Shortest Distance to a Character/README.md b/solution/0800-0899/0821.Shortest Distance to a Character/README.md index ed38389aac5d3..610c3c26fa552 100644 --- a/solution/0800-0899/0821.Shortest Distance to a Character/README.md +++ b/solution/0800-0899/0821.Shortest Distance to a Character/README.md @@ -198,14 +198,14 @@ impl Solution { if s[i] == c { pre = i as i32; } - res[i] = i32::abs(i as i32 - pre); + res[i] = i32::abs((i as i32) - pre); } pre = i32::MAX; for i in (0..n).rev() { if s[i] == c { pre = i as i32; } - res[i] = res[i].min(i32::abs(i as i32 - pre)); + res[i] = res[i].min(i32::abs((i as i32) - pre)); } res } diff --git a/solution/0800-0899/0821.Shortest Distance to a Character/README_EN.md b/solution/0800-0899/0821.Shortest Distance to a Character/README_EN.md index a6cdaaea4e01f..e35a0e228b97b 100644 --- a/solution/0800-0899/0821.Shortest Distance to a Character/README_EN.md +++ b/solution/0800-0899/0821.Shortest Distance to a Character/README_EN.md @@ -175,14 +175,14 @@ impl Solution { if s[i] == c { pre = i as i32; } - res[i] = i32::abs(i as i32 - pre); + res[i] = i32::abs((i as i32) - pre); } pre = i32::MAX; for i in (0..n).rev() { if s[i] == c { pre = i as i32; } - res[i] = res[i].min(i32::abs(i as i32 - pre)); + res[i] = res[i].min(i32::abs((i as i32) - pre)); } res } diff --git a/solution/0800-0899/0821.Shortest Distance to a Character/Solution.rs b/solution/0800-0899/0821.Shortest Distance to a Character/Solution.rs index 6ae5b9f713f94..cc405464e12ac 100644 --- a/solution/0800-0899/0821.Shortest Distance to a Character/Solution.rs +++ b/solution/0800-0899/0821.Shortest Distance to a Character/Solution.rs @@ -9,14 +9,14 @@ impl Solution { if s[i] == c { pre = i as i32; } - res[i] = i32::abs(i as i32 - pre); + res[i] = i32::abs((i as i32) - pre); } pre = i32::MAX; for i in (0..n).rev() { if s[i] == c { pre = i as i32; } - res[i] = res[i].min(i32::abs(i as i32 - pre)); + res[i] = res[i].min(i32::abs((i as i32) - pre)); } res } diff --git a/solution/0800-0899/0827.Making A Large Island/README.md b/solution/0800-0899/0827.Making A Large Island/README.md index 1313dd47791d5..d0cfe4378783d 100644 --- a/solution/0800-0899/0827.Making A Large Island/README.md +++ b/solution/0800-0899/0827.Making A Large Island/README.md @@ -628,7 +628,7 @@ impl Solution { j: usize, grid: &Vec>, paths: &mut Vec<(usize, usize)>, - vis: &mut Vec>, + vis: &mut Vec> ) { let n = vis.len(); if vis[i][j] || grid[i][j] != 1 { diff --git a/solution/0800-0899/0827.Making A Large Island/README_EN.md b/solution/0800-0899/0827.Making A Large Island/README_EN.md index 0a114f0013136..65279c9011f69 100644 --- a/solution/0800-0899/0827.Making A Large Island/README_EN.md +++ b/solution/0800-0899/0827.Making A Large Island/README_EN.md @@ -566,7 +566,7 @@ impl Solution { j: usize, grid: &Vec>, paths: &mut Vec<(usize, usize)>, - vis: &mut Vec>, + vis: &mut Vec> ) { let n = vis.len(); if vis[i][j] || grid[i][j] != 1 { diff --git a/solution/0800-0899/0827.Making A Large Island/Solution.rs b/solution/0800-0899/0827.Making A Large Island/Solution.rs index 7e5b80e14d41e..284e99e93b3bc 100644 --- a/solution/0800-0899/0827.Making A Large Island/Solution.rs +++ b/solution/0800-0899/0827.Making A Large Island/Solution.rs @@ -5,7 +5,7 @@ impl Solution { j: usize, grid: &Vec>, paths: &mut Vec<(usize, usize)>, - vis: &mut Vec>, + vis: &mut Vec> ) { let n = vis.len(); if vis[i][j] || grid[i][j] != 1 { diff --git a/solution/0800-0899/0844.Backspace String Compare/README.md b/solution/0800-0899/0844.Backspace String Compare/README.md index 4627c5217a502..7f04a640469b3 100644 --- a/solution/0800-0899/0844.Backspace String Compare/README.md +++ b/solution/0800-0899/0844.Backspace String Compare/README.md @@ -285,7 +285,7 @@ impl Solution { } else { break; } - i -= 1 + i -= 1; } skip = 0; while j != 0 { @@ -296,7 +296,7 @@ impl Solution { } else { break; } - j -= 1 + j -= 1; } if i == 0 && j == 0 { break; diff --git a/solution/0800-0899/0844.Backspace String Compare/README_EN.md b/solution/0800-0899/0844.Backspace String Compare/README_EN.md index 956b68b6829a6..b25c8638fdd66 100644 --- a/solution/0800-0899/0844.Backspace String Compare/README_EN.md +++ b/solution/0800-0899/0844.Backspace String Compare/README_EN.md @@ -261,7 +261,7 @@ impl Solution { } else { break; } - i -= 1 + i -= 1; } skip = 0; while j != 0 { @@ -272,7 +272,7 @@ impl Solution { } else { break; } - j -= 1 + j -= 1; } if i == 0 && j == 0 { break; diff --git a/solution/0800-0899/0844.Backspace String Compare/Solution.rs b/solution/0800-0899/0844.Backspace String Compare/Solution.rs index 072b57e427a9a..5e4b79306fcc2 100644 --- a/solution/0800-0899/0844.Backspace String Compare/Solution.rs +++ b/solution/0800-0899/0844.Backspace String Compare/Solution.rs @@ -12,7 +12,7 @@ impl Solution { } else { break; } - i -= 1 + i -= 1; } skip = 0; while j != 0 { @@ -23,7 +23,7 @@ impl Solution { } else { break; } - j -= 1 + j -= 1; } if i == 0 && j == 0 { break; diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md index ee4e043edc159..d16604f00afc0 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md @@ -306,7 +306,7 @@ impl Solution { // Begin BFS while !q.is_empty() { let ((i, st), count) = q.pop_front().unwrap(); - if st == ((1 << n) - 1) { + if st == (1 << n) - 1 { return count; } // If the path has not been visited diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md index 396ae5a5cf448..6d5c04121980d 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md @@ -268,7 +268,7 @@ impl Solution { // Begin BFS while !q.is_empty() { let ((i, st), count) = q.pop_front().unwrap(); - if st == ((1 << n) - 1) { + if st == (1 << n) - 1 { return count; } // If the path has not been visited diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs index cc1396894a2c9..e7734fe9764dd 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.rs @@ -16,7 +16,7 @@ impl Solution { // Begin BFS while !q.is_empty() { let ((i, st), count) = q.pop_front().unwrap(); - if st == ((1 << n) - 1) { + if st == (1 << n) - 1 { return count; } // If the path has not been visited @@ -31,4 +31,4 @@ impl Solution { -1 } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0868.Binary Gap/README.md b/solution/0800-0899/0868.Binary Gap/README.md index 4eed09ea783a4..31b1104fb5900 100644 --- a/solution/0800-0899/0868.Binary Gap/README.md +++ b/solution/0800-0899/0868.Binary Gap/README.md @@ -124,7 +124,7 @@ impl Solution { let mut i = 0; let mut j = -1; while n != 0 { - if n & 1 == 1 { + if (n & 1) == 1 { if j != -1 { res = res.max(i - j); } diff --git a/solution/0800-0899/0868.Binary Gap/README_EN.md b/solution/0800-0899/0868.Binary Gap/README_EN.md index 3485eddf91382..eacd274fe0cea 100644 --- a/solution/0800-0899/0868.Binary Gap/README_EN.md +++ b/solution/0800-0899/0868.Binary Gap/README_EN.md @@ -111,7 +111,7 @@ impl Solution { let mut i = 0; let mut j = -1; while n != 0 { - if n & 1 == 1 { + if (n & 1) == 1 { if j != -1 { res = res.max(i - j); } diff --git a/solution/0800-0899/0868.Binary Gap/Solution.rs b/solution/0800-0899/0868.Binary Gap/Solution.rs index da589ff69d1a1..9cf793f690e83 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.rs +++ b/solution/0800-0899/0868.Binary Gap/Solution.rs @@ -4,7 +4,7 @@ impl Solution { let mut i = 0; let mut j = -1; while n != 0 { - if n & 1 == 1 { + if (n & 1) == 1 { if j != -1 { res = res.max(i - j); } diff --git a/solution/0800-0899/0872.Leaf-Similar Trees/README.md b/solution/0800-0899/0872.Leaf-Similar Trees/README.md index 7c987a389321f..f96dfbe665190 100644 --- a/solution/0800-0899/0872.Leaf-Similar Trees/README.md +++ b/solution/0800-0899/0872.Leaf-Similar Trees/README.md @@ -174,7 +174,10 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { #[allow(dead_code)] - pub fn leaf_similar(root1: Option>>, root2: Option>>) -> bool { + pub fn leaf_similar( + root1: Option>>, + root2: Option>> + ) -> bool { let mut one_vec: Vec = Vec::new(); let mut two_vec: Vec = Vec::new(); @@ -201,7 +204,8 @@ impl Solution { #[allow(dead_code)] fn is_leaf_node(node: &Option>>) -> bool { - node.as_ref().unwrap().borrow().left.is_none() && node.as_ref().unwrap().borrow().right.is_none() + node.as_ref().unwrap().borrow().left.is_none() && + node.as_ref().unwrap().borrow().right.is_none() } } ``` diff --git a/solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md b/solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md index fa05b2b44e9fd..240b5b15f2872 100644 --- a/solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md +++ b/solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md @@ -156,7 +156,10 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { #[allow(dead_code)] - pub fn leaf_similar(root1: Option>>, root2: Option>>) -> bool { + pub fn leaf_similar( + root1: Option>>, + root2: Option>> + ) -> bool { let mut one_vec: Vec = Vec::new(); let mut two_vec: Vec = Vec::new(); @@ -183,7 +186,8 @@ impl Solution { #[allow(dead_code)] fn is_leaf_node(node: &Option>>) -> bool { - node.as_ref().unwrap().borrow().left.is_none() && node.as_ref().unwrap().borrow().right.is_none() + node.as_ref().unwrap().borrow().left.is_none() && + node.as_ref().unwrap().borrow().right.is_none() } } ``` diff --git a/solution/0800-0899/0872.Leaf-Similar Trees/Solution.rs b/solution/0800-0899/0872.Leaf-Similar Trees/Solution.rs index 1e48b6308f484..5dad3ca599213 100644 --- a/solution/0800-0899/0872.Leaf-Similar Trees/Solution.rs +++ b/solution/0800-0899/0872.Leaf-Similar Trees/Solution.rs @@ -20,7 +20,10 @@ use std::rc::Rc; use std::cell::RefCell; impl Solution { #[allow(dead_code)] - pub fn leaf_similar(root1: Option>>, root2: Option>>) -> bool { + pub fn leaf_similar( + root1: Option>>, + root2: Option>> + ) -> bool { let mut one_vec: Vec = Vec::new(); let mut two_vec: Vec = Vec::new(); @@ -47,6 +50,7 @@ impl Solution { #[allow(dead_code)] fn is_leaf_node(node: &Option>>) -> bool { - node.as_ref().unwrap().borrow().left.is_none() && node.as_ref().unwrap().borrow().right.is_none() + node.as_ref().unwrap().borrow().left.is_none() && + node.as_ref().unwrap().borrow().right.is_none() } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md index a1d4c99b4ae68..ab0030c65b82e 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md @@ -169,7 +169,7 @@ impl Solution { let mut res = Vec::new(); for (k, v) in map { if v { - res.push(String::from(k)) + res.push(String::from(k)); } } res diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md index 6439787114f2a..740cf8e39e823 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md @@ -141,7 +141,7 @@ impl Solution { let mut res = Vec::new(); for (k, v) in map { if v { - res.push(String::from(k)) + res.push(String::from(k)); } } res diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.rs b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.rs index 66b873acd7d7e..fc427c4f1909e 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.rs +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.rs @@ -12,7 +12,7 @@ impl Solution { let mut res = Vec::new(); for (k, v) in map { if v { - res.push(String::from(k)) + res.push(String::from(k)); } } res diff --git a/solution/0800-0899/0886.Possible Bipartition/README.md b/solution/0800-0899/0886.Possible Bipartition/README.md index c446baab4b4b0..c629781bc4932 100644 --- a/solution/0800-0899/0886.Possible Bipartition/README.md +++ b/solution/0800-0899/0886.Possible Bipartition/README.md @@ -394,7 +394,7 @@ impl Solution { fn dfs(i: usize, v: usize, color: &mut Vec, g: &Vec>) -> bool { color[i] = v; for &j in (*g[i]).iter() { - if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) { + if color[j] == color[i] || (color[j] == 0 && Self::dfs(j, v ^ 3, color, g)) { return true; } } diff --git a/solution/0800-0899/0886.Possible Bipartition/README_EN.md b/solution/0800-0899/0886.Possible Bipartition/README_EN.md index 5eca2798f4fdd..bb328e34f8277 100644 --- a/solution/0800-0899/0886.Possible Bipartition/README_EN.md +++ b/solution/0800-0899/0886.Possible Bipartition/README_EN.md @@ -319,7 +319,7 @@ impl Solution { fn dfs(i: usize, v: usize, color: &mut Vec, g: &Vec>) -> bool { color[i] = v; for &j in (*g[i]).iter() { - if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) { + if color[j] == color[i] || (color[j] == 0 && Self::dfs(j, v ^ 3, color, g)) { return true; } } diff --git a/solution/0800-0899/0886.Possible Bipartition/Solution.rs b/solution/0800-0899/0886.Possible Bipartition/Solution.rs index 2920b4fc3a833..356b433233c23 100644 --- a/solution/0800-0899/0886.Possible Bipartition/Solution.rs +++ b/solution/0800-0899/0886.Possible Bipartition/Solution.rs @@ -2,7 +2,7 @@ impl Solution { fn dfs(i: usize, v: usize, color: &mut Vec, g: &Vec>) -> bool { color[i] = v; for &j in (*g[i]).iter() { - if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) { + if color[j] == color[i] || (color[j] == 0 && Self::dfs(j, v ^ 3, color, g)) { return true; } } diff --git a/solution/0800-0899/0894.All Possible Full Binary Trees/README.md b/solution/0800-0899/0894.All Possible Full Binary Trees/README.md index 61daf2d9e3ff0..d56b0d1cc8b3b 100644 --- a/solution/0800-0899/0894.All Possible Full Binary Trees/README.md +++ b/solution/0800-0899/0894.All Possible Full Binary Trees/README.md @@ -193,7 +193,10 @@ public: // } impl TreeNode { - pub fn new_with_node(left: Option>>, right: Option>>) -> Self { + pub fn new_with_node( + left: Option>>, + right: Option>> + ) -> Self { Self { val: 0, left, @@ -212,7 +215,10 @@ impl Solution { } #[allow(dead_code)] - fn dfs(n: i32, record_vec: &mut Vec>>>>) -> Vec>>> { + fn dfs( + n: i32, + record_vec: &mut Vec>>>> + ) -> Vec>>> { if record_vec[n as usize].len() != 0 { return record_vec[n as usize].clone(); } @@ -230,7 +236,13 @@ impl Solution { for left in Self::dfs(i, record_vec) { for right in Self::dfs(j, record_vec) { // Construct the ret vector - ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))))); + ret_vec.push( + Some( + Rc::new( + RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())) + ) + ) + ); } } } diff --git a/solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md b/solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md index 94b5a71784ea0..ef4a244348981 100644 --- a/solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md +++ b/solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md @@ -173,7 +173,10 @@ public: // } impl TreeNode { - pub fn new_with_node(left: Option>>, right: Option>>) -> Self { + pub fn new_with_node( + left: Option>>, + right: Option>> + ) -> Self { Self { val: 0, left, @@ -192,7 +195,10 @@ impl Solution { } #[allow(dead_code)] - fn dfs(n: i32, record_vec: &mut Vec>>>>) -> Vec>>> { + fn dfs( + n: i32, + record_vec: &mut Vec>>>> + ) -> Vec>>> { if record_vec[n as usize].len() != 0 { return record_vec[n as usize].clone(); } @@ -210,7 +216,13 @@ impl Solution { for left in Self::dfs(i, record_vec) { for right in Self::dfs(j, record_vec) { // Construct the ret vector - ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))))); + ret_vec.push( + Some( + Rc::new( + RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())) + ) + ) + ); } } } diff --git a/solution/0800-0899/0894.All Possible Full Binary Trees/Solution.rs b/solution/0800-0899/0894.All Possible Full Binary Trees/Solution.rs index e9c686c320782..16d5b1fafa8f2 100644 --- a/solution/0800-0899/0894.All Possible Full Binary Trees/Solution.rs +++ b/solution/0800-0899/0894.All Possible Full Binary Trees/Solution.rs @@ -18,7 +18,10 @@ // } impl TreeNode { - pub fn new_with_node(left: Option>>, right: Option>>) -> Self { + pub fn new_with_node( + left: Option>>, + right: Option>> + ) -> Self { Self { val: 0, left, @@ -37,7 +40,10 @@ impl Solution { } #[allow(dead_code)] - fn dfs(n: i32, record_vec: &mut Vec>>>>) -> Vec>>> { + fn dfs( + n: i32, + record_vec: &mut Vec>>>> + ) -> Vec>>> { if record_vec[n as usize].len() != 0 { return record_vec[n as usize].clone(); } @@ -55,7 +61,13 @@ impl Solution { for left in Self::dfs(i, record_vec) { for right in Self::dfs(j, record_vec) { // Construct the ret vector - ret_vec.push(Some(Rc::new(RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))))); + ret_vec.push( + Some( + Rc::new( + RefCell::new(TreeNode::new_with_node(left.clone(), right.clone())) + ) + ) + ); } } } @@ -64,4 +76,4 @@ impl Solution { record_vec[n as usize].clone() } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0901.Online Stock Span/README.md b/solution/0900-0999/0901.Online Stock Span/README.md index 3ffc765e48bf8..bcc61c2cbc580 100644 --- a/solution/0900-0999/0901.Online Stock Span/README.md +++ b/solution/0900-0999/0901.Online Stock Span/README.md @@ -218,7 +218,6 @@ struct StockSpanner { stk: VecDeque<(i32, i32)>, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -226,7 +225,7 @@ struct StockSpanner { impl StockSpanner { fn new() -> Self { Self { - stk: vec![(i32::MAX, -1)].into_iter().collect() + stk: vec![(i32::MAX, -1)].into_iter().collect(), } } @@ -238,9 +237,7 @@ impl StockSpanner { self.stk.push_back((price, cnt)); cnt } -} - -/** +}/** * Your StockSpanner object will be instantiated and called as such: * let obj = StockSpanner::new(); * let ret_1: i32 = obj.next(price); diff --git a/solution/0900-0999/0901.Online Stock Span/README_EN.md b/solution/0900-0999/0901.Online Stock Span/README_EN.md index 7e645d17a1a88..46f7fc8ac0f07 100644 --- a/solution/0900-0999/0901.Online Stock Span/README_EN.md +++ b/solution/0900-0999/0901.Online Stock Span/README_EN.md @@ -207,7 +207,6 @@ struct StockSpanner { stk: VecDeque<(i32, i32)>, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -215,7 +214,7 @@ struct StockSpanner { impl StockSpanner { fn new() -> Self { Self { - stk: vec![(i32::MAX, -1)].into_iter().collect() + stk: vec![(i32::MAX, -1)].into_iter().collect(), } } @@ -227,9 +226,7 @@ impl StockSpanner { self.stk.push_back((price, cnt)); cnt } -} - -/** +}/** * Your StockSpanner object will be instantiated and called as such: * let obj = StockSpanner::new(); * let ret_1: i32 = obj.next(price); diff --git a/solution/0900-0999/0901.Online Stock Span/Solution.rs b/solution/0900-0999/0901.Online Stock Span/Solution.rs index e9691075807e6..2f650c64d8432 100644 --- a/solution/0900-0999/0901.Online Stock Span/Solution.rs +++ b/solution/0900-0999/0901.Online Stock Span/Solution.rs @@ -3,7 +3,6 @@ struct StockSpanner { stk: VecDeque<(i32, i32)>, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. @@ -11,7 +10,7 @@ struct StockSpanner { impl StockSpanner { fn new() -> Self { Self { - stk: vec![(i32::MAX, -1)].into_iter().collect() + stk: vec![(i32::MAX, -1)].into_iter().collect(), } } @@ -23,10 +22,8 @@ impl StockSpanner { self.stk.push_back((price, cnt)); cnt } -} - -/** +}/** * Your StockSpanner object will be instantiated and called as such: * let obj = StockSpanner::new(); * let ret_1: i32 = obj.next(price); - */ \ No newline at end of file + */ diff --git a/solution/0900-0999/0905.Sort Array By Parity/README.md b/solution/0900-0999/0905.Sort Array By Parity/README.md index fcf1fb03d754d..305e8b69b58fe 100644 --- a/solution/0900-0999/0905.Sort Array By Parity/README.md +++ b/solution/0900-0999/0905.Sort Array By Parity/README.md @@ -110,10 +110,10 @@ impl Solution { pub fn sort_array_by_parity(mut nums: Vec) -> Vec { let (mut l, mut r) = (0, nums.len() - 1); while l < r { - while l < r && nums[l] & 1 == 0 { + while l < r && (nums[l] & 1) == 0 { l += 1; } - while l < r && nums[r] & 1 == 1 { + while l < r && (nums[r] & 1) == 1 { r -= 1; } nums.swap(l, r); diff --git a/solution/0900-0999/0905.Sort Array By Parity/README_EN.md b/solution/0900-0999/0905.Sort Array By Parity/README_EN.md index 264d10bb377e7..cfd10228d78f7 100644 --- a/solution/0900-0999/0905.Sort Array By Parity/README_EN.md +++ b/solution/0900-0999/0905.Sort Array By Parity/README_EN.md @@ -98,10 +98,10 @@ impl Solution { pub fn sort_array_by_parity(mut nums: Vec) -> Vec { let (mut l, mut r) = (0, nums.len() - 1); while l < r { - while l < r && nums[l] & 1 == 0 { + while l < r && (nums[l] & 1) == 0 { l += 1; } - while l < r && nums[r] & 1 == 1 { + while l < r && (nums[r] & 1) == 1 { r -= 1; } nums.swap(l, r); diff --git a/solution/0900-0999/0905.Sort Array By Parity/Solution.rs b/solution/0900-0999/0905.Sort Array By Parity/Solution.rs index ea3e81396a721..9e4f92e97071c 100644 --- a/solution/0900-0999/0905.Sort Array By Parity/Solution.rs +++ b/solution/0900-0999/0905.Sort Array By Parity/Solution.rs @@ -2,10 +2,10 @@ impl Solution { pub fn sort_array_by_parity(mut nums: Vec) -> Vec { let (mut l, mut r) = (0, nums.len() - 1); while l < r { - while l < r && nums[l] & 1 == 0 { + while l < r && (nums[l] & 1) == 0 { l += 1; } - while l < r && nums[r] & 1 == 1 { + while l < r && (nums[r] & 1) == 1 { r -= 1; } nums.swap(l, r); diff --git a/solution/0900-0999/0907.Sum of Subarray Minimums/README.md b/solution/0900-0999/0907.Sum of Subarray Minimums/README.md index 0958e85d6e890..8010374edabd2 100644 --- a/solution/0900-0999/0907.Sum of Subarray Minimums/README.md +++ b/solution/0900-0999/0907.Sum of Subarray Minimums/README.md @@ -186,7 +186,7 @@ public: ### **Rust** ```rust -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { pub fn sum_subarray_mins(arr: Vec) -> i32 { @@ -225,11 +225,13 @@ impl Solution { // Traverse the array, to find the sum for i in 0..n { - ret += ((right[i] - i as i32) * (i as i32 - left[i])) as i64 * arr[i] as i64 % MOD; + ret += + ((((right[i] - (i as i32)) * ((i as i32) - left[i])) as i64) * (arr[i] as i64)) % + MOD; ret %= MOD; } - (ret % MOD as i64) as i32 + (ret % (MOD as i64)) as i32 } } ``` diff --git a/solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md b/solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md index 42329a8c4a1e3..12deebfce4271 100644 --- a/solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md +++ b/solution/0900-0999/0907.Sum of Subarray Minimums/README_EN.md @@ -186,7 +186,7 @@ public: ### **Rust** ```rust -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { pub fn sum_subarray_mins(arr: Vec) -> i32 { @@ -225,11 +225,13 @@ impl Solution { // Traverse the array, to find the sum for i in 0..n { - ret += ((right[i] - i as i32) * (i as i32 - left[i])) as i64 * arr[i] as i64 % MOD; + ret += + ((((right[i] - (i as i32)) * ((i as i32) - left[i])) as i64) * (arr[i] as i64)) % + MOD; ret %= MOD; } - (ret % MOD as i64) as i32 + (ret % (MOD as i64)) as i32 } } ``` diff --git a/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.rs b/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.rs index 4dbf07902e335..88a984f333273 100644 --- a/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.rs +++ b/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.rs @@ -1,4 +1,4 @@ -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { pub fn sum_subarray_mins(arr: Vec) -> i32 { @@ -37,10 +37,12 @@ impl Solution { // Traverse the array, to find the sum for i in 0..n { - ret += ((right[i] - i as i32) * (i as i32 - left[i])) as i64 * arr[i] as i64 % MOD; + ret += + ((((right[i] - (i as i32)) * ((i as i32) - left[i])) as i64) * (arr[i] as i64)) % + MOD; ret %= MOD; } - (ret % MOD as i64) as i32 + (ret % (MOD as i64)) as i32 } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0908.Smallest Range I/README.md b/solution/0900-0999/0908.Smallest Range I/README.md index eeec21832709c..5562b86541cae 100644 --- a/solution/0900-0999/0908.Smallest Range I/README.md +++ b/solution/0900-0999/0908.Smallest Range I/README.md @@ -128,7 +128,7 @@ impl Solution { pub fn smallest_range_i(nums: Vec, k: i32) -> i32 { let max = nums.iter().max().unwrap(); let min = nums.iter().min().unwrap(); - 0.max(max - min - k * 2) + (0).max(max - min - k * 2) } } ``` diff --git a/solution/0900-0999/0908.Smallest Range I/README_EN.md b/solution/0900-0999/0908.Smallest Range I/README_EN.md index b8f5ebd731771..cd5169e80d438 100644 --- a/solution/0900-0999/0908.Smallest Range I/README_EN.md +++ b/solution/0900-0999/0908.Smallest Range I/README_EN.md @@ -118,7 +118,7 @@ impl Solution { pub fn smallest_range_i(nums: Vec, k: i32) -> i32 { let max = nums.iter().max().unwrap(); let min = nums.iter().min().unwrap(); - 0.max(max - min - k * 2) + (0).max(max - min - k * 2) } } ``` diff --git a/solution/0900-0999/0908.Smallest Range I/Solution.rs b/solution/0900-0999/0908.Smallest Range I/Solution.rs index 5cc2e119fe279..8a839cb5412cd 100644 --- a/solution/0900-0999/0908.Smallest Range I/Solution.rs +++ b/solution/0900-0999/0908.Smallest Range I/Solution.rs @@ -2,6 +2,6 @@ impl Solution { pub fn smallest_range_i(nums: Vec, k: i32) -> i32 { let max = nums.iter().max().unwrap(); let min = nums.iter().min().unwrap(); - 0.max(max - min - k * 2) + (0).max(max - min - k * 2) } } diff --git a/solution/0900-0999/0920.Number of Music Playlists/README.md b/solution/0900-0999/0920.Number of Music Playlists/README.md index 632dead07e1f2..e122d441a3dd0 100644 --- a/solution/0900-0999/0920.Number of Music Playlists/README.md +++ b/solution/0900-0999/0920.Number of Music Playlists/README.md @@ -220,16 +220,16 @@ impl Solution { for j in 1..=n as usize { // Choose the song that has not been chosen before // We have n - (j - 1) songs to choose - dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64; + dp[i][j] += dp[i - 1][j - 1] * ((n - ((j as i32) - 1)) as i64); // Choose the song that has been chosen before // We have j - k songs to choose if j > k - if j as i32 > k { - dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64; + if (j as i32) > k { + dp[i][j] += dp[i - 1][j] * (((j as i32) - k) as i64); } // Update dp[i][j] - dp[i][j] %= (1e9 as i32 + 7) as i64; + dp[i][j] %= ((1e9 as i32) + 7) as i64; } } diff --git a/solution/0900-0999/0920.Number of Music Playlists/README_EN.md b/solution/0900-0999/0920.Number of Music Playlists/README_EN.md index c1635bb937b8e..09632b1443266 100644 --- a/solution/0900-0999/0920.Number of Music Playlists/README_EN.md +++ b/solution/0900-0999/0920.Number of Music Playlists/README_EN.md @@ -210,16 +210,16 @@ impl Solution { for j in 1..=n as usize { // Choose the song that has not been chosen before // We have n - (j - 1) songs to choose - dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64; + dp[i][j] += dp[i - 1][j - 1] * ((n - ((j as i32) - 1)) as i64); // Choose the song that has been chosen before // We have j - k songs to choose if j > k - if j as i32 > k { - dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64; + if (j as i32) > k { + dp[i][j] += dp[i - 1][j] * (((j as i32) - k) as i64); } // Update dp[i][j] - dp[i][j] %= (1e9 as i32 + 7) as i64; + dp[i][j] %= ((1e9 as i32) + 7) as i64; } } diff --git a/solution/0900-0999/0920.Number of Music Playlists/Solution.rs b/solution/0900-0999/0920.Number of Music Playlists/Solution.rs index 4322a0caf3490..67fc2eaf080b2 100644 --- a/solution/0900-0999/0920.Number of Music Playlists/Solution.rs +++ b/solution/0900-0999/0920.Number of Music Playlists/Solution.rs @@ -11,19 +11,19 @@ impl Solution { for j in 1..=n as usize { // Choose the song that has not been chosen before // We have n - (j - 1) songs to choose - dp[i][j] += dp[i - 1][j - 1] * ((n - (j as i32 - 1))) as i64; - + dp[i][j] += dp[i - 1][j - 1] * ((n - ((j as i32) - 1)) as i64); + // Choose the song that has been chosen before // We have j - k songs to choose if j > k - if j as i32 > k { - dp[i][j] += dp[i - 1][j] * (j as i32 - k) as i64; + if (j as i32) > k { + dp[i][j] += dp[i - 1][j] * (((j as i32) - k) as i64); } // Update dp[i][j] - dp[i][j] %= (1e9 as i32 + 7) as i64; + dp[i][j] %= ((1e9 as i32) + 7) as i64; } } dp[goal as usize][n as usize] as i32 } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0929.Unique Email Addresses/README.md b/solution/0900-0999/0929.Unique Email Addresses/README.md index 73723c102030f..8dd3246590190 100644 --- a/solution/0900-0999/0929.Unique Email Addresses/README.md +++ b/solution/0900-0999/0929.Unique Email Addresses/README.md @@ -219,7 +219,7 @@ impl Solution { if c == b'+' { break; } - s.push(c as char) + s.push(c as char); } s.push('@'); s.push_str(res[1]); diff --git a/solution/0900-0999/0929.Unique Email Addresses/README_EN.md b/solution/0900-0999/0929.Unique Email Addresses/README_EN.md index be8381fd193c6..7c6019d3e9635 100644 --- a/solution/0900-0999/0929.Unique Email Addresses/README_EN.md +++ b/solution/0900-0999/0929.Unique Email Addresses/README_EN.md @@ -207,7 +207,7 @@ impl Solution { if c == b'+' { break; } - s.push(c as char) + s.push(c as char); } s.push('@'); s.push_str(res[1]); diff --git a/solution/0900-0999/0929.Unique Email Addresses/Solution.rs b/solution/0900-0999/0929.Unique Email Addresses/Solution.rs index 5e23ec9f472e1..32f5442f5ca04 100644 --- a/solution/0900-0999/0929.Unique Email Addresses/Solution.rs +++ b/solution/0900-0999/0929.Unique Email Addresses/Solution.rs @@ -12,7 +12,7 @@ impl Solution { if c == b'+' { break; } - s.push(c as char) + s.push(c as char); } s.push('@'); s.push_str(res[1]); diff --git a/solution/0900-0999/0933.Number of Recent Calls/README.md b/solution/0900-0999/0933.Number of Recent Calls/README.md index 374042b90c6b4..ae7fe3f7ef85b 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/README.md +++ b/solution/0900-0999/0933.Number of Recent Calls/README.md @@ -326,35 +326,31 @@ class RecentCounter { ```rust use std::collections::VecDeque; struct RecentCounter { - queue: VecDeque + queue: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RecentCounter { - fn new() -> Self { Self { - queue: VecDeque::new() + queue: VecDeque::new(), } } fn ping(&mut self, t: i32) -> i32 { self.queue.push_back(t); while let Some(&v) = self.queue.front() { - if v >= t - 3000 { + if v >= t - 3000 { break; } self.queue.pop_front(); } self.queue.len() as i32 } -} - -/** +}/** * Your RecentCounter object will be instantiated and called as such: * let obj = RecentCounter::new(); * let ret_1: i32 = obj.ping(t); diff --git a/solution/0900-0999/0933.Number of Recent Calls/README_EN.md b/solution/0900-0999/0933.Number of Recent Calls/README_EN.md index 441ccd78fe7ae..2102260417400 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/README_EN.md +++ b/solution/0900-0999/0933.Number of Recent Calls/README_EN.md @@ -327,35 +327,31 @@ class RecentCounter { ```rust use std::collections::VecDeque; struct RecentCounter { - queue: VecDeque + queue: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RecentCounter { - fn new() -> Self { Self { - queue: VecDeque::new() + queue: VecDeque::new(), } } fn ping(&mut self, t: i32) -> i32 { self.queue.push_back(t); while let Some(&v) = self.queue.front() { - if v >= t - 3000 { + if v >= t - 3000 { break; } self.queue.pop_front(); } self.queue.len() as i32 } -} - -/** +}/** * Your RecentCounter object will be instantiated and called as such: * let obj = RecentCounter::new(); * let ret_1: i32 = obj.ping(t); diff --git a/solution/0900-0999/0933.Number of Recent Calls/Solution.rs b/solution/0900-0999/0933.Number of Recent Calls/Solution.rs index 5b4e85f2606be..aca9fc7fd11d7 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/Solution.rs +++ b/solution/0900-0999/0933.Number of Recent Calls/Solution.rs @@ -1,35 +1,31 @@ use std::collections::VecDeque; struct RecentCounter { - queue: VecDeque + queue: VecDeque, } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl RecentCounter { - fn new() -> Self { Self { - queue: VecDeque::new() + queue: VecDeque::new(), } } fn ping(&mut self, t: i32) -> i32 { self.queue.push_back(t); while let Some(&v) = self.queue.front() { - if v >= t - 3000 { + if v >= t - 3000 { break; } self.queue.pop_front(); } self.queue.len() as i32 } -} - -/** +}/** * Your RecentCounter object will be instantiated and called as such: * let obj = RecentCounter::new(); * let ret_1: i32 = obj.ping(t); - */ \ No newline at end of file + */ diff --git a/solution/0900-0999/0937.Reorder Data in Log Files/README.md b/solution/0900-0999/0937.Reorder Data in Log Files/README.md index 8b5d04ffb3736..a7d5dd3f6c221 100644 --- a/solution/0900-0999/0937.Reorder Data in Log Files/README.md +++ b/solution/0900-0999/0937.Reorder Data in Log Files/README.md @@ -140,10 +140,12 @@ impl Solution { logs.sort_by(|s1, s2| { let (start1, content1) = s1.split_once(' ').unwrap(); let (start2, content2) = s2.split_once(' ').unwrap(); - match ( - content1.chars().nth(0).unwrap().is_digit(10), - content2.chars().nth(0).unwrap().is_digit(10), - ) { + match + ( + content1.chars().nth(0).unwrap().is_digit(10), + content2.chars().nth(0).unwrap().is_digit(10), + ) + { (true, true) => std::cmp::Ordering::Equal, (true, false) => std::cmp::Ordering::Greater, (false, true) => std::cmp::Ordering::Less, diff --git a/solution/0900-0999/0937.Reorder Data in Log Files/README_EN.md b/solution/0900-0999/0937.Reorder Data in Log Files/README_EN.md index e4e1a4a01ca76..a9e91c44a5d00 100644 --- a/solution/0900-0999/0937.Reorder Data in Log Files/README_EN.md +++ b/solution/0900-0999/0937.Reorder Data in Log Files/README_EN.md @@ -128,10 +128,12 @@ impl Solution { logs.sort_by(|s1, s2| { let (start1, content1) = s1.split_once(' ').unwrap(); let (start2, content2) = s2.split_once(' ').unwrap(); - match ( - content1.chars().nth(0).unwrap().is_digit(10), - content2.chars().nth(0).unwrap().is_digit(10), - ) { + match + ( + content1.chars().nth(0).unwrap().is_digit(10), + content2.chars().nth(0).unwrap().is_digit(10), + ) + { (true, true) => std::cmp::Ordering::Equal, (true, false) => std::cmp::Ordering::Greater, (false, true) => std::cmp::Ordering::Less, diff --git a/solution/0900-0999/0937.Reorder Data in Log Files/Solution.rs b/solution/0900-0999/0937.Reorder Data in Log Files/Solution.rs index e094984c3ecc0..124f9ac142690 100644 --- a/solution/0900-0999/0937.Reorder Data in Log Files/Solution.rs +++ b/solution/0900-0999/0937.Reorder Data in Log Files/Solution.rs @@ -3,10 +3,12 @@ impl Solution { logs.sort_by(|s1, s2| { let (start1, content1) = s1.split_once(' ').unwrap(); let (start2, content2) = s2.split_once(' ').unwrap(); - match ( - content1.chars().nth(0).unwrap().is_digit(10), - content2.chars().nth(0).unwrap().is_digit(10), - ) { + match + ( + content1.chars().nth(0).unwrap().is_digit(10), + content2.chars().nth(0).unwrap().is_digit(10), + ) + { (true, true) => std::cmp::Ordering::Equal, (true, false) => std::cmp::Ordering::Greater, (false, true) => std::cmp::Ordering::Less, diff --git a/solution/0900-0999/0940.Distinct Subsequences II/README.md b/solution/0900-0999/0940.Distinct Subsequences II/README.md index aa52690a23a39..c158cb9d74cfd 100644 --- a/solution/0900-0999/0940.Distinct Subsequences II/README.md +++ b/solution/0900-0999/0940.Distinct Subsequences II/README.md @@ -281,18 +281,23 @@ function distinctSubseqII(s: string): number { ```rust impl Solution { pub fn distinct_subseq_ii(s: String) -> i32 { - const MOD: i32 = 1e9 as i32 + 7; + const MOD: i32 = (1e9 as i32) + 7; let mut dp = [0; 26]; for u in s.as_bytes() { let i = (u - &b'a') as usize; - dp[i] = { - let mut sum = 0; - dp.iter().for_each(|&v| sum = (sum + v) % MOD); - sum - } + 1; + dp[i] = + ({ + let mut sum = 0; + dp.iter().for_each(|&v| { + sum = (sum + v) % MOD; + }); + sum + }) + 1; } let mut res = 0; - dp.iter().for_each(|&v| res = (res + v) % MOD); + dp.iter().for_each(|&v| { + res = (res + v) % MOD; + }); res } } diff --git a/solution/0900-0999/0940.Distinct Subsequences II/README_EN.md b/solution/0900-0999/0940.Distinct Subsequences II/README_EN.md index 9bd200ca4aefa..4cbe59a4a9924 100644 --- a/solution/0900-0999/0940.Distinct Subsequences II/README_EN.md +++ b/solution/0900-0999/0940.Distinct Subsequences II/README_EN.md @@ -243,18 +243,23 @@ function distinctSubseqII(s: string): number { ```rust impl Solution { pub fn distinct_subseq_ii(s: String) -> i32 { - const MOD: i32 = 1e9 as i32 + 7; + const MOD: i32 = (1e9 as i32) + 7; let mut dp = [0; 26]; for u in s.as_bytes() { let i = (u - &b'a') as usize; - dp[i] = { - let mut sum = 0; - dp.iter().for_each(|&v| sum = (sum + v) % MOD); - sum - } + 1; + dp[i] = + ({ + let mut sum = 0; + dp.iter().for_each(|&v| { + sum = (sum + v) % MOD; + }); + sum + }) + 1; } let mut res = 0; - dp.iter().for_each(|&v| res = (res + v) % MOD); + dp.iter().for_each(|&v| { + res = (res + v) % MOD; + }); res } } diff --git a/solution/0900-0999/0940.Distinct Subsequences II/Solution.rs b/solution/0900-0999/0940.Distinct Subsequences II/Solution.rs index 2e066edbb0e85..576d3554dc839 100644 --- a/solution/0900-0999/0940.Distinct Subsequences II/Solution.rs +++ b/solution/0900-0999/0940.Distinct Subsequences II/Solution.rs @@ -1,17 +1,22 @@ impl Solution { pub fn distinct_subseq_ii(s: String) -> i32 { - const MOD: i32 = 1e9 as i32 + 7; + const MOD: i32 = (1e9 as i32) + 7; let mut dp = [0; 26]; for u in s.as_bytes() { let i = (u - &b'a') as usize; - dp[i] = { - let mut sum = 0; - dp.iter().for_each(|&v| sum = (sum + v) % MOD); - sum - } + 1; + dp[i] = + ({ + let mut sum = 0; + dp.iter().for_each(|&v| { + sum = (sum + v) % MOD; + }); + sum + }) + 1; } let mut res = 0; - dp.iter().for_each(|&v| res = (res + v) % MOD); + dp.iter().for_each(|&v| { + res = (res + v) % MOD; + }); res } } diff --git a/solution/0900-0999/0942.DI String Match/README.md b/solution/0900-0999/0942.DI String Match/README.md index 9e8f1c460c378..b7933d844bef5 100644 --- a/solution/0900-0999/0942.DI String Match/README.md +++ b/solution/0900-0999/0942.DI String Match/README.md @@ -171,13 +171,15 @@ impl Solution { let mut res = Vec::with_capacity(n + 1); let (mut low, mut high) = (-1, (n + 1) as i32); for i in 0..n { - res.push(if s[i] == b'I' { - low += 1; - low - } else { - high -= 1; - high - }); + res.push( + if s[i] == b'I' { + low += 1; + low + } else { + high -= 1; + high + } + ); } res.push(low + 1); res diff --git a/solution/0900-0999/0942.DI String Match/README_EN.md b/solution/0900-0999/0942.DI String Match/README_EN.md index 22d36a314a759..46c01611ee730 100644 --- a/solution/0900-0999/0942.DI String Match/README_EN.md +++ b/solution/0900-0999/0942.DI String Match/README_EN.md @@ -149,13 +149,15 @@ impl Solution { let mut res = Vec::with_capacity(n + 1); let (mut low, mut high) = (-1, (n + 1) as i32); for i in 0..n { - res.push(if s[i] == b'I' { - low += 1; - low - } else { - high -= 1; - high - }); + res.push( + if s[i] == b'I' { + low += 1; + low + } else { + high -= 1; + high + } + ); } res.push(low + 1); res diff --git a/solution/0900-0999/0942.DI String Match/Solution.rs b/solution/0900-0999/0942.DI String Match/Solution.rs index 3b1d3b05b7987..0e439f5068053 100644 --- a/solution/0900-0999/0942.DI String Match/Solution.rs +++ b/solution/0900-0999/0942.DI String Match/Solution.rs @@ -5,13 +5,15 @@ impl Solution { let mut res = Vec::with_capacity(n + 1); let (mut low, mut high) = (-1, (n + 1) as i32); for i in 0..n { - res.push(if s[i] == b'I' { - low += 1; - low - } else { - high -= 1; - high - }); + res.push( + if s[i] == b'I' { + low += 1; + low + } else { + high -= 1; + high + } + ); } res.push(low + 1); res diff --git a/solution/0900-0999/0953.Verifying an Alien Dictionary/README.md b/solution/0900-0999/0953.Verifying an Alien Dictionary/README.md index e8affd002fae6..34394252400c9 100644 --- a/solution/0900-0999/0953.Verifying an Alien Dictionary/README.md +++ b/solution/0900-0999/0953.Verifying an Alien Dictionary/README.md @@ -204,9 +204,13 @@ impl Solution { pub fn is_alien_sorted(words: Vec, order: String) -> bool { let n = words.len(); let mut map = HashMap::new(); - order.as_bytes().iter().enumerate().for_each(|(i, &v)| { - map.insert(v, i); - }); + order + .as_bytes() + .iter() + .enumerate() + .for_each(|(i, &v)| { + map.insert(v, i); + }); for i in 1..n { let s1 = words[i - 1].as_bytes(); let s2 = words[i].as_bytes(); diff --git a/solution/0900-0999/0953.Verifying an Alien Dictionary/README_EN.md b/solution/0900-0999/0953.Verifying an Alien Dictionary/README_EN.md index eb310ef7b2645..c23bb15360bc7 100644 --- a/solution/0900-0999/0953.Verifying an Alien Dictionary/README_EN.md +++ b/solution/0900-0999/0953.Verifying an Alien Dictionary/README_EN.md @@ -194,9 +194,13 @@ impl Solution { pub fn is_alien_sorted(words: Vec, order: String) -> bool { let n = words.len(); let mut map = HashMap::new(); - order.as_bytes().iter().enumerate().for_each(|(i, &v)| { - map.insert(v, i); - }); + order + .as_bytes() + .iter() + .enumerate() + .for_each(|(i, &v)| { + map.insert(v, i); + }); for i in 1..n { let s1 = words[i - 1].as_bytes(); let s2 = words[i].as_bytes(); diff --git a/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.rs b/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.rs index fc4e9b1b4f9b3..e23f47ce76eb1 100644 --- a/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.rs +++ b/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.rs @@ -3,9 +3,13 @@ impl Solution { pub fn is_alien_sorted(words: Vec, order: String) -> bool { let n = words.len(); let mut map = HashMap::new(); - order.as_bytes().iter().enumerate().for_each(|(i, &v)| { - map.insert(v, i); - }); + order + .as_bytes() + .iter() + .enumerate() + .for_each(|(i, &v)| { + map.insert(v, i); + }); for i in 1..n { let s1 = words[i - 1].as_bytes(); let s2 = words[i].as_bytes(); diff --git a/solution/0900-0999/0969.Pancake Sorting/README.md b/solution/0900-0999/0969.Pancake Sorting/README.md index fc8ecfe87389d..c1603bd0d270e 100644 --- a/solution/0900-0999/0969.Pancake Sorting/README.md +++ b/solution/0900-0999/0969.Pancake Sorting/README.md @@ -221,10 +221,10 @@ impl Solution { if max_idx != n { if max_idx != 0 { arr[..=max_idx].reverse(); - res.push(max_idx as i32 + 1); + res.push((max_idx as i32) + 1); } arr[..=n].reverse(); - res.push(n as i32 + 1); + res.push((n as i32) + 1); } } res diff --git a/solution/0900-0999/0969.Pancake Sorting/README_EN.md b/solution/0900-0999/0969.Pancake Sorting/README_EN.md index 44fb4ce1faaa1..eb57f89f05a54 100644 --- a/solution/0900-0999/0969.Pancake Sorting/README_EN.md +++ b/solution/0900-0999/0969.Pancake Sorting/README_EN.md @@ -210,10 +210,10 @@ impl Solution { if max_idx != n { if max_idx != 0 { arr[..=max_idx].reverse(); - res.push(max_idx as i32 + 1); + res.push((max_idx as i32) + 1); } arr[..=n].reverse(); - res.push(n as i32 + 1); + res.push((n as i32) + 1); } } res diff --git a/solution/0900-0999/0969.Pancake Sorting/Solution.rs b/solution/0900-0999/0969.Pancake Sorting/Solution.rs index b892531011214..c35cae193b651 100644 --- a/solution/0900-0999/0969.Pancake Sorting/Solution.rs +++ b/solution/0900-0999/0969.Pancake Sorting/Solution.rs @@ -11,10 +11,10 @@ impl Solution { if max_idx != n { if max_idx != 0 { arr[..=max_idx].reverse(); - res.push(max_idx as i32 + 1); + res.push((max_idx as i32) + 1); } arr[..=n].reverse(); - res.push(n as i32 + 1); + res.push((n as i32) + 1); } } res diff --git a/solution/0900-0999/0986.Interval List Intersections/README.md b/solution/0900-0999/0986.Interval List Intersections/README.md index ea99c347a90fc..c3d617d67a9fa 100644 --- a/solution/0900-0999/0986.Interval List Intersections/README.md +++ b/solution/0900-0999/0986.Interval List Intersections/README.md @@ -189,7 +189,7 @@ function intervalIntersection(firstList: number[][], secondList: number[][]): nu impl Solution { pub fn interval_intersection( first_list: Vec>, - second_list: Vec>, + second_list: Vec> ) -> Vec> { let n = first_list.len(); let m = second_list.len(); diff --git a/solution/0900-0999/0986.Interval List Intersections/README_EN.md b/solution/0900-0999/0986.Interval List Intersections/README_EN.md index dec77829cdb0d..2526fa3f8ea49 100644 --- a/solution/0900-0999/0986.Interval List Intersections/README_EN.md +++ b/solution/0900-0999/0986.Interval List Intersections/README_EN.md @@ -163,7 +163,7 @@ function intervalIntersection(firstList: number[][], secondList: number[][]): nu impl Solution { pub fn interval_intersection( first_list: Vec>, - second_list: Vec>, + second_list: Vec> ) -> Vec> { let n = first_list.len(); let m = second_list.len(); diff --git a/solution/0900-0999/0986.Interval List Intersections/Solution.rs b/solution/0900-0999/0986.Interval List Intersections/Solution.rs index 5e104c8271a6b..5641a407b4bf3 100644 --- a/solution/0900-0999/0986.Interval List Intersections/Solution.rs +++ b/solution/0900-0999/0986.Interval List Intersections/Solution.rs @@ -1,7 +1,7 @@ impl Solution { pub fn interval_intersection( first_list: Vec>, - second_list: Vec>, + second_list: Vec> ) -> Vec> { let n = first_list.len(); let m = second_list.len(); diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 2533cf0892d6d..a5462d1516333 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -284,7 +284,9 @@ impl Solution { for i in 0..m { for j in 0..n { match grid[i][j] { - 1 => count += 1, + 1 => { + count += 1; + } 2 => queue.push_back([i as i32, j as i32]), _ => (), } @@ -299,11 +301,12 @@ impl Solution { for i in 0..4 { let new_x = x + dirs[i]; let new_y = y + dirs[i + 1]; - if new_x >= 0 - && new_x < m as i32 - && new_y >= 0 - && new_y < n as i32 - && grid[new_x as usize][new_y as usize] == 1 + if + new_x >= 0 && + new_x < (m as i32) && + new_y >= 0 && + new_y < (n as i32) && + grid[new_x as usize][new_y as usize] == 1 { grid[new_x as usize][new_y as usize] = 2; queue.push_back([new_x, new_y]); diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index c5ed2c5c9efb2..49d841a5415d9 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -257,7 +257,9 @@ impl Solution { for i in 0..m { for j in 0..n { match grid[i][j] { - 1 => count += 1, + 1 => { + count += 1; + } 2 => queue.push_back([i as i32, j as i32]), _ => (), } @@ -272,11 +274,12 @@ impl Solution { for i in 0..4 { let new_x = x + dirs[i]; let new_y = y + dirs[i + 1]; - if new_x >= 0 - && new_x < m as i32 - && new_y >= 0 - && new_y < n as i32 - && grid[new_x as usize][new_y as usize] == 1 + if + new_x >= 0 && + new_x < (m as i32) && + new_y >= 0 && + new_y < (n as i32) && + grid[new_x as usize][new_y as usize] == 1 { grid[new_x as usize][new_y as usize] = 2; queue.push_back([new_x, new_y]); diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.rs b/solution/0900-0999/0994.Rotting Oranges/Solution.rs index 2fb62e645f13a..00c99af89c968 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.rs +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.rs @@ -10,7 +10,9 @@ impl Solution { for i in 0..m { for j in 0..n { match grid[i][j] { - 1 => count += 1, + 1 => { + count += 1; + } 2 => queue.push_back([i as i32, j as i32]), _ => (), } @@ -25,11 +27,12 @@ impl Solution { for i in 0..4 { let new_x = x + dirs[i]; let new_y = y + dirs[i + 1]; - if new_x >= 0 - && new_x < m as i32 - && new_y >= 0 - && new_y < n as i32 - && grid[new_x as usize][new_y as usize] == 1 + if + new_x >= 0 && + new_x < (m as i32) && + new_y >= 0 && + new_y < (n as i32) && + grid[new_x as usize][new_y as usize] == 1 { grid[new_x as usize][new_y as usize] = 2; queue.push_back([new_x, new_y]); diff --git a/solution/0900-0999/0998.Maximum Binary Tree II/README.md b/solution/0900-0999/0998.Maximum Binary Tree II/README.md index 06f425d6efd42..c968b83e267c9 100644 --- a/solution/0900-0999/0998.Maximum Binary Tree II/README.md +++ b/solution/0900-0999/0998.Maximum Binary Tree II/README.md @@ -397,14 +397,18 @@ use std::cell::RefCell; impl Solution { pub fn insert_into_max_tree( mut root: Option>>, - val: i32, + val: i32 ) -> Option>> { if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val { - return Some(Rc::new(RefCell::new(TreeNode { - val, - left: root.take(), - right: None, - }))); + return Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: root.take(), + right: None, + }) + ) + ); } { let mut root = root.as_ref().unwrap().as_ref().borrow_mut(); diff --git a/solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md b/solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md index affd51c4b9c00..119a45104cfc6 100644 --- a/solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md +++ b/solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md @@ -367,14 +367,18 @@ use std::cell::RefCell; impl Solution { pub fn insert_into_max_tree( mut root: Option>>, - val: i32, + val: i32 ) -> Option>> { if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val { - return Some(Rc::new(RefCell::new(TreeNode { - val, - left: root.take(), - right: None, - }))); + return Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: root.take(), + right: None, + }) + ) + ); } { let mut root = root.as_ref().unwrap().as_ref().borrow_mut(); diff --git a/solution/0900-0999/0998.Maximum Binary Tree II/Solution.rs b/solution/0900-0999/0998.Maximum Binary Tree II/Solution.rs index e7c291f189763..936f488308102 100644 --- a/solution/0900-0999/0998.Maximum Binary Tree II/Solution.rs +++ b/solution/0900-0999/0998.Maximum Binary Tree II/Solution.rs @@ -21,14 +21,18 @@ use std::cell::RefCell; impl Solution { pub fn insert_into_max_tree( mut root: Option>>, - val: i32, + val: i32 ) -> Option>> { if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val { - return Some(Rc::new(RefCell::new(TreeNode { - val, - left: root.take(), - right: None, - }))); + return Some( + Rc::new( + RefCell::new(TreeNode { + val, + left: root.take(), + right: None, + }) + ) + ); } { let mut root = root.as_ref().unwrap().as_ref().borrow_mut(); diff --git a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README.md b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README.md index cb2a0e70f3503..9f9c38f530290 100644 --- a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README.md +++ b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README.md @@ -270,16 +270,20 @@ impl Solution { preorder: &Vec, next: &Vec, left: usize, - right: usize, + right: usize ) -> Option>> { if left >= right { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: preorder[left], - left: Self::dfs(preorder, next, left + 1, next[left]), - right: Self::dfs(preorder, next, next[left], right), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: preorder[left], + left: Self::dfs(preorder, next, left + 1, next[left]), + right: Self::dfs(preorder, next, next[left], right), + }) + ) + ) } pub fn bst_from_preorder(preorder: Vec) -> Option>> { diff --git a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README_EN.md b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README_EN.md index 129032813213c..b4663e659c189 100644 --- a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README_EN.md +++ b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/README_EN.md @@ -254,16 +254,20 @@ impl Solution { preorder: &Vec, next: &Vec, left: usize, - right: usize, + right: usize ) -> Option>> { if left >= right { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: preorder[left], - left: Self::dfs(preorder, next, left + 1, next[left]), - right: Self::dfs(preorder, next, next[left], right), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: preorder[left], + left: Self::dfs(preorder, next, left + 1, next[left]), + right: Self::dfs(preorder, next, next[left], right), + }) + ) + ) } pub fn bst_from_preorder(preorder: Vec) -> Option>> { diff --git a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.rs b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.rs index 6b4d526afa253..b2e38fb861192 100644 --- a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.rs +++ b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.rs @@ -23,16 +23,20 @@ impl Solution { preorder: &Vec, next: &Vec, left: usize, - right: usize, + right: usize ) -> Option>> { if left >= right { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: preorder[left], - left: Self::dfs(preorder, next, left + 1, next[left]), - right: Self::dfs(preorder, next, next[left], right), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: preorder[left], + left: Self::dfs(preorder, next, left + 1, next[left]), + right: Self::dfs(preorder, next, next[left], right), + }) + ) + ) } pub fn bst_from_preorder(preorder: Vec) -> Option>> { diff --git a/solution/1000-1099/1036.Escape a Large Maze/README.md b/solution/1000-1099/1036.Escape a Large Maze/README.md index 05e8f7a01857f..ee24eef1fe2f2 100644 --- a/solution/1000-1099/1036.Escape a Large Maze/README.md +++ b/solution/1000-1099/1036.Escape a Large Maze/README.md @@ -195,7 +195,7 @@ func isEscapePossible(blocked [][]int, source []int, target []int) bool { ### **Rust** ```rust -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; const BOUNDARY: i32 = 1_000_000; const MAX: usize = 20000; @@ -225,12 +225,13 @@ fn bfs(block: &HashSet<(i32, i32)>, source: &Vec, target: &Vec) -> boo } for (dx, dy) in dir.iter() { let (nx, ny) = (x + dx, y + dy); - if nx < 0 - || nx >= BOUNDARY - || ny < 0 - || ny >= BOUNDARY - || vis.contains(&(nx, ny)) - || block.contains(&(nx, ny)) + if + nx < 0 || + nx >= BOUNDARY || + ny < 0 || + ny >= BOUNDARY || + vis.contains(&(nx, ny)) || + block.contains(&(nx, ny)) { continue; } diff --git a/solution/1000-1099/1036.Escape a Large Maze/README_EN.md b/solution/1000-1099/1036.Escape a Large Maze/README_EN.md index c2009c3a19d39..e7ffbd5408960 100644 --- a/solution/1000-1099/1036.Escape a Large Maze/README_EN.md +++ b/solution/1000-1099/1036.Escape a Large Maze/README_EN.md @@ -184,7 +184,7 @@ func isEscapePossible(blocked [][]int, source []int, target []int) bool { ### **Rust** ```rust -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; const BOUNDARY: i32 = 1_000_000; const MAX: usize = 20000; @@ -214,12 +214,13 @@ fn bfs(block: &HashSet<(i32, i32)>, source: &Vec, target: &Vec) -> boo } for (dx, dy) in dir.iter() { let (nx, ny) = (x + dx, y + dy); - if nx < 0 - || nx >= BOUNDARY - || ny < 0 - || ny >= BOUNDARY - || vis.contains(&(nx, ny)) - || block.contains(&(nx, ny)) + if + nx < 0 || + nx >= BOUNDARY || + ny < 0 || + ny >= BOUNDARY || + vis.contains(&(nx, ny)) || + block.contains(&(nx, ny)) { continue; } diff --git a/solution/1000-1099/1036.Escape a Large Maze/Solution.rs b/solution/1000-1099/1036.Escape a Large Maze/Solution.rs index 19e88060f539e..c9a867f46d746 100644 --- a/solution/1000-1099/1036.Escape a Large Maze/Solution.rs +++ b/solution/1000-1099/1036.Escape a Large Maze/Solution.rs @@ -1,4 +1,4 @@ -use std::collections::{HashSet, VecDeque}; +use std::collections::{ HashSet, VecDeque }; const BOUNDARY: i32 = 1_000_000; const MAX: usize = 20000; @@ -28,12 +28,13 @@ fn bfs(block: &HashSet<(i32, i32)>, source: &Vec, target: &Vec) -> boo } for (dx, dy) in dir.iter() { let (nx, ny) = (x + dx, y + dy); - if nx < 0 - || nx >= BOUNDARY - || ny < 0 - || ny >= BOUNDARY - || vis.contains(&(nx, ny)) - || block.contains(&(nx, ny)) + if + nx < 0 || + nx >= BOUNDARY || + ny < 0 || + ny >= BOUNDARY || + vis.contains(&(nx, ny)) || + block.contains(&(nx, ny)) { continue; } diff --git a/solution/1000-1099/1048.Longest String Chain/README.md b/solution/1000-1099/1048.Longest String Chain/README.md index b748fb9f42545..20eb22218fd5d 100644 --- a/solution/1000-1099/1048.Longest String Chain/README.md +++ b/solution/1000-1099/1048.Longest String Chain/README.md @@ -197,9 +197,7 @@ impl Solution { let mut map: HashMap = HashMap::new(); // Sort the words vector first - words.sort_by(|lhs, rhs| { - lhs.len().cmp(&rhs.len()) - }); + words.sort_by(|lhs, rhs| { lhs.len().cmp(&rhs.len()) }); // Begin the "dp" process for w in words.iter() { @@ -208,9 +206,7 @@ impl Solution { for i in 0..n { let s = w[..i].to_string() + &w[i + 1..]; - let v = map - .entry(s.clone()) - .or_default(); + let v = map.entry(s.clone()).or_default(); x = std::cmp::max(x, *v + 1); } diff --git a/solution/1000-1099/1048.Longest String Chain/README_EN.md b/solution/1000-1099/1048.Longest String Chain/README_EN.md index e034a41423e94..06d9809c6b4df 100644 --- a/solution/1000-1099/1048.Longest String Chain/README_EN.md +++ b/solution/1000-1099/1048.Longest String Chain/README_EN.md @@ -178,9 +178,7 @@ impl Solution { let mut map: HashMap = HashMap::new(); // Sort the words vector first - words.sort_by(|lhs, rhs| { - lhs.len().cmp(&rhs.len()) - }); + words.sort_by(|lhs, rhs| { lhs.len().cmp(&rhs.len()) }); // Begin the "dp" process for w in words.iter() { @@ -189,9 +187,7 @@ impl Solution { for i in 0..n { let s = w[..i].to_string() + &w[i + 1..]; - let v = map - .entry(s.clone()) - .or_default(); + let v = map.entry(s.clone()).or_default(); x = std::cmp::max(x, *v + 1); } diff --git a/solution/1000-1099/1048.Longest String Chain/Solution.rs b/solution/1000-1099/1048.Longest String Chain/Solution.rs index 3b08738682b90..e7d8d2e83b06f 100644 --- a/solution/1000-1099/1048.Longest String Chain/Solution.rs +++ b/solution/1000-1099/1048.Longest String Chain/Solution.rs @@ -8,9 +8,7 @@ impl Solution { let mut map: HashMap = HashMap::new(); // Sort the words vector first - words.sort_by(|lhs, rhs| { - lhs.len().cmp(&rhs.len()) - }); + words.sort_by(|lhs, rhs| { lhs.len().cmp(&rhs.len()) }); // Begin the "dp" process for w in words.iter() { @@ -19,9 +17,7 @@ impl Solution { for i in 0..n { let s = w[..i].to_string() + &w[i + 1..]; - let v = map - .entry(s.clone()) - .or_default(); + let v = map.entry(s.clone()).or_default(); x = std::cmp::max(x, *v + 1); } @@ -32,4 +28,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/1000-1099/1049.Last Stone Weight II/README.md b/solution/1000-1099/1049.Last Stone Weight II/README.md index 85468a983b19f..18f62f7a4b260 100644 --- a/solution/1000-1099/1049.Last Stone Weight II/README.md +++ b/solution/1000-1099/1049.Last Stone Weight II/README.md @@ -192,17 +192,19 @@ impl Solution { sum += *e; } - let m = (sum / 2) as usize; let mut dp: Vec> = vec![vec![0; m + 1]; n + 1]; // Begin the actual dp process for i in 1..=n { for j in 1..=m { - dp[i][j] = if stones[i - 1] > j as i32 { + dp[i][j] = if stones[i - 1] > (j as i32) { dp[i - 1][j] } else { - std::cmp::max(dp[i - 1][j], dp[i - 1][j - stones[i - 1] as usize] + stones[i - 1]) + std::cmp::max( + dp[i - 1][j], + dp[i - 1][j - (stones[i - 1] as usize)] + stones[i - 1] + ) }; } } diff --git a/solution/1000-1099/1049.Last Stone Weight II/README_EN.md b/solution/1000-1099/1049.Last Stone Weight II/README_EN.md index bfdf729c62674..b5b8e7a654cad 100644 --- a/solution/1000-1099/1049.Last Stone Weight II/README_EN.md +++ b/solution/1000-1099/1049.Last Stone Weight II/README_EN.md @@ -176,17 +176,19 @@ impl Solution { sum += *e; } - let m = (sum / 2) as usize; let mut dp: Vec> = vec![vec![0; m + 1]; n + 1]; // Begin the actual dp process for i in 1..=n { for j in 1..=m { - dp[i][j] = if stones[i - 1] > j as i32 { + dp[i][j] = if stones[i - 1] > (j as i32) { dp[i - 1][j] } else { - std::cmp::max(dp[i - 1][j], dp[i - 1][j - stones[i - 1] as usize] + stones[i - 1]) + std::cmp::max( + dp[i - 1][j], + dp[i - 1][j - (stones[i - 1] as usize)] + stones[i - 1] + ) }; } } diff --git a/solution/1000-1099/1049.Last Stone Weight II/Solution.rs b/solution/1000-1099/1049.Last Stone Weight II/Solution.rs index 3b14d9b2217da..34b1b380d17d7 100644 --- a/solution/1000-1099/1049.Last Stone Weight II/Solution.rs +++ b/solution/1000-1099/1049.Last Stone Weight II/Solution.rs @@ -8,21 +8,23 @@ impl Solution { sum += *e; } - let m = (sum / 2) as usize; let mut dp: Vec> = vec![vec![0; m + 1]; n + 1]; // Begin the actual dp process for i in 1..=n { for j in 1..=m { - dp[i][j] = if stones[i - 1] > j as i32 { + dp[i][j] = if stones[i - 1] > (j as i32) { dp[i - 1][j] } else { - std::cmp::max(dp[i - 1][j], dp[i - 1][j - stones[i - 1] as usize] + stones[i - 1]) + std::cmp::max( + dp[i - 1][j], + dp[i - 1][j - (stones[i - 1] as usize)] + stones[i - 1] + ) }; } } sum - 2 * dp[n][m] } -} \ No newline at end of file +} diff --git a/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs b/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs index 949fb85d4389d..837f6f5577acb 100644 --- a/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs +++ b/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs @@ -1,16 +1,16 @@ -impl Solution { - pub fn gcd_of_strings(str1: String, str2: String) -> String { - if str1.clone() + &str2 != str2.clone() + &str1 { - return String::from(""); - } - fn gcd(a: usize, b: usize) -> usize { - if b == 0 { - return a; - } - gcd(b, a % b) - } - - let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len())); - str1[..gcd(m, n)].to_string() - } -} +impl Solution { + pub fn gcd_of_strings(str1: String, str2: String) -> String { + if str1.clone() + &str2 != str2.clone() + &str1 { + return String::from(""); + } + fn gcd(a: usize, b: usize) -> usize { + if b == 0 { + return a; + } + gcd(b, a % b) + } + + let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len())); + str1[..gcd(m, n)].to_string() + } +} diff --git a/solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md b/solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md index 36db2ad06dfb8..f0f0c071cedda 100644 --- a/solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md +++ b/solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md @@ -255,9 +255,9 @@ impl Solution { grid[i][j] = 1; for x in -1..=1 { for y in -1..=1 { - let x = x + i as i32; - let y = y + j as i32; - if x < 0 || x == n as i32 || y < 0 || y == n as i32 { + let x = x + (i as i32); + let y = y + (j as i32); + if x < 0 || x == (n as i32) || y < 0 || y == (n as i32) { continue; } queue.push_back([x as usize, y as usize]); diff --git a/solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md b/solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md index d7e7e536211eb..fe134fcef982f 100644 --- a/solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md +++ b/solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md @@ -233,9 +233,9 @@ impl Solution { grid[i][j] = 1; for x in -1..=1 { for y in -1..=1 { - let x = x + i as i32; - let y = y + j as i32; - if x < 0 || x == n as i32 || y < 0 || y == n as i32 { + let x = x + (i as i32); + let y = y + (j as i32); + if x < 0 || x == (n as i32) || y < 0 || y == (n as i32) { continue; } queue.push_back([x as usize, y as usize]); diff --git a/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.rs b/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.rs index 59060b11a0268..4ed6c8d4b0975 100644 --- a/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.rs +++ b/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.rs @@ -18,9 +18,9 @@ impl Solution { grid[i][j] = 1; for x in -1..=1 { for y in -1..=1 { - let x = x + i as i32; - let y = y + j as i32; - if x < 0 || x == n as i32 || y < 0 || y == n as i32 { + let x = x + (i as i32); + let y = y + (j as i32); + if x < 0 || x == (n as i32) || y < 0 || y == (n as i32) { continue; } queue.push_back([x as usize, y as usize]); diff --git a/solution/1000-1099/1095.Find in Mountain Array/Solution.rs b/solution/1000-1099/1095.Find in Mountain Array/Solution.rs index a1ad7cc1aa55d..6ab968c23a7c0 100644 --- a/solution/1000-1099/1095.Find in Mountain Array/Solution.rs +++ b/solution/1000-1099/1095.Find in Mountain Array/Solution.rs @@ -44,4 +44,4 @@ impl Solution { -1 } } -} \ No newline at end of file +} diff --git a/solution/1100-1199/1106.Parsing A Boolean Expression/README.md b/solution/1100-1199/1106.Parsing A Boolean Expression/README.md index 782687399f6dd..41f4fa50ea2cf 100644 --- a/solution/1100-1199/1106.Parsing A Boolean Expression/README.md +++ b/solution/1100-1199/1106.Parsing A Boolean Expression/README.md @@ -256,10 +256,18 @@ impl Solution { res.push(!Self::dfs(i, expr)[0]); } b'&' => { - res.push(Self::dfs(i, expr).iter().all(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .all(|v| *v) + ); } b'|' => { - res.push(Self::dfs(i, expr).iter().any(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .any(|v| *v) + ); } _ => {} } diff --git a/solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md b/solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md index 5dcdb33605eab..7994b21ad111a 100644 --- a/solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md +++ b/solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md @@ -233,10 +233,18 @@ impl Solution { res.push(!Self::dfs(i, expr)[0]); } b'&' => { - res.push(Self::dfs(i, expr).iter().all(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .all(|v| *v) + ); } b'|' => { - res.push(Self::dfs(i, expr).iter().any(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .any(|v| *v) + ); } _ => {} } diff --git a/solution/1100-1199/1106.Parsing A Boolean Expression/Solution.rs b/solution/1100-1199/1106.Parsing A Boolean Expression/Solution.rs index 171b8443e5f59..0ac36ea63e96e 100644 --- a/solution/1100-1199/1106.Parsing A Boolean Expression/Solution.rs +++ b/solution/1100-1199/1106.Parsing A Boolean Expression/Solution.rs @@ -19,10 +19,18 @@ impl Solution { res.push(!Self::dfs(i, expr)[0]); } b'&' => { - res.push(Self::dfs(i, expr).iter().all(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .all(|v| *v) + ); } b'|' => { - res.push(Self::dfs(i, expr).iter().any(|v| *v)); + res.push( + Self::dfs(i, expr) + .iter() + .any(|v| *v) + ); } _ => {} } diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/README.md b/solution/1100-1199/1109.Corporate Flight Bookings/README.md index 18e8f5eaaeaa2..ffe356ee1f517 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/README.md +++ b/solution/1100-1199/1109.Corporate Flight Bookings/README.md @@ -268,9 +268,9 @@ impl Solution { // Build the difference vector first for b in &bookings { - let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + let (l, r) = ((b[0] as usize) - 1, (b[1] as usize) - 1); ans[l] += b[2]; - if r < n as usize - 1 { + if r < (n as usize) - 1 { ans[r + 1] -= b[2]; } } diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md b/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md index 358c197c0bdb8..2af09b3d13c08 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md +++ b/solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md @@ -238,9 +238,9 @@ impl Solution { // Build the difference vector first for b in &bookings { - let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + let (l, r) = ((b[0] as usize) - 1, (b[1] as usize) - 1); ans[l] += b[2]; - if r < n as usize - 1 { + if r < (n as usize) - 1 { ans[r + 1] -= b[2]; } } diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs index 2089a9c488032..24c5225d780ec 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs +++ b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.rs @@ -5,9 +5,9 @@ impl Solution { // Build the difference vector first for b in &bookings { - let (l, r) = (b[0] as usize - 1, b[1] as usize - 1); + let (l, r) = ((b[0] as usize) - 1, (b[1] as usize) - 1); ans[l] += b[2]; - if r < n as usize - 1 { + if r < (n as usize) - 1 { ans[r + 1] -= b[2]; } } @@ -19,4 +19,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/1100-1199/1143.Longest Common Subsequence/README.md b/solution/1100-1199/1143.Longest Common Subsequence/README.md index 2653db89eeb11..c0cb498960caa 100644 --- a/solution/1100-1199/1143.Longest Common Subsequence/README.md +++ b/solution/1100-1199/1143.Longest Common Subsequence/README.md @@ -218,7 +218,7 @@ impl Solution { f[i - 1][j - 1] + 1 } else { f[i - 1][j].max(f[i][j - 1]) - } + }; } } f[m][n] diff --git a/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md b/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md index f6f139f63375e..7335505292c1f 100644 --- a/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md +++ b/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md @@ -208,7 +208,7 @@ impl Solution { f[i - 1][j - 1] + 1 } else { f[i - 1][j].max(f[i][j - 1]) - } + }; } } f[m][n] diff --git a/solution/1100-1199/1143.Longest Common Subsequence/Solution.rs b/solution/1100-1199/1143.Longest Common Subsequence/Solution.rs index 00fad4108deca..78db47309d9ff 100644 --- a/solution/1100-1199/1143.Longest Common Subsequence/Solution.rs +++ b/solution/1100-1199/1143.Longest Common Subsequence/Solution.rs @@ -9,7 +9,7 @@ impl Solution { f[i - 1][j - 1] + 1 } else { f[i - 1][j].max(f[i][j - 1]) - } + }; } } f[m][n] diff --git a/solution/1100-1199/1155.Number of Dice Rolls With Target Sum/Solution.rs b/solution/1100-1199/1155.Number of Dice Rolls With Target Sum/Solution.rs index 387f6ed000056..4bb8d9e961086 100644 --- a/solution/1100-1199/1155.Number of Dice Rolls With Target Sum/Solution.rs +++ b/solution/1100-1199/1155.Number of Dice Rolls With Target Sum/Solution.rs @@ -1,22 +1,22 @@ -impl Solution { - pub fn num_rolls_to_target(n: i32, k: i32, target: i32) -> i32 { - let _mod = 1_000_000_007; - let n = n as usize; - let k = k as usize; - let target = target as usize; - let mut f = vec![0; target + 1]; - f[0] = 1; - - for i in 1..=n { - let mut g = vec![0; target + 1]; - for j in 1..=target { - for h in 1..=j.min(k) { - g[j] = (g[j] + f[j - h]) % _mod; - } - } - f = g; - } - - f[target] - } -} \ No newline at end of file +impl Solution { + pub fn num_rolls_to_target(n: i32, k: i32, target: i32) -> i32 { + let _mod = 1_000_000_007; + let n = n as usize; + let k = k as usize; + let target = target as usize; + let mut f = vec![0; target + 1]; + f[0] = 1; + + for i in 1..=n { + let mut g = vec![0; target + 1]; + for j in 1..=target { + for h in 1..=j.min(k) { + g[j] = (g[j] + f[j - h]) % _mod; + } + } + f = g; + } + + f[target] + } +} diff --git a/solution/1100-1199/1189.Maximum Number of Balloons/README.md b/solution/1100-1199/1189.Maximum Number of Balloons/README.md index 9de72ea824136..9b5fd5f79603f 100644 --- a/solution/1100-1199/1189.Maximum Number of Balloons/README.md +++ b/solution/1100-1199/1189.Maximum Number of Balloons/README.md @@ -154,11 +154,21 @@ impl Solution { let mut arr = [0; 5]; for c in text.chars() { match c { - 'b' => arr[0] += 1, - 'a' => arr[1] += 1, - 'l' => arr[2] += 1, - 'o' => arr[3] += 1, - 'n' => arr[4] += 1, + 'b' => { + arr[0] += 1; + } + 'a' => { + arr[1] += 1; + } + 'l' => { + arr[2] += 1; + } + 'o' => { + arr[3] += 1; + } + 'n' => { + arr[4] += 1; + } _ => {} } } diff --git a/solution/1100-1199/1189.Maximum Number of Balloons/README_EN.md b/solution/1100-1199/1189.Maximum Number of Balloons/README_EN.md index 1a01b33af1484..ba0637121d961 100644 --- a/solution/1100-1199/1189.Maximum Number of Balloons/README_EN.md +++ b/solution/1100-1199/1189.Maximum Number of Balloons/README_EN.md @@ -139,11 +139,21 @@ impl Solution { let mut arr = [0; 5]; for c in text.chars() { match c { - 'b' => arr[0] += 1, - 'a' => arr[1] += 1, - 'l' => arr[2] += 1, - 'o' => arr[3] += 1, - 'n' => arr[4] += 1, + 'b' => { + arr[0] += 1; + } + 'a' => { + arr[1] += 1; + } + 'l' => { + arr[2] += 1; + } + 'o' => { + arr[3] += 1; + } + 'n' => { + arr[4] += 1; + } _ => {} } } diff --git a/solution/1100-1199/1189.Maximum Number of Balloons/Solution.rs b/solution/1100-1199/1189.Maximum Number of Balloons/Solution.rs index ef1a608f6023c..ccc9f3cf2901a 100644 --- a/solution/1100-1199/1189.Maximum Number of Balloons/Solution.rs +++ b/solution/1100-1199/1189.Maximum Number of Balloons/Solution.rs @@ -3,11 +3,21 @@ impl Solution { let mut arr = [0; 5]; for c in text.chars() { match c { - 'b' => arr[0] += 1, - 'a' => arr[1] += 1, - 'l' => arr[2] += 1, - 'o' => arr[3] += 1, - 'n' => arr[4] += 1, + 'b' => { + arr[0] += 1; + } + 'a' => { + arr[1] += 1; + } + 'l' => { + arr[2] += 1; + } + 'o' => { + arr[3] += 1; + } + 'n' => { + arr[4] += 1; + } _ => {} } } diff --git a/solution/1100-1199/1197.Minimum Knight Moves/README.md b/solution/1100-1199/1197.Minimum Knight Moves/README.md index 296dffa17e71f..1ac69288b17a6 100644 --- a/solution/1100-1199/1197.Minimum Knight Moves/README.md +++ b/solution/1100-1199/1197.Minimum Knight Moves/README.md @@ -298,7 +298,16 @@ public: ```rust use std::collections::VecDeque; -const DIR: [(i32, i32); 8] = [(-2, 1), (2, 1), (-1, 2), (1, 2), (2, -1), (-2, -1), (1, -2), (-1, -2)]; +const DIR: [(i32, i32); 8] = [ + (-2, 1), + (2, 1), + (-1, 2), + (1, 2), + (2, -1), + (-2, -1), + (1, -2), + (-1, -2), +]; impl Solution { #[allow(dead_code)] @@ -328,7 +337,13 @@ impl Solution { } #[allow(dead_code)] - fn enqueue(vis: &mut Vec>, q: &mut VecDeque<(i32, i32, i32)>, i: i32, j: i32, cur_step: i32) { + fn enqueue( + vis: &mut Vec>, + q: &mut VecDeque<(i32, i32, i32)>, + i: i32, + j: i32, + cur_step: i32 + ) { let next_step = cur_step + 1; for (dx, dy) in DIR { let x = i + dx; @@ -358,7 +373,16 @@ impl Solution { use std::collections::VecDeque; use std::collections::HashMap; -const DIR: [(i32, i32); 8] = [(-2, 1), (2, 1), (-1, 2), (1, 2), (2, -1), (-2, -1), (1, -2), (-1, -2)]; +const DIR: [(i32, i32); 8] = [ + (-2, 1), + (2, 1), + (-1, 2), + (1, 2), + (2, -1), + (-2, -1), + (1, -2), + (-1, -2), +]; impl Solution { #[allow(dead_code)] @@ -398,13 +422,20 @@ impl Solution { } #[allow(dead_code)] - fn extend(map_to: &mut HashMap, map_from: &mut HashMap, cur_q: &mut VecDeque<(i32, i32)>) -> i32 { + fn extend( + map_to: &mut HashMap, + map_from: &mut HashMap, + cur_q: &mut VecDeque<(i32, i32)> + ) -> i32 { let n = cur_q.len(); for _ in 0..n { let (i, j) = cur_q.front().unwrap().clone(); cur_q.pop_front(); // The cur_step here must exist - let cur_step = map_to.get(&(601 * i + j)).unwrap().clone(); + let cur_step = map_to + .get(&(601 * i + j)) + .unwrap() + .clone(); for (dx, dy) in DIR { let x = i + dx; let y = j + dy; @@ -416,7 +447,14 @@ impl Solution { // Check if this node has been visited by the other side if map_from.contains_key(&(601 * x + y)) { // We found the node - return cur_step + 1 + map_from.get(&(601 * x + y)).unwrap().clone(); + return ( + cur_step + + 1 + + map_from + .get(&(601 * x + y)) + .unwrap() + .clone() + ); } // Otherwise, update map_to and push the new node to queue map_to.insert(601 * x + y, cur_step + 1); diff --git a/solution/1100-1199/1197.Minimum Knight Moves/README_EN.md b/solution/1100-1199/1197.Minimum Knight Moves/README_EN.md index db734a0727902..6c750ad8662c9 100644 --- a/solution/1100-1199/1197.Minimum Knight Moves/README_EN.md +++ b/solution/1100-1199/1197.Minimum Knight Moves/README_EN.md @@ -277,7 +277,16 @@ public: ```rust use std::collections::VecDeque; -const DIR: [(i32, i32); 8] = [(-2, 1), (2, 1), (-1, 2), (1, 2), (2, -1), (-2, -1), (1, -2), (-1, -2)]; +const DIR: [(i32, i32); 8] = [ + (-2, 1), + (2, 1), + (-1, 2), + (1, 2), + (2, -1), + (-2, -1), + (1, -2), + (-1, -2), +]; impl Solution { #[allow(dead_code)] @@ -307,7 +316,13 @@ impl Solution { } #[allow(dead_code)] - fn enqueue(vis: &mut Vec>, q: &mut VecDeque<(i32, i32, i32)>, i: i32, j: i32, cur_step: i32) { + fn enqueue( + vis: &mut Vec>, + q: &mut VecDeque<(i32, i32, i32)>, + i: i32, + j: i32, + cur_step: i32 + ) { let next_step = cur_step + 1; for (dx, dy) in DIR { let x = i + dx; @@ -337,7 +352,16 @@ Two-end BFS: use std::collections::VecDeque; use std::collections::HashMap; -const DIR: [(i32, i32); 8] = [(-2, 1), (2, 1), (-1, 2), (1, 2), (2, -1), (-2, -1), (1, -2), (-1, -2)]; +const DIR: [(i32, i32); 8] = [ + (-2, 1), + (2, 1), + (-1, 2), + (1, 2), + (2, -1), + (-2, -1), + (1, -2), + (-1, -2), +]; impl Solution { #[allow(dead_code)] @@ -377,13 +401,20 @@ impl Solution { } #[allow(dead_code)] - fn extend(map_to: &mut HashMap, map_from: &mut HashMap, cur_q: &mut VecDeque<(i32, i32)>) -> i32 { + fn extend( + map_to: &mut HashMap, + map_from: &mut HashMap, + cur_q: &mut VecDeque<(i32, i32)> + ) -> i32 { let n = cur_q.len(); for _ in 0..n { let (i, j) = cur_q.front().unwrap().clone(); cur_q.pop_front(); // The cur_step here must exist - let cur_step = map_to.get(&(601 * i + j)).unwrap().clone(); + let cur_step = map_to + .get(&(601 * i + j)) + .unwrap() + .clone(); for (dx, dy) in DIR { let x = i + dx; let y = j + dy; @@ -395,7 +426,14 @@ impl Solution { // Check if this node has been visited by the other side if map_from.contains_key(&(601 * x + y)) { // We found the node - return cur_step + 1 + map_from.get(&(601 * x + y)).unwrap().clone(); + return ( + cur_step + + 1 + + map_from + .get(&(601 * x + y)) + .unwrap() + .clone() + ); } // Otherwise, update map_to and push the new node to queue map_to.insert(601 * x + y, cur_step + 1); diff --git a/solution/1100-1199/1197.Minimum Knight Moves/Solution.rs b/solution/1100-1199/1197.Minimum Knight Moves/Solution.rs index 2eb8c1ead0fa3..0f58ce905594f 100644 --- a/solution/1100-1199/1197.Minimum Knight Moves/Solution.rs +++ b/solution/1100-1199/1197.Minimum Knight Moves/Solution.rs @@ -1,6 +1,15 @@ use std::collections::VecDeque; -const DIR: [(i32, i32); 8] = [(-2, 1), (2, 1), (-1, 2), (1, 2), (2, -1), (-2, -1), (1, -2), (-1, -2)]; +const DIR: [(i32, i32); 8] = [ + (-2, 1), + (2, 1), + (-1, 2), + (1, 2), + (2, -1), + (-2, -1), + (1, -2), + (-1, -2), +]; impl Solution { #[allow(dead_code)] @@ -30,7 +39,13 @@ impl Solution { } #[allow(dead_code)] - fn enqueue(vis: &mut Vec>, q: &mut VecDeque<(i32, i32, i32)>, i: i32, j: i32, cur_step: i32) { + fn enqueue( + vis: &mut Vec>, + q: &mut VecDeque<(i32, i32, i32)>, + i: i32, + j: i32, + cur_step: i32 + ) { let next_step = cur_step + 1; for (dx, dy) in DIR { let x = i + dx; @@ -51,4 +66,4 @@ impl Solution { fn check_bounds(i: i32, j: i32) -> bool { i < 0 || i > 600 || j < 0 || j > 600 } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1202.Smallest String With Swaps/README.md b/solution/1200-1299/1202.Smallest String With Swaps/README.md index 3b8112cb17948..528bc90618d31 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/README.md +++ b/solution/1200-1299/1202.Smallest String With Swaps/README.md @@ -216,7 +216,7 @@ impl Solution { #[allow(dead_code)] fn find(x: usize, d_set: &mut Vec) -> usize { - if d_set[x] != x { + if d_set[x] != x { d_set[x] = Self::find(d_set[x], d_set); } d_set[x] diff --git a/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md b/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md index 7465e74c6d57c..a56d5af64e5cb 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md +++ b/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md @@ -201,7 +201,7 @@ impl Solution { #[allow(dead_code)] fn find(x: usize, d_set: &mut Vec) -> usize { - if d_set[x] != x { + if d_set[x] != x { d_set[x] = Self::find(d_set[x], d_set); } d_set[x] diff --git a/solution/1200-1299/1202.Smallest String With Swaps/Solution.rs b/solution/1200-1299/1202.Smallest String With Swaps/Solution.rs index 908c4ca86af6e..793fdd1f05752 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/Solution.rs +++ b/solution/1200-1299/1202.Smallest String With Swaps/Solution.rs @@ -41,7 +41,7 @@ impl Solution { #[allow(dead_code)] fn find(x: usize, d_set: &mut Vec) -> usize { - if d_set[x] != x { + if d_set[x] != x { d_set[x] = Self::find(d_set[x], d_set); } d_set[x] @@ -53,4 +53,4 @@ impl Solution { let p_y = Self::find(y, d_set); d_set[p_x] = p_y; } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1216.Valid Palindrome III/README.md b/solution/1200-1299/1216.Valid Palindrome III/README.md index 1bb9d6ccf0adf..1bd8868cfe3bc 100644 --- a/solution/1200-1299/1216.Valid Palindrome III/README.md +++ b/solution/1200-1299/1216.Valid Palindrome III/README.md @@ -202,14 +202,14 @@ impl Solution { } for i in (0..n - 2).rev() { - for j in (i + 1)..n { + for j in i + 1..n { if s[i] == s[j] { f[i][j] = f[i + 1][j - 1] + 2; } else { f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]); } - if f[i][j] + k >= n as i32 { + if f[i][j] + k >= (n as i32) { return true; } } diff --git a/solution/1200-1299/1216.Valid Palindrome III/README_EN.md b/solution/1200-1299/1216.Valid Palindrome III/README_EN.md index 7437b61ebfe06..2b47e3bf314d8 100644 --- a/solution/1200-1299/1216.Valid Palindrome III/README_EN.md +++ b/solution/1200-1299/1216.Valid Palindrome III/README_EN.md @@ -192,14 +192,14 @@ impl Solution { } for i in (0..n - 2).rev() { - for j in (i + 1)..n { + for j in i + 1..n { if s[i] == s[j] { f[i][j] = f[i + 1][j - 1] + 2; } else { f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]); } - if f[i][j] + k >= n as i32 { + if f[i][j] + k >= (n as i32) { return true; } } diff --git a/solution/1200-1299/1216.Valid Palindrome III/Solution.rs b/solution/1200-1299/1216.Valid Palindrome III/Solution.rs index af19672b75485..33c4ca130f964 100644 --- a/solution/1200-1299/1216.Valid Palindrome III/Solution.rs +++ b/solution/1200-1299/1216.Valid Palindrome III/Solution.rs @@ -1,27 +1,27 @@ -impl Solution { - pub fn is_valid_palindrome(s: String, k: i32) -> bool { - let s = s.as_bytes(); - let n = s.len(); - let mut f = vec![vec![0; n]; n]; - - for i in 0..n { - f[i][i] = 1; - } - - for i in (0..n - 2).rev() { - for j in (i + 1)..n { - if s[i] == s[j] { - f[i][j] = f[i + 1][j - 1] + 2; - } else { - f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]); - } - - if f[i][j] + k >= n as i32 { - return true; - } - } - } - - false - } -} \ No newline at end of file +impl Solution { + pub fn is_valid_palindrome(s: String, k: i32) -> bool { + let s = s.as_bytes(); + let n = s.len(); + let mut f = vec![vec![0; n]; n]; + + for i in 0..n { + f[i][i] = 1; + } + + for i in (0..n - 2).rev() { + for j in i + 1..n { + if s[i] == s[j] { + f[i][j] = f[i + 1][j - 1] + 2; + } else { + f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]); + } + + if f[i][j] + k >= (n as i32) { + return true; + } + } + } + + false + } +} diff --git a/solution/1200-1299/1229.Meeting Scheduler/README.md b/solution/1200-1299/1229.Meeting Scheduler/README.md index e9dd374a765d0..60ba1d0190e6e 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/README.md +++ b/solution/1200-1299/1229.Meeting Scheduler/README.md @@ -140,17 +140,17 @@ public: ```rust impl Solution { #[allow(dead_code)] - pub fn min_available_duration(slots1: Vec>, slots2: Vec>, duration: i32) -> Vec { + pub fn min_available_duration( + slots1: Vec>, + slots2: Vec>, + duration: i32 + ) -> Vec { let mut slots1 = slots1; let mut slots2 = slots2; // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); - slots2.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + slots1.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); + slots2.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Then traverse the two vector let mut i: usize = 0; @@ -162,8 +162,10 @@ impl Solution { let (start, end) = (slots1[i][0], slots1[i][1]); while j < M && slots2[j][0] < end { // If still in the scope - let (cur_x, cur_y) = - (std::cmp::max(start, slots2[j][0]), std::cmp::min(end, slots2[j][1])); + let (cur_x, cur_y) = ( + std::cmp::max(start, slots2[j][0]), + std::cmp::min(end, slots2[j][1]), + ); if cur_y - cur_x >= duration { return vec![cur_x, cur_x + duration]; } diff --git a/solution/1200-1299/1229.Meeting Scheduler/README_EN.md b/solution/1200-1299/1229.Meeting Scheduler/README_EN.md index 9542d9823a729..aeed2fa6ad56c 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/README_EN.md +++ b/solution/1200-1299/1229.Meeting Scheduler/README_EN.md @@ -124,17 +124,17 @@ public: ```rust impl Solution { #[allow(dead_code)] - pub fn min_available_duration(slots1: Vec>, slots2: Vec>, duration: i32) -> Vec { + pub fn min_available_duration( + slots1: Vec>, + slots2: Vec>, + duration: i32 + ) -> Vec { let mut slots1 = slots1; let mut slots2 = slots2; // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); - slots2.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + slots1.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); + slots2.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Then traverse the two vector let mut i: usize = 0; @@ -146,8 +146,10 @@ impl Solution { let (start, end) = (slots1[i][0], slots1[i][1]); while j < M && slots2[j][0] < end { // If still in the scope - let (cur_x, cur_y) = - (std::cmp::max(start, slots2[j][0]), std::cmp::min(end, slots2[j][1])); + let (cur_x, cur_y) = ( + std::cmp::max(start, slots2[j][0]), + std::cmp::min(end, slots2[j][1]), + ); if cur_y - cur_x >= duration { return vec![cur_x, cur_x + duration]; } diff --git a/solution/1200-1299/1229.Meeting Scheduler/Solution.rs b/solution/1200-1299/1229.Meeting Scheduler/Solution.rs index eb60dbdaf95f5..c1f5735adda69 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/Solution.rs +++ b/solution/1200-1299/1229.Meeting Scheduler/Solution.rs @@ -1,29 +1,31 @@ impl Solution { #[allow(dead_code)] - pub fn min_available_duration(slots1: Vec>, slots2: Vec>, duration: i32) -> Vec { + pub fn min_available_duration( + slots1: Vec>, + slots2: Vec>, + duration: i32 + ) -> Vec { let mut slots1 = slots1; let mut slots2 = slots2; // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); - slots2.sort_by(|lhs, rhs| { - lhs[0].cmp(&rhs[0]) - }); + slots1.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); + slots2.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) }); // Then traverse the two vector let mut i: usize = 0; let mut j: usize = 0; let N = slots1.len(); let M = slots2.len(); - + while i < N && j < M { let (start, end) = (slots1[i][0], slots1[i][1]); while j < M && slots2[j][0] < end { // If still in the scope - let (cur_x, cur_y) = - (std::cmp::max(start, slots2[j][0]), std::cmp::min(end, slots2[j][1])); + let (cur_x, cur_y) = ( + std::cmp::max(start, slots2[j][0]), + std::cmp::min(end, slots2[j][1]), + ); if cur_y - cur_x >= duration { return vec![cur_x, cur_x + duration]; } @@ -40,4 +42,4 @@ impl Solution { // The default is an empty vector vec![] } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1244.Design A Leaderboard/README.md b/solution/1200-1299/1244.Design A Leaderboard/README.md index 1ceab67506142..895039c8f2c10 100644 --- a/solution/1200-1299/1244.Design A Leaderboard/README.md +++ b/solution/1200-1299/1244.Design A Leaderboard/README.md @@ -245,10 +245,11 @@ impl Leaderboard { #[allow(dead_code)] fn top(&self, k: i32) -> i32 { - let mut cur_vec: Vec<(i32, i32)> = self.record_map.iter().map(|(k, v)| (*k, *v)).collect(); - cur_vec.sort_by(|lhs, rhs| { - rhs.1.cmp(&lhs.1) - }); + let mut cur_vec: Vec<(i32, i32)> = self.record_map + .iter() + .map(|(k, v)| (*k, *v)) + .collect(); + cur_vec.sort_by(|lhs, rhs| { rhs.1.cmp(&lhs.1) }); // Iterate reversely for K let mut sum = 0; let mut i = 0; diff --git a/solution/1200-1299/1244.Design A Leaderboard/README_EN.md b/solution/1200-1299/1244.Design A Leaderboard/README_EN.md index a65111adb6247..0ac2a4f06e3fa 100644 --- a/solution/1200-1299/1244.Design A Leaderboard/README_EN.md +++ b/solution/1200-1299/1244.Design A Leaderboard/README_EN.md @@ -216,10 +216,11 @@ impl Leaderboard { #[allow(dead_code)] fn top(&self, k: i32) -> i32 { - let mut cur_vec: Vec<(i32, i32)> = self.record_map.iter().map(|(k, v)| (*k, *v)).collect(); - cur_vec.sort_by(|lhs, rhs| { - rhs.1.cmp(&lhs.1) - }); + let mut cur_vec: Vec<(i32, i32)> = self.record_map + .iter() + .map(|(k, v)| (*k, *v)) + .collect(); + cur_vec.sort_by(|lhs, rhs| { rhs.1.cmp(&lhs.1) }); // Iterate reversely for K let mut sum = 0; let mut i = 0; diff --git a/solution/1200-1299/1244.Design A Leaderboard/Solution.rs b/solution/1200-1299/1244.Design A Leaderboard/Solution.rs index bfdde4a9ab644..f73470eb8a54b 100644 --- a/solution/1200-1299/1244.Design A Leaderboard/Solution.rs +++ b/solution/1200-1299/1244.Design A Leaderboard/Solution.rs @@ -27,10 +27,11 @@ impl Leaderboard { #[allow(dead_code)] fn top(&self, k: i32) -> i32 { - let mut cur_vec: Vec<(i32, i32)> = self.record_map.iter().map(|(k, v)| (*k, *v)).collect(); - cur_vec.sort_by(|lhs, rhs| { - rhs.1.cmp(&lhs.1) - }); + let mut cur_vec: Vec<(i32, i32)> = self.record_map + .iter() + .map(|(k, v)| (*k, *v)) + .collect(); + cur_vec.sort_by(|lhs, rhs| { rhs.1.cmp(&lhs.1) }); // Iterate reversely for K let mut sum = 0; let mut i = 0; @@ -51,4 +52,4 @@ impl Leaderboard { // Just set the score to 0 self.record_map.insert(player_id, 0); } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README.md b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README.md index e70862271b006..748c3ce530ba0 100644 --- a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README.md +++ b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README.md @@ -263,8 +263,12 @@ impl Solution { let mut right = 0; for c in bs.iter() { match c { - &b'(' => left += 1, - &b')' if right < left => right += 1, + &b'(' => { + left += 1; + } + &b')' if right < left => { + right += 1; + } _ => {} } } diff --git a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md index 99f37fb65c889..0a42711e7277c 100644 --- a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md +++ b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md @@ -240,8 +240,12 @@ impl Solution { let mut right = 0; for c in bs.iter() { match c { - &b'(' => left += 1, - &b')' if right < left => right += 1, + &b'(' => { + left += 1; + } + &b')' if right < left => { + right += 1; + } _ => {} } } diff --git a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/Solution.rs b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/Solution.rs index 192dae3d3e279..decdca7d91105 100644 --- a/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/Solution.rs +++ b/solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/Solution.rs @@ -6,8 +6,12 @@ impl Solution { let mut right = 0; for c in bs.iter() { match c { - &b'(' => left += 1, - &b')' if right < left => right += 1, + &b'(' => { + left += 1; + } + &b')' if right < left => { + right += 1; + } _ => {} } } diff --git a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution.rs b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution.rs index b7db7a2ab6664..80da6ff133ddf 100644 --- a/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution.rs +++ b/solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn subtract_product_and_sum(mut n: i32) -> i32 { - let mut x = 1; - let mut y = 0; - while n != 0 { - let v = n % 10; - n /= 10; - x *= v; - y += v; - } - x - y - } -} +impl Solution { + pub fn subtract_product_and_sum(mut n: i32) -> i32 { + let mut x = 1; + let mut y = 0; + while n != 0 { + let v = n % 10; + n /= 10; + x *= v; + y += v; + } + x - y + } +} diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md index 6af55770c9651..a124518030a44 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md @@ -206,7 +206,7 @@ impl Solution { for i in 0..n { g[group_sizes[i] as usize].push(i as i32); - if g[group_sizes[i] as usize].len() == group_sizes[i] as usize { + if g[group_sizes[i] as usize].len() == (group_sizes[i] as usize) { ret.push(g[group_sizes[i] as usize].clone()); g[group_sizes[i] as usize].clear(); } diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md index e91a93fb30c95..2046a8da08ff2 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md @@ -188,7 +188,7 @@ impl Solution { for i in 0..n { g[group_sizes[i] as usize].push(i as i32); - if g[group_sizes[i] as usize].len() == group_sizes[i] as usize { + if g[group_sizes[i] as usize].len() == (group_sizes[i] as usize) { ret.push(g[group_sizes[i] as usize].clone()); g[group_sizes[i] as usize].clear(); } diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/README.md b/solution/1300-1399/1302.Deepest Leaves Sum/README.md index 871d712d134e6..8a74bc47341c8 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/README.md +++ b/solution/1300-1399/1302.Deepest Leaves Sum/README.md @@ -460,7 +460,7 @@ impl Solution { *res += node.val; } else if depth > *max_depth { *max_depth = depth; - *res = node.val + *res = node.val; } return; } diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md b/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md index e61e569171112..2fc448cfe9e52 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md +++ b/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md @@ -440,7 +440,7 @@ impl Solution { *res += node.val; } else if depth > *max_depth { *max_depth = depth; - *res = node.val + *res = node.val; } return; } diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/Solution.rs b/solution/1300-1399/1302.Deepest Leaves Sum/Solution.rs index 6436217f7e22a..fae5f9833940d 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/Solution.rs +++ b/solution/1300-1399/1302.Deepest Leaves Sum/Solution.rs @@ -27,7 +27,7 @@ impl Solution { *res += node.val; } else if depth > *max_depth { *max_depth = depth; - *res = node.val + *res = node.val; } return; } diff --git a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md index b2c711d3b26d0..45cbe8c2e415b 100644 --- a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md +++ b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md @@ -271,7 +271,7 @@ use std::rc::Rc; impl Solution { pub fn get_all_elements( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Vec { fn dfs(root: &Option>>, t: &mut Vec) { if let Some(root) = root { diff --git a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README_EN.md b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README_EN.md index 6d6ad6c59dc46..3651c180f18de 100644 --- a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README_EN.md +++ b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/README_EN.md @@ -255,7 +255,7 @@ use std::rc::Rc; impl Solution { pub fn get_all_elements( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Vec { fn dfs(root: &Option>>, t: &mut Vec) { if let Some(root) = root { diff --git a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.rs b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.rs index b25f1d910e43c..caa0e52613c00 100644 --- a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.rs +++ b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.rs @@ -21,7 +21,7 @@ use std::rc::Rc; impl Solution { pub fn get_all_elements( root1: Option>>, - root2: Option>>, + root2: Option>> ) -> Vec { fn dfs(root: &Option>>, t: &mut Vec) { if let Some(root) = root { diff --git a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md index 7be362a4c519f..8e1e882d26a49 100644 --- a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md +++ b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md @@ -137,7 +137,7 @@ impl Solution { code = s[i]; i += 1; } - res.push(char::from('a' as u8 + code - b'1')); + res.push(char::from(('a' as u8) + code - b'1')); } res } diff --git a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md index fb2668034a1f1..d665d0a2d9d60 100644 --- a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md +++ b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md @@ -127,7 +127,7 @@ impl Solution { code = s[i]; i += 1; } - res.push(char::from('a' as u8 + code - b'1')); + res.push(char::from(('a' as u8) + code - b'1')); } res } diff --git a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/Solution.rs b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/Solution.rs index ce41c789645a2..d0a3d25effee3 100644 --- a/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/Solution.rs +++ b/solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/Solution.rs @@ -13,7 +13,7 @@ impl Solution { code = s[i]; i += 1; } - res.push(char::from('a' as u8 + code - b'1')); + res.push(char::from(('a' as u8) + code - b'1')); } res } diff --git a/solution/1300-1399/1316.Distinct Echo Substrings/README.md b/solution/1300-1399/1316.Distinct Echo Substrings/README.md index 1976bfb23f0ff..963d15df077b7 100644 --- a/solution/1300-1399/1316.Distinct Echo Substrings/README.md +++ b/solution/1300-1399/1316.Distinct Echo Substrings/README.md @@ -178,7 +178,7 @@ impl Solution { // Initialize the base vector & hash vector for i in 0..n { - let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64; + let cur_char = ((text.chars().nth(i).unwrap() as u8) - ('a' as u8) + 1) as u64; // Update base vector base_vec[i + 1] = base_vec[i] * BASE; // Update hash vector diff --git a/solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md b/solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md index 01057fc3a6314..c5a2bfb79d3c6 100644 --- a/solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md +++ b/solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md @@ -154,7 +154,7 @@ impl Solution { // Initialize the base vector & hash vector for i in 0..n { - let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64; + let cur_char = ((text.chars().nth(i).unwrap() as u8) - ('a' as u8) + 1) as u64; // Update base vector base_vec[i + 1] = base_vec[i] * BASE; // Update hash vector diff --git a/solution/1300-1399/1316.Distinct Echo Substrings/Solution.rs b/solution/1300-1399/1316.Distinct Echo Substrings/Solution.rs index 90fb820757aa7..7a73849aa50e5 100644 --- a/solution/1300-1399/1316.Distinct Echo Substrings/Solution.rs +++ b/solution/1300-1399/1316.Distinct Echo Substrings/Solution.rs @@ -12,7 +12,7 @@ impl Solution { // Initialize the base vector & hash vector for i in 0..n { - let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64; + let cur_char = ((text.chars().nth(i).unwrap() as u8) - ('a' as u8) + 1) as u64; // Update base vector base_vec[i + 1] = base_vec[i] * BASE; // Update hash vector @@ -39,4 +39,4 @@ impl Solution { fn get_hash(start: usize, end: usize, base_vec: &Vec, hash_vec: &Vec) -> u64 { hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1] } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md index 2f8c7692fc4e2..a10f0fac4e631 100644 --- a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md +++ b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md @@ -175,19 +175,22 @@ impl Solution { // Initialize the last vector for (i, &r) in ranges.iter().enumerate() { - if i as i32 - r >= 0 { - last[(i as i32 - r) as usize] = std::cmp::max(last[(i as i32 - r) as usize], i as i32 + r); + if (i as i32) - r >= 0 { + last[((i as i32) - r) as usize] = std::cmp::max( + last[((i as i32) - r) as usize], + (i as i32) + r + ); } else { - last[0] = std::cmp::max(last[0], i as i32 + r); + last[0] = std::cmp::max(last[0], (i as i32) + r); } } for i in 0..n as usize { mx = std::cmp::max(mx, last[i]); - if mx <= i as i32 { + if mx <= (i as i32) { return -1; } - if pre == i as i32 { + if pre == (i as i32) { ans += 1; pre = mx; } diff --git a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md index 3872215c9df46..60ac56747fcb0 100644 --- a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md +++ b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md @@ -135,19 +135,22 @@ impl Solution { // Initialize the last vector for (i, &r) in ranges.iter().enumerate() { - if i as i32 - r >= 0 { - last[(i as i32 - r) as usize] = std::cmp::max(last[(i as i32 - r) as usize], i as i32 + r); + if (i as i32) - r >= 0 { + last[((i as i32) - r) as usize] = std::cmp::max( + last[((i as i32) - r) as usize], + (i as i32) + r + ); } else { - last[0] = std::cmp::max(last[0], i as i32 + r); + last[0] = std::cmp::max(last[0], (i as i32) + r); } } for i in 0..n as usize { mx = std::cmp::max(mx, last[i]); - if mx <= i as i32 { + if mx <= (i as i32) { return -1; } - if pre == i as i32 { + if pre == (i as i32) { ans += 1; pre = mx; } diff --git a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/Solution.rs b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/Solution.rs index 2e221f2bd9ccd..f70d5a5a07174 100644 --- a/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/Solution.rs +++ b/solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/Solution.rs @@ -8,19 +8,22 @@ impl Solution { // Initialize the last vector for (i, &r) in ranges.iter().enumerate() { - if i as i32 - r >= 0 { - last[(i as i32 - r) as usize] = std::cmp::max(last[(i as i32 - r) as usize], i as i32 + r); + if (i as i32) - r >= 0 { + last[((i as i32) - r) as usize] = std::cmp::max( + last[((i as i32) - r) as usize], + (i as i32) + r + ); } else { - last[0] = std::cmp::max(last[0], i as i32 + r); + last[0] = std::cmp::max(last[0], (i as i32) + r); } } for i in 0..n as usize { mx = std::cmp::max(mx, last[i]); - if mx <= i as i32 { + if mx <= (i as i32) { return -1; } - if pre == i as i32 { + if pre == (i as i32) { ans += 1; pre = mx; } @@ -28,4 +31,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/README.md b/solution/1300-1399/1346.Check If N and Its Double Exist/README.md index 4f6317b42a05a..01e7728c6eaf0 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/README.md +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/README.md @@ -425,8 +425,12 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match arr[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } Ordering::Equal => { if mid == i { break; diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md b/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md index 1c457948ac10d..4cc61f59b5f3d 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md @@ -391,8 +391,12 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match arr[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } Ordering::Equal => { if mid == i { break; diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.rs b/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.rs index f9b696ec4f5e3..baed199d57ef1 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.rs +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.rs @@ -10,8 +10,12 @@ impl Solution { while left < right { let mid = left + (right - left) / 2; match arr[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, + Ordering::Less => { + left = mid + 1; + } + Ordering::Greater => { + right = mid; + } Ordering::Equal => { if mid == i { break; @@ -23,4 +27,4 @@ impl Solution { } false } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README.md b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README.md index 73234bf47c01a..55c04046af45f 100644 --- a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README.md +++ b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README.md @@ -119,7 +119,7 @@ public: ### **Rust** ```rust -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { #[allow(dead_code)] diff --git a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README_EN.md b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README_EN.md index 71a362ba6ac7a..a8db24dfdbd58 100644 --- a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README_EN.md +++ b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/README_EN.md @@ -93,7 +93,7 @@ public: ### **Rust** ```rust -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { #[allow(dead_code)] diff --git a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/Solution.rs b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/Solution.rs index ef4da2cb41764..aaf11144ee213 100644 --- a/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/Solution.rs +++ b/solution/1300-1399/1359.Count All Valid Pickup and Delivery Options/Solution.rs @@ -1,4 +1,4 @@ -const MOD: i64 = 1e9 as i64 + 7; +const MOD: i64 = (1e9 as i64) + 7; impl Solution { #[allow(dead_code)] @@ -9,4 +9,4 @@ impl Solution { } f as i32 } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/README.md b/solution/1300-1399/1367.Linked List in Binary Tree/README.md index 806e3f14e8739..7c0501ec9e4cd 100644 --- a/solution/1300-1399/1367.Linked List in Binary Tree/README.md +++ b/solution/1300-1399/1367.Linked List in Binary Tree/README.md @@ -339,9 +339,9 @@ impl Solution { return false; } let node = root.as_ref().unwrap().borrow(); - Self::dfs(head, root) - || Self::my_is_sub_path(head, &node.left) - || Self::my_is_sub_path(head, &node.right) + Self::dfs(head, root) || + Self::my_is_sub_path(head, &node.left) || + Self::my_is_sub_path(head, &node.right) } pub fn is_sub_path(head: Option>, root: Option>>) -> bool { diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md b/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md index 8e5fd2abd569e..c49e98e6cdb31 100644 --- a/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md +++ b/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md @@ -319,9 +319,9 @@ impl Solution { return false; } let node = root.as_ref().unwrap().borrow(); - Self::dfs(head, root) - || Self::my_is_sub_path(head, &node.left) - || Self::my_is_sub_path(head, &node.right) + Self::dfs(head, root) || + Self::my_is_sub_path(head, &node.left) || + Self::my_is_sub_path(head, &node.right) } pub fn is_sub_path(head: Option>, root: Option>>) -> bool { diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/Solution.rs b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.rs index be8934f7d9c66..f773172293156 100644 --- a/solution/1300-1399/1367.Linked List in Binary Tree/Solution.rs +++ b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.rs @@ -55,9 +55,9 @@ impl Solution { return false; } let node = root.as_ref().unwrap().borrow(); - Self::dfs(head, root) - || Self::my_is_sub_path(head, &node.left) - || Self::my_is_sub_path(head, &node.right) + Self::dfs(head, root) || + Self::my_is_sub_path(head, &node.left) || + Self::my_is_sub_path(head, &node.right) } pub fn is_sub_path(head: Option>, root: Option>>) -> bool { diff --git a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README.md b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README.md index 3d1d2e536b480..2cd56bc5f1c36 100644 --- a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README.md +++ b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README.md @@ -203,7 +203,10 @@ impl Solution { right = mid; } } - if i32::abs(num - arr2[left]) <= d || (left != 0 && i32::abs(num - arr2[left - 1]) <= d) { + if + i32::abs(num - arr2[left]) <= d || + (left != 0 && i32::abs(num - arr2[left - 1]) <= d) + { continue; } res += 1; diff --git a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md index 420ea0d53dde4..3adff882266bb 100644 --- a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md +++ b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md @@ -184,7 +184,10 @@ impl Solution { right = mid; } } - if i32::abs(num - arr2[left]) <= d || (left != 0 && i32::abs(num - arr2[left - 1]) <= d) { + if + i32::abs(num - arr2[left]) <= d || + (left != 0 && i32::abs(num - arr2[left - 1]) <= d) + { continue; } res += 1; diff --git a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.rs b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.rs index 70fa4f47fcccd..7d06668c6adb6 100644 --- a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.rs +++ b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.rs @@ -14,7 +14,9 @@ impl Solution { right = mid; } } - if i32::abs(num - arr2[left]) <= d || (left != 0 && i32::abs(num - arr2[left - 1]) <= d) + if + i32::abs(num - arr2[left]) <= d || + (left != 0 && i32::abs(num - arr2[left - 1]) <= d) { continue; } diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md index 4424ec8de429d..1771aac37c859 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README.md @@ -157,7 +157,7 @@ impl Solution { sum += num; min = min.min(sum); } - 1.max(1 - min) + (1).max(1 - min) } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md index a100f4c49afa8..35edd72232dbf 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/README_EN.md @@ -147,7 +147,7 @@ impl Solution { sum += num; min = min.min(sum); } - 1.max(1 - min) + (1).max(1 - min) } } ``` diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.rs b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.rs index b8a8eb3b5dd3d..6c7eb82a60014 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.rs +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.rs @@ -6,6 +6,6 @@ impl Solution { sum += num; min = min.min(sum); } - 1.max(1 - min) + (1).max(1 - min) } } diff --git a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README.md b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README.md index 328ca5e0b8a0c..687349402c54b 100644 --- a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README.md +++ b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README.md @@ -172,7 +172,10 @@ function maxScore(cardPoints: number[], k: number): number { impl Solution { pub fn max_score(card_points: Vec, k: i32) -> i32 { let (k, n) = (k as usize, card_points.len()); - let mut sum = card_points.iter().take(n - k).sum::(); + let mut sum = card_points + .iter() + .take(n - k) + .sum::(); let mut min = sum; for i in 0..k { sum += card_points[n - k + i] - card_points[i]; diff --git a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README_EN.md b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README_EN.md index ad69dfdc7d7b3..2f4dc451ce16d 100644 --- a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README_EN.md +++ b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/README_EN.md @@ -150,7 +150,10 @@ function maxScore(cardPoints: number[], k: number): number { impl Solution { pub fn max_score(card_points: Vec, k: i32) -> i32 { let (k, n) = (k as usize, card_points.len()); - let mut sum = card_points.iter().take(n - k).sum::(); + let mut sum = card_points + .iter() + .take(n - k) + .sum::(); let mut min = sum; for i in 0..k { sum += card_points[n - k + i] - card_points[i]; diff --git a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.rs b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.rs index e92a238fe96ef..6d59c3b894b23 100644 --- a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.rs +++ b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.rs @@ -1,7 +1,10 @@ impl Solution { pub fn max_score(card_points: Vec, k: i32) -> i32 { let (k, n) = (k as usize, card_points.len()); - let mut sum = card_points.iter().take(n - k).sum::(); + let mut sum = card_points + .iter() + .take(n - k) + .sum::(); let mut min = sum; for i in 0..k { sum += card_points[n - k + i] - card_points[i]; diff --git a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README.md b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README.md index 6848c14dafff6..41e923f849beb 100644 --- a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README.md +++ b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README.md @@ -131,7 +131,10 @@ function kidsWithCandies(candies: number[], extraCandies: number): boolean[] { impl Solution { pub fn kids_with_candies(candies: Vec, extra_candies: i32) -> Vec { let max = *candies.iter().max().unwrap(); - candies.iter().map(|v| v + extra_candies >= max).collect() + candies + .iter() + .map(|v| v + extra_candies >= max) + .collect() } } ``` diff --git a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README_EN.md b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README_EN.md index c1a00094d9e31..71323b90c53b5 100644 --- a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README_EN.md +++ b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/README_EN.md @@ -128,7 +128,10 @@ function kidsWithCandies(candies: number[], extraCandies: number): boolean[] { impl Solution { pub fn kids_with_candies(candies: Vec, extra_candies: i32) -> Vec { let max = *candies.iter().max().unwrap(); - candies.iter().map(|v| v + extra_candies >= max).collect() + candies + .iter() + .map(|v| v + extra_candies >= max) + .collect() } } ``` diff --git a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/Solution.rs b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/Solution.rs index 57666e2349af2..f87625ff2bf28 100644 --- a/solution/1400-1499/1431.Kids With the Greatest Number of Candies/Solution.rs +++ b/solution/1400-1499/1431.Kids With the Greatest Number of Candies/Solution.rs @@ -1,6 +1,9 @@ impl Solution { pub fn kids_with_candies(candies: Vec, extra_candies: i32) -> Vec { let max = *candies.iter().max().unwrap(); - candies.iter().map(|v| v + extra_candies >= max).collect() + candies + .iter() + .map(|v| v + extra_candies >= max) + .collect() } } diff --git a/solution/1400-1499/1436.Destination City/README.md b/solution/1400-1499/1436.Destination City/README.md index 61d81a5c0382c..5b2fd7ed73c86 100644 --- a/solution/1400-1499/1436.Destination City/README.md +++ b/solution/1400-1499/1436.Destination City/README.md @@ -174,7 +174,10 @@ function destCity(paths: string[][]): string { use std::collections::HashSet; impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); + let set = paths + .iter() + .map(|v| &v[0]) + .collect::>(); for path in paths.iter() { if !set.contains(&path[1]) { return path[1].clone(); diff --git a/solution/1400-1499/1436.Destination City/README_EN.md b/solution/1400-1499/1436.Destination City/README_EN.md index 7c9f672bf8460..948fc2b3e9ae1 100644 --- a/solution/1400-1499/1436.Destination City/README_EN.md +++ b/solution/1400-1499/1436.Destination City/README_EN.md @@ -158,7 +158,10 @@ function destCity(paths: string[][]): string { use std::collections::HashSet; impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); + let set = paths + .iter() + .map(|v| &v[0]) + .collect::>(); for path in paths.iter() { if !set.contains(&path[1]) { return path[1].clone(); diff --git a/solution/1400-1499/1436.Destination City/Solution.rs b/solution/1400-1499/1436.Destination City/Solution.rs index bb33d41b8f055..9dd6b98cde9f7 100644 --- a/solution/1400-1499/1436.Destination City/Solution.rs +++ b/solution/1400-1499/1436.Destination City/Solution.rs @@ -1,7 +1,10 @@ use std::collections::HashSet; impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); + let set = paths + .iter() + .map(|v| &v[0]) + .collect::>(); for path in paths.iter() { if !set.contains(&path[1]) { return path[1].clone(); diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md index 56d9e879a753b..e4ea3eea4ec7c 100644 --- a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md @@ -154,7 +154,7 @@ impl Solution { for j in 1..=m { dp[i][j] = std::cmp::max( std::cmp::max(dp[i - 1][j], dp[i][j - 1]), - std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1] ); } } diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md index 2b7d0aedad036..92b3422678131 100644 --- a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md @@ -120,7 +120,7 @@ impl Solution { for j in 1..=m { dp[i][j] = std::cmp::max( std::cmp::max(dp[i - 1][j], dp[i][j - 1]), - std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1] ); } } diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs index f165ff211442a..c9df56177fede 100644 --- a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs @@ -10,11 +10,11 @@ impl Solution { for j in 1..=m { dp[i][j] = std::cmp::max( std::cmp::max(dp[i - 1][j], dp[i][j - 1]), - std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1] ); } } dp[n][m] } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md index 3c71da89eed3c..e96b6a5891542 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md @@ -183,7 +183,12 @@ function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: n ```rust impl Solution { - pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { + pub fn max_area( + h: i32, + w: i32, + mut horizontal_cuts: Vec, + mut vertical_cuts: Vec + ) -> i32 { const MOD: i64 = 1_000_000_007; horizontal_cuts.sort(); @@ -192,15 +197,18 @@ impl Solution { let m = horizontal_cuts.len(); let n = vertical_cuts.len(); - let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); - let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); + let mut x = i64::max( + horizontal_cuts[0] as i64, + (h as i64) - (horizontal_cuts[m - 1] as i64) + ); + let mut y = i64::max(vertical_cuts[0] as i64, (w as i64) - (vertical_cuts[n - 1] as i64)); for i in 1..m { - x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); + x = i64::max(x, (horizontal_cuts[i] as i64) - (horizontal_cuts[i - 1] as i64)); } for i in 1..n { - y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); + y = i64::max(y, (vertical_cuts[i] as i64) - (vertical_cuts[i - 1] as i64)); } ((x * y) % MOD) as i32 diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md index 7c60b21c8300a..cf13df3ad75d2 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md @@ -170,7 +170,12 @@ function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: n ```rust impl Solution { - pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { + pub fn max_area( + h: i32, + w: i32, + mut horizontal_cuts: Vec, + mut vertical_cuts: Vec + ) -> i32 { const MOD: i64 = 1_000_000_007; horizontal_cuts.sort(); @@ -179,15 +184,18 @@ impl Solution { let m = horizontal_cuts.len(); let n = vertical_cuts.len(); - let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); - let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); + let mut x = i64::max( + horizontal_cuts[0] as i64, + (h as i64) - (horizontal_cuts[m - 1] as i64) + ); + let mut y = i64::max(vertical_cuts[0] as i64, (w as i64) - (vertical_cuts[n - 1] as i64)); for i in 1..m { - x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); + x = i64::max(x, (horizontal_cuts[i] as i64) - (horizontal_cuts[i - 1] as i64)); } for i in 1..n { - y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); + y = i64::max(y, (vertical_cuts[i] as i64) - (vertical_cuts[i - 1] as i64)); } ((x * y) % MOD) as i32 diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs index 89da228f09fc6..42d7be05acc70 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs @@ -1,24 +1,32 @@ -impl Solution { - pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { - const MOD: i64 = 1_000_000_007; - - horizontal_cuts.sort(); - vertical_cuts.sort(); - - let m = horizontal_cuts.len(); - let n = vertical_cuts.len(); - - let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); - let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); - - for i in 1..m { - x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); - } - - for i in 1..n { - y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); - } - - ((x * y) % MOD) as i32 - } -} \ No newline at end of file +impl Solution { + pub fn max_area( + h: i32, + w: i32, + mut horizontal_cuts: Vec, + mut vertical_cuts: Vec + ) -> i32 { + const MOD: i64 = 1_000_000_007; + + horizontal_cuts.sort(); + vertical_cuts.sort(); + + let m = horizontal_cuts.len(); + let n = vertical_cuts.len(); + + let mut x = i64::max( + horizontal_cuts[0] as i64, + (h as i64) - (horizontal_cuts[m - 1] as i64) + ); + let mut y = i64::max(vertical_cuts[0] as i64, (w as i64) - (vertical_cuts[n - 1] as i64)); + + for i in 1..m { + x = i64::max(x, (horizontal_cuts[i] as i64) - (horizontal_cuts[i - 1] as i64)); + } + + for i in 1..n { + y = i64::max(y, (vertical_cuts[i] as i64) - (vertical_cuts[i - 1] as i64)); + } + + ((x * y) % MOD) as i32 + } +} diff --git a/solution/1400-1499/1470.Shuffle the Array/README.md b/solution/1400-1499/1470.Shuffle the Array/README.md index 06e0166d31c9e..a699903429666 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README.md +++ b/solution/1400-1499/1470.Shuffle the Array/README.md @@ -166,11 +166,7 @@ impl Solution { for i in 0..n * 2 { let mut j = i; while nums[i] > 0 { - j = if j < n { - 2 * j - } else { - 2 * (j - n) + 1 - }; + j = if j < n { 2 * j } else { 2 * (j - n) + 1 }; nums.swap(i, j); nums[j] *= -1; } diff --git a/solution/1400-1499/1470.Shuffle the Array/README_EN.md b/solution/1400-1499/1470.Shuffle the Array/README_EN.md index b4bb6222ee01b..2b80727c9cb0b 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README_EN.md +++ b/solution/1400-1499/1470.Shuffle the Array/README_EN.md @@ -159,11 +159,7 @@ impl Solution { for i in 0..n * 2 { let mut j = i; while nums[i] > 0 { - j = if j < n { - 2 * j - } else { - 2 * (j - n) + 1 - }; + j = if j < n { 2 * j } else { 2 * (j - n) + 1 }; nums.swap(i, j); nums[j] *= -1; } diff --git a/solution/1500-1599/1512.Number of Good Pairs/README.md b/solution/1500-1599/1512.Number of Good Pairs/README.md index a7b03a32a84d1..c099e0a28eb1e 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/README.md +++ b/solution/1500-1599/1512.Number of Good Pairs/README.md @@ -220,7 +220,7 @@ impl Solution { } let mut ans = 0; for &v in cnt.iter() { - ans += v * (v - 1) / 2 + ans += (v * (v - 1)) / 2; } ans } diff --git a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md index 15e2b4bacda52..35bb9f0c8e26f 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md +++ b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md @@ -206,7 +206,7 @@ impl Solution { } let mut ans = 0; for &v in cnt.iter() { - ans += v * (v - 1) / 2 + ans += (v * (v - 1)) / 2; } ans } diff --git a/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/Solution.rs b/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/Solution.rs index 3d76501d60011..ea0e1dbd411a1 100644 --- a/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/Solution.rs +++ b/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/Solution.rs @@ -2,4 +2,4 @@ impl Solution { pub fn count_odds(low: i32, high: i32) -> i32 { ((high + 1) >> 1) - (low >> 1) } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.rs b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.rs index 365252b4d016c..34d14923c4db3 100644 --- a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.rs +++ b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.rs @@ -53,4 +53,4 @@ impl Solution { let p_y = Self::find(y, d_set); d_set[p_x] = p_y; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1572.Matrix Diagonal Sum/README.md b/solution/1500-1599/1572.Matrix Diagonal Sum/README.md index 65317d2dbe7a6..536fd8cf0fbcf 100644 --- a/solution/1500-1599/1572.Matrix Diagonal Sum/README.md +++ b/solution/1500-1599/1572.Matrix Diagonal Sum/README.md @@ -169,7 +169,7 @@ impl Solution { for i in 0..n { ans += mat[i][i] + mat[n - 1 - i][i]; } - if n & 1 == 1 { + if (n & 1) == 1 { ans -= mat[n >> 1][n >> 1]; } ans diff --git a/solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md b/solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md index a00ea213cfa5e..c087ba33fff53 100644 --- a/solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md +++ b/solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md @@ -149,7 +149,7 @@ impl Solution { for i in 0..n { ans += mat[i][i] + mat[n - 1 - i][i]; } - if n & 1 == 1 { + if (n & 1) == 1 { ans -= mat[n >> 1][n >> 1]; } ans diff --git a/solution/1500-1599/1572.Matrix Diagonal Sum/Solution.rs b/solution/1500-1599/1572.Matrix Diagonal Sum/Solution.rs index 0420fa6c7a9b6..4e78351623dc2 100644 --- a/solution/1500-1599/1572.Matrix Diagonal Sum/Solution.rs +++ b/solution/1500-1599/1572.Matrix Diagonal Sum/Solution.rs @@ -5,7 +5,7 @@ impl Solution { for i in 0..n { ans += mat[i][i] + mat[n - 1 - i][i]; } - if n & 1 == 1 { + if (n & 1) == 1 { ans -= mat[n >> 1][n >> 1]; } ans diff --git a/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.rs b/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.rs index a5439d2032697..dd34fe62e99be 100644 --- a/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.rs +++ b/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.rs @@ -13,4 +13,4 @@ impl Solution { } ans } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md b/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md index ba0169b560701..f7d4eb19ea4c0 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md @@ -179,7 +179,7 @@ impl Solution { if n == 1 { return works[0].to_string() + &Self::create_spaces(count); } - works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1)) + works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) } } ``` diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md b/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md index 955eb9c18d3f2..4a24ce2e2c795 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md @@ -147,7 +147,7 @@ impl Solution { if n == 1 { return works[0].to_string() + &Self::create_spaces(count); } - works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1)) + works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) } } ``` diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs index 2c4dc38d0ecb8..488a04e21b7a3 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs @@ -23,6 +23,6 @@ impl Solution { if n == 1 { return works[0].to_string() + &Self::create_spaces(count); } - works.join(&Self::create_spaces((count / (n - 1)))) + &Self::create_spaces(count % (n - 1)) + works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) } } diff --git a/solution/1500-1599/1598.Crawler Log Folder/README.md b/solution/1500-1599/1598.Crawler Log Folder/README.md index fc1ba047e016c..fd08c4970a8ce 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/README.md +++ b/solution/1500-1599/1598.Crawler Log Folder/README.md @@ -187,7 +187,7 @@ impl Solution { let mut depth = 0; for log in logs.iter() { if log == "../" { - depth = 0.max(depth - 1); + depth = (0).max(depth - 1); } else if log != "./" { depth += 1; } diff --git a/solution/1500-1599/1598.Crawler Log Folder/README_EN.md b/solution/1500-1599/1598.Crawler Log Folder/README_EN.md index f1e0521c0d638..061724372da2d 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/README_EN.md +++ b/solution/1500-1599/1598.Crawler Log Folder/README_EN.md @@ -174,7 +174,7 @@ impl Solution { let mut depth = 0; for log in logs.iter() { if log == "../" { - depth = 0.max(depth - 1); + depth = (0).max(depth - 1); } else if log != "./" { depth += 1; } diff --git a/solution/1500-1599/1598.Crawler Log Folder/Solution.rs b/solution/1500-1599/1598.Crawler Log Folder/Solution.rs index b0f0bb8ca6e69..72afce11124d5 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/Solution.rs +++ b/solution/1500-1599/1598.Crawler Log Folder/Solution.rs @@ -3,7 +3,7 @@ impl Solution { let mut depth = 0; for log in logs.iter() { if log == "../" { - depth = 0.max(depth - 1); + depth = (0).max(depth - 1); } else if log != "./" { depth += 1; } diff --git a/solution/1600-1699/1603.Design Parking System/README.md b/solution/1600-1699/1603.Design Parking System/README.md index 6157608375ed4..8588e875bb5f4 100644 --- a/solution/1600-1699/1603.Design Parking System/README.md +++ b/solution/1600-1699/1603.Design Parking System/README.md @@ -189,16 +189,14 @@ struct ParkingSystem { count: [i32; 3], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl ParkingSystem { - fn new(big: i32, medium: i32, small: i32) -> Self { Self { - count: [big, medium, small] + count: [big, medium, small], } } @@ -210,9 +208,7 @@ impl ParkingSystem { self.count[i] -= 1; true } -} - -/** +}/** * Your ParkingSystem object will be instantiated and called as such: * let obj = ParkingSystem::new(big, medium, small); * let ret_1: bool = obj.add_car(carType); diff --git a/solution/1600-1699/1603.Design Parking System/README_EN.md b/solution/1600-1699/1603.Design Parking System/README_EN.md index bcd209d5e6c47..030969cd3b52d 100644 --- a/solution/1600-1699/1603.Design Parking System/README_EN.md +++ b/solution/1600-1699/1603.Design Parking System/README_EN.md @@ -173,16 +173,14 @@ struct ParkingSystem { count: [i32; 3], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl ParkingSystem { - fn new(big: i32, medium: i32, small: i32) -> Self { Self { - count: [big, medium, small] + count: [big, medium, small], } } @@ -194,9 +192,7 @@ impl ParkingSystem { self.count[i] -= 1; true } -} - -/** +}/** * Your ParkingSystem object will be instantiated and called as such: * let obj = ParkingSystem::new(big, medium, small); * let ret_1: bool = obj.add_car(carType); diff --git a/solution/1600-1699/1603.Design Parking System/Solution.rs b/solution/1600-1699/1603.Design Parking System/Solution.rs index 2e44df16b8b4e..5c159357185bc 100644 --- a/solution/1600-1699/1603.Design Parking System/Solution.rs +++ b/solution/1600-1699/1603.Design Parking System/Solution.rs @@ -2,16 +2,14 @@ struct ParkingSystem { count: [i32; 3], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl ParkingSystem { - fn new(big: i32, medium: i32, small: i32) -> Self { Self { - count: [big, medium, small] + count: [big, medium, small], } } @@ -23,10 +21,8 @@ impl ParkingSystem { self.count[i] -= 1; true } -} - -/** +}/** * Your ParkingSystem object will be instantiated and called as such: * let obj = ParkingSystem::new(big, medium, small); * let ret_1: bool = obj.add_car(carType); - */ \ No newline at end of file + */ diff --git a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README.md b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README.md index 9a331220b090a..0799d0d6ecec4 100644 --- a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README.md +++ b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README.md @@ -156,12 +156,12 @@ impl Solution { pub fn trim_mean(mut arr: Vec) -> f64 { arr.sort(); let n = arr.len(); - let count = (n as f64 * 0.05).floor() as usize; + let count = ((n as f64) * 0.05).floor() as usize; let mut sum = 0; for i in count..n - count { sum += arr[i]; } - sum as f64 / (n as f64 * 0.9) + (sum as f64) / ((n as f64) * 0.9) } } ``` diff --git a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README_EN.md b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README_EN.md index 3c7c5ec29d873..b0d464aaa4b5e 100644 --- a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README_EN.md +++ b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/README_EN.md @@ -124,12 +124,12 @@ impl Solution { pub fn trim_mean(mut arr: Vec) -> f64 { arr.sort(); let n = arr.len(); - let count = (n as f64 * 0.05).floor() as usize; + let count = ((n as f64) * 0.05).floor() as usize; let mut sum = 0; for i in count..n - count { sum += arr[i]; } - sum as f64 / (n as f64 * 0.9) + (sum as f64) / ((n as f64) * 0.9) } } ``` diff --git a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.rs b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.rs index 87ab84a68d631..0b19b1e8e983a 100644 --- a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.rs +++ b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.rs @@ -2,11 +2,11 @@ impl Solution { pub fn trim_mean(mut arr: Vec) -> f64 { arr.sort(); let n = arr.len(); - let count = (n as f64 * 0.05).floor() as usize; + let count = ((n as f64) * 0.05).floor() as usize; let mut sum = 0; for i in count..n - count { sum += arr[i]; } - sum as f64 / (n as f64 * 0.9) + (sum as f64) / ((n as f64) * 0.9) } } diff --git a/solution/1600-1699/1640.Check Array Formation Through Concatenation/README.md b/solution/1600-1699/1640.Check Array Formation Through Concatenation/README.md index c467dfa183fa6..0c85113073527 100644 --- a/solution/1600-1699/1640.Check Array Formation Through Concatenation/README.md +++ b/solution/1600-1699/1640.Check Array Formation Through Concatenation/README.md @@ -313,7 +313,9 @@ impl Solution { let mut i = 0; while i < n { match map.get(&arr[i]) { - None => return false, + None => { + return false; + } Some(&j) => { for &item in pieces[j].iter() { if item != arr[i] { diff --git a/solution/1600-1699/1640.Check Array Formation Through Concatenation/README_EN.md b/solution/1600-1699/1640.Check Array Formation Through Concatenation/README_EN.md index d4a8461639582..1a66c81368f33 100644 --- a/solution/1600-1699/1640.Check Array Formation Through Concatenation/README_EN.md +++ b/solution/1600-1699/1640.Check Array Formation Through Concatenation/README_EN.md @@ -284,7 +284,9 @@ impl Solution { let mut i = 0; while i < n { match map.get(&arr[i]) { - None => return false, + None => { + return false; + } Some(&j) => { for &item in pieces[j].iter() { if item != arr[i] { diff --git a/solution/1600-1699/1640.Check Array Formation Through Concatenation/Solution.rs b/solution/1600-1699/1640.Check Array Formation Through Concatenation/Solution.rs index f45ac1794a206..b86fd78349cd3 100644 --- a/solution/1600-1699/1640.Check Array Formation Through Concatenation/Solution.rs +++ b/solution/1600-1699/1640.Check Array Formation Through Concatenation/Solution.rs @@ -9,7 +9,9 @@ impl Solution { let mut i = 0; while i < n { match map.get(&arr[i]) { - None => return false, + None => { + return false; + } Some(&j) => { for &item in pieces[j].iter() { if item != arr[i] { diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md index 7c3369cec6199..4557251c0cf51 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md @@ -202,12 +202,10 @@ impl Solution { let mut ans = 0; for c in s.chars() { - cnt[(c as u8 - 'a' as u8) as usize] += 1; + cnt[((c as u8) - ('a' as u8)) as usize] += 1; } - cnt.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + cnt.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); for i in 1..26 { while cnt[i] >= cnt[i - 1] && cnt[i] > 0 { diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md index 183ab42a7521d..ea5d77bd99288 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md @@ -180,12 +180,10 @@ impl Solution { let mut ans = 0; for c in s.chars() { - cnt[(c as u8 - 'a' as u8) as usize] += 1; + cnt[((c as u8) - ('a' as u8)) as usize] += 1; } - cnt.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + cnt.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); for i in 1..26 { while cnt[i] >= cnt[i - 1] && cnt[i] > 0 { diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.rs b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.rs index 4344e5ee9f24c..0fdc93421b69d 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.rs +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.rs @@ -5,12 +5,10 @@ impl Solution { let mut ans = 0; for c in s.chars() { - cnt[(c as u8 - 'a' as u8) as usize] += 1; + cnt[((c as u8) - ('a' as u8)) as usize] += 1; } - cnt.sort_by(|&lhs, &rhs| { - rhs.cmp(&lhs) - }); + cnt.sort_by(|&lhs, &rhs| { rhs.cmp(&lhs) }); for i in 1..26 { while cnt[i] >= cnt[i - 1] && cnt[i] > 0 { @@ -21,4 +19,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/1600-1699/1656.Design an Ordered Stream/README.md b/solution/1600-1699/1656.Design an Ordered Stream/README.md index 3162fae4dd7f1..dd5905d3a13d5 100644 --- a/solution/1600-1699/1656.Design an Ordered Stream/README.md +++ b/solution/1600-1699/1656.Design an Ordered Stream/README.md @@ -237,9 +237,7 @@ impl OrderedStream { } res } -} - -/** +}/** * Your OrderedStream object will be instantiated and called as such: * let obj = OrderedStream::new(n); * let ret_1: Vec = obj.insert(idKey, value); diff --git a/solution/1600-1699/1656.Design an Ordered Stream/README_EN.md b/solution/1600-1699/1656.Design an Ordered Stream/README_EN.md index 8e1ed9f13164e..4877cb7394b07 100644 --- a/solution/1600-1699/1656.Design an Ordered Stream/README_EN.md +++ b/solution/1600-1699/1656.Design an Ordered Stream/README_EN.md @@ -224,9 +224,7 @@ impl OrderedStream { } res } -} - -/** +}/** * Your OrderedStream object will be instantiated and called as such: * let obj = OrderedStream::new(n); * let ret_1: Vec = obj.insert(idKey, value); diff --git a/solution/1600-1699/1656.Design an Ordered Stream/Solution.rs b/solution/1600-1699/1656.Design an Ordered Stream/Solution.rs index 1c131fee552f0..1af99533a70d7 100644 --- a/solution/1600-1699/1656.Design an Ordered Stream/Solution.rs +++ b/solution/1600-1699/1656.Design an Ordered Stream/Solution.rs @@ -25,10 +25,8 @@ impl OrderedStream { } res } -} - -/** +}/** * Your OrderedStream object will be instantiated and called as such: * let obj = OrderedStream::new(n); * let ret_1: Vec = obj.insert(idKey, value); - */ \ No newline at end of file + */ diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md index 6bdd501e4912f..65fec550eaf36 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md @@ -322,7 +322,7 @@ impl Solution { i += 1; } if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32) + ans = ans.min((n - 1 - (j - i)) as i32); } } if ans == i32::MAX { diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md index d901d093fd16c..466a2afac6c18 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md @@ -290,7 +290,7 @@ impl Solution { i += 1; } if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32) + ans = ans.min((n - 1 - (j - i)) as i32); } } if ans == i32::MAX { diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs index 02207a4f69b3b..b61d6c1af0b9d 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs @@ -15,7 +15,7 @@ impl Solution { i += 1; } if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32) + ans = ans.min((n - 1 - (j - i)) as i32); } } if ans == i32::MAX { diff --git a/solution/1600-1699/1668.Maximum Repeating Substring/README.md b/solution/1600-1699/1668.Maximum Repeating Substring/README.md index aff88baa1141c..dc6e9d8a17864 100644 --- a/solution/1600-1699/1668.Maximum Repeating Substring/README.md +++ b/solution/1600-1699/1668.Maximum Repeating Substring/README.md @@ -176,11 +176,7 @@ impl Solution { for i in 0..=n - m { let s = &sequence[i..i + m]; if s == word { - dp[i] = if (i as i32) - (m as i32) < 0 { - 0 - } else { - dp[i - m] - } + 1; + dp[i] = (if (i as i32) - (m as i32) < 0 { 0 } else { dp[i - m] }) + 1; } } *dp.iter().max().unwrap() diff --git a/solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md b/solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md index 7071b0cf9fe47..fc42103235dca 100644 --- a/solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md +++ b/solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md @@ -159,11 +159,7 @@ impl Solution { for i in 0..=n - m { let s = &sequence[i..i + m]; if s == word { - dp[i] = if (i as i32) - (m as i32) < 0 { - 0 - } else { - dp[i - m] - } + 1; + dp[i] = (if (i as i32) - (m as i32) < 0 { 0 } else { dp[i - m] }) + 1; } } *dp.iter().max().unwrap() diff --git a/solution/1600-1699/1668.Maximum Repeating Substring/Solution.rs b/solution/1600-1699/1668.Maximum Repeating Substring/Solution.rs index f78b9359d7118..e9f0a39799fb8 100644 --- a/solution/1600-1699/1668.Maximum Repeating Substring/Solution.rs +++ b/solution/1600-1699/1668.Maximum Repeating Substring/Solution.rs @@ -9,11 +9,7 @@ impl Solution { for i in 0..=n - m { let s = &sequence[i..i + m]; if s == word { - dp[i] = if (i as i32) - (m as i32) < 0 { - 0 - } else { - dp[i - m] - } + 1; + dp[i] = (if (i as i32) - (m as i32) < 0 { 0 } else { dp[i - m] }) + 1; } } *dp.iter().max().unwrap() diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/README.md b/solution/1600-1699/1678.Goal Parser Interpretation/README.md index 111190f29b238..2112cc1fee9cf 100644 --- a/solution/1600-1699/1678.Goal Parser Interpretation/README.md +++ b/solution/1600-1699/1678.Goal Parser Interpretation/README.md @@ -219,12 +219,8 @@ impl Solution { } if bs[i] == b'(' { ans.push_str({ - if bs[i + 1] == b')' { - "o" - } else { - "al" - } - }) + if bs[i + 1] == b')' { "o" } else { "al" } + }); } } ans diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md b/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md index cf0a863405094..70fd97acc9527 100644 --- a/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md +++ b/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md @@ -197,12 +197,8 @@ impl Solution { } if bs[i] == b'(' { ans.push_str({ - if bs[i + 1] == b')' { - "o" - } else { - "al" - } - }) + if bs[i + 1] == b')' { "o" } else { "al" } + }); } } ans diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/Solution.rs b/solution/1600-1699/1678.Goal Parser Interpretation/Solution.rs index e12250853d120..73f8139f01e3b 100644 --- a/solution/1600-1699/1678.Goal Parser Interpretation/Solution.rs +++ b/solution/1600-1699/1678.Goal Parser Interpretation/Solution.rs @@ -8,12 +8,8 @@ impl Solution { } if bs[i] == b'(' { ans.push_str({ - if bs[i + 1] == b')' { - "o" - } else { - "al" - } - }) + if bs[i + 1] == b')' { "o" } else { "al" } + }); } } ans diff --git a/solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md b/solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md index 492d03becfc00..78f0ace35c224 100644 --- a/solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md +++ b/solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md @@ -268,8 +268,12 @@ impl Solution { l += 1; r -= 1; } - sum if sum > k => r -= 1, - _ => l += 1, + sum if sum > k => { + r -= 1; + } + _ => { + l += 1; + } } } ans diff --git a/solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md b/solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md index 2b53926f2fec4..f32579d1cf0b7 100644 --- a/solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md +++ b/solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md @@ -235,8 +235,12 @@ impl Solution { l += 1; r -= 1; } - sum if sum > k => r -= 1, - _ => l += 1, + sum if sum > k => { + r -= 1; + } + _ => { + l += 1; + } } } ans diff --git a/solution/1600-1699/1679.Max Number of K-Sum Pairs/Solution.rs b/solution/1600-1699/1679.Max Number of K-Sum Pairs/Solution.rs index 9e2fd6202b48d..2d9572bed95e2 100644 --- a/solution/1600-1699/1679.Max Number of K-Sum Pairs/Solution.rs +++ b/solution/1600-1699/1679.Max Number of K-Sum Pairs/Solution.rs @@ -1,21 +1,21 @@ -impl Solution { - pub fn max_operations(nums: Vec, k: i32) -> i32 { - let mut cnt = std::collections::HashMap::new(); - let mut ans = 0; - for x in nums { - let m = k - x; - if let Some(v) = cnt.get_mut(&m) { - ans += 1; - *v -= 1; - if *v == 0 { - cnt.remove(&m); - } - } else if let Some(v) = cnt.get_mut(&x) { - *v += 1; - } else { - cnt.insert(x, 1); - } - } - ans - } -} +impl Solution { + pub fn max_operations(nums: Vec, k: i32) -> i32 { + let mut cnt = std::collections::HashMap::new(); + let mut ans = 0; + for x in nums { + let m = k - x; + if let Some(v) = cnt.get_mut(&m) { + ans += 1; + *v -= 1; + if *v == 0 { + cnt.remove(&m); + } + } else if let Some(v) = cnt.get_mut(&x) { + *v += 1; + } else { + cnt.insert(x, 1); + } + } + ans + } +} diff --git a/solution/1600-1699/1684.Count the Number of Consistent Strings/README.md b/solution/1600-1699/1684.Count the Number of Consistent Strings/README.md index b2f90438e4efc..92de0807d7cdf 100644 --- a/solution/1600-1699/1684.Count the Number of Consistent Strings/README.md +++ b/solution/1600-1699/1684.Count the Number of Consistent Strings/README.md @@ -339,7 +339,7 @@ impl Solution { fn helper(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes().iter() { - res |= 1 << (c - b'a') as i32; + res |= 1 << ((c - b'a') as i32); } res } diff --git a/solution/1600-1699/1684.Count the Number of Consistent Strings/README_EN.md b/solution/1600-1699/1684.Count the Number of Consistent Strings/README_EN.md index 5a300b2ad822e..2d9d38e86bf12 100644 --- a/solution/1600-1699/1684.Count the Number of Consistent Strings/README_EN.md +++ b/solution/1600-1699/1684.Count the Number of Consistent Strings/README_EN.md @@ -313,7 +313,7 @@ impl Solution { fn helper(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes().iter() { - res |= 1 << (c - b'a') as i32; + res |= 1 << ((c - b'a') as i32); } res } diff --git a/solution/1600-1699/1684.Count the Number of Consistent Strings/Solution.rs b/solution/1600-1699/1684.Count the Number of Consistent Strings/Solution.rs index f5cf66a0bb485..0158dff409bd1 100644 --- a/solution/1600-1699/1684.Count the Number of Consistent Strings/Solution.rs +++ b/solution/1600-1699/1684.Count the Number of Consistent Strings/Solution.rs @@ -2,7 +2,7 @@ impl Solution { fn helper(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes().iter() { - res |= 1 << (c - b'a') as i32; + res |= 1 << ((c - b'a') as i32); } res } diff --git a/solution/1600-1699/1694.Reformat Phone Number/README.md b/solution/1600-1699/1694.Reformat Phone Number/README.md index 1483f62fdfaf8..0115e0288e5d0 100644 --- a/solution/1600-1699/1694.Reformat Phone Number/README.md +++ b/solution/1600-1699/1694.Reformat Phone Number/README.md @@ -220,12 +220,15 @@ function reformatNumber(number: string): string { ```rust impl Solution { pub fn reformat_number(number: String) -> String { - let cs: Vec = number.chars().filter(|&c| c != ' ' && c != '-').collect(); + let cs: Vec = number + .chars() + .filter(|&c| c != ' ' && c != '-') + .collect(); let n = cs.len(); cs.iter() .enumerate() .map(|(i, c)| { - if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 { + if ((i + 1) % 3 == 0 && i < n - 2) || (n % 3 == 1 && i == n - 3) { return c.to_string() + &"-"; } c.to_string() diff --git a/solution/1600-1699/1694.Reformat Phone Number/README_EN.md b/solution/1600-1699/1694.Reformat Phone Number/README_EN.md index 9f4278915efa7..038becf2e7c94 100644 --- a/solution/1600-1699/1694.Reformat Phone Number/README_EN.md +++ b/solution/1600-1699/1694.Reformat Phone Number/README_EN.md @@ -181,12 +181,15 @@ function reformatNumber(number: string): string { ```rust impl Solution { pub fn reformat_number(number: String) -> String { - let cs: Vec = number.chars().filter(|&c| c != ' ' && c != '-').collect(); + let cs: Vec = number + .chars() + .filter(|&c| c != ' ' && c != '-') + .collect(); let n = cs.len(); cs.iter() .enumerate() .map(|(i, c)| { - if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 { + if ((i + 1) % 3 == 0 && i < n - 2) || (n % 3 == 1 && i == n - 3) { return c.to_string() + &"-"; } c.to_string() diff --git a/solution/1600-1699/1694.Reformat Phone Number/Solution.rs b/solution/1600-1699/1694.Reformat Phone Number/Solution.rs index b53c313bbdc2f..e0a557cda7688 100644 --- a/solution/1600-1699/1694.Reformat Phone Number/Solution.rs +++ b/solution/1600-1699/1694.Reformat Phone Number/Solution.rs @@ -1,11 +1,14 @@ impl Solution { pub fn reformat_number(number: String) -> String { - let cs: Vec = number.chars().filter(|&c| c != ' ' && c != '-').collect(); + let cs: Vec = number + .chars() + .filter(|&c| c != ' ' && c != '-') + .collect(); let n = cs.len(); cs.iter() .enumerate() .map(|(i, c)| { - if (i + 1) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 { + if ((i + 1) % 3 == 0 && i < n - 2) || (n % 3 == 1 && i == n - 3) { return c.to_string() + &"-"; } c.to_string() diff --git a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README.md b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README.md index b3592118cf3e5..7a6cfa441e5f2 100644 --- a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README.md +++ b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README.md @@ -281,7 +281,11 @@ public: ```rust impl Solution { #[allow(dead_code)] - pub fn distance_limited_paths_exist(n: i32, edge_list: Vec>, queries: Vec>) -> Vec { + pub fn distance_limited_paths_exist( + n: i32, + edge_list: Vec>, + queries: Vec> + ) -> Vec { let mut disjoint_set: Vec = vec![0; n as usize]; let mut ans_vec: Vec = vec![false; queries.len()]; let mut q_vec: Vec = vec![0; queries.len()]; diff --git a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README_EN.md b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README_EN.md index effefd2c747b2..f27d93437a441 100644 --- a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README_EN.md +++ b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README_EN.md @@ -154,7 +154,11 @@ public: ```rust impl Solution { #[allow(dead_code)] - pub fn distance_limited_paths_exist(n: i32, edge_list: Vec>, queries: Vec>) -> Vec { + pub fn distance_limited_paths_exist( + n: i32, + edge_list: Vec>, + queries: Vec> + ) -> Vec { let mut disjoint_set: Vec = vec![0; n as usize]; let mut ans_vec: Vec = vec![false; queries.len()]; let mut q_vec: Vec = vec![0; queries.len()]; diff --git a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.rs b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.rs index edf662e5bd2a0..93f26dfe59109 100644 --- a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.rs +++ b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.rs @@ -1,6 +1,10 @@ impl Solution { #[allow(dead_code)] - pub fn distance_limited_paths_exist(n: i32, edge_list: Vec>, queries: Vec>) -> Vec { + pub fn distance_limited_paths_exist( + n: i32, + edge_list: Vec>, + queries: Vec> + ) -> Vec { let mut disjoint_set: Vec = vec![0; n as usize]; let mut ans_vec: Vec = vec![false; queries.len()]; let mut q_vec: Vec = vec![0; queries.len()]; @@ -65,4 +69,4 @@ impl Solution { let p_d = Solution::find(d, d_set); p_s == p_d } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1708.Largest Subarray Length K/README.md b/solution/1700-1799/1708.Largest Subarray Length K/README.md index d16d1782218d0..efa921d0b8437 100644 --- a/solution/1700-1799/1708.Largest Subarray Length K/README.md +++ b/solution/1700-1799/1708.Largest Subarray Length K/README.md @@ -125,13 +125,13 @@ impl Solution { let mut ret_vec = vec![i32::MIN]; let n = nums.len(); - if n == k as usize { + if n == (k as usize) { return nums; } - for i in 0..=n - k as usize { + for i in 0..=n - (k as usize) { if nums[i] > ret_vec[0] { - ret_vec = nums[i..i + k as usize].to_vec(); + ret_vec = nums[i..i + (k as usize)].to_vec(); } } diff --git a/solution/1700-1799/1708.Largest Subarray Length K/README_EN.md b/solution/1700-1799/1708.Largest Subarray Length K/README_EN.md index f3bc95b5925fc..3814ee762b5ea 100644 --- a/solution/1700-1799/1708.Largest Subarray Length K/README_EN.md +++ b/solution/1700-1799/1708.Largest Subarray Length K/README_EN.md @@ -109,13 +109,13 @@ impl Solution { let mut ret_vec = vec![i32::MIN]; let n = nums.len(); - if n == k as usize { + if n == (k as usize) { return nums; } - for i in 0..=n - k as usize { + for i in 0..=n - (k as usize) { if nums[i] > ret_vec[0] { - ret_vec = nums[i..i + k as usize].to_vec(); + ret_vec = nums[i..i + (k as usize)].to_vec(); } } diff --git a/solution/1700-1799/1708.Largest Subarray Length K/Solution.rs b/solution/1700-1799/1708.Largest Subarray Length K/Solution.rs index 32e90a4598be2..3f1dc186d1591 100644 --- a/solution/1700-1799/1708.Largest Subarray Length K/Solution.rs +++ b/solution/1700-1799/1708.Largest Subarray Length K/Solution.rs @@ -4,16 +4,16 @@ impl Solution { let mut ret_vec = vec![i32::MIN]; let n = nums.len(); - if n == k as usize { + if n == (k as usize) { return nums; } - for i in 0..=n - k as usize { + for i in 0..=n - (k as usize) { if nums[i] > ret_vec[0] { - ret_vec = nums[i..i + k as usize].to_vec(); + ret_vec = nums[i..i + (k as usize)].to_vec(); } } ret_vec } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1726.Tuple with Same Product/README.md b/solution/1700-1799/1726.Tuple with Same Product/README.md index b95200ed5bd35..f5f0557fabcd6 100644 --- a/solution/1700-1799/1726.Tuple with Same Product/README.md +++ b/solution/1700-1799/1726.Tuple with Same Product/README.md @@ -153,7 +153,7 @@ impl Solution { } for v in cnt.values() { - ans += v * (v - 1) / 2; + ans += (v * (v - 1)) / 2; } ans << 3 diff --git a/solution/1700-1799/1726.Tuple with Same Product/README_EN.md b/solution/1700-1799/1726.Tuple with Same Product/README_EN.md index 72fe45de5e7fa..471f0d5cedbbb 100644 --- a/solution/1700-1799/1726.Tuple with Same Product/README_EN.md +++ b/solution/1700-1799/1726.Tuple with Same Product/README_EN.md @@ -143,7 +143,7 @@ impl Solution { } for v in cnt.values() { - ans += v * (v - 1) / 2; + ans += (v * (v - 1)) / 2; } ans << 3 diff --git a/solution/1700-1799/1726.Tuple with Same Product/Solution.rs b/solution/1700-1799/1726.Tuple with Same Product/Solution.rs index 3caf6cfe8abed..af2a83aaa1d9d 100644 --- a/solution/1700-1799/1726.Tuple with Same Product/Solution.rs +++ b/solution/1700-1799/1726.Tuple with Same Product/Solution.rs @@ -1,21 +1,21 @@ -use std::collections::HashMap; - -impl Solution { - pub fn tuple_same_product(nums: Vec) -> i32 { - let mut cnt: HashMap = HashMap::new(); - let mut ans = 0; - - for i in 1..nums.len() { - for j in 0..i { - let x = nums[i] * nums[j]; - *cnt.entry(x).or_insert(0) += 1; - } - } - - for v in cnt.values() { - ans += v * (v - 1) / 2; - } - - ans << 3 - } -} \ No newline at end of file +use std::collections::HashMap; + +impl Solution { + pub fn tuple_same_product(nums: Vec) -> i32 { + let mut cnt: HashMap = HashMap::new(); + let mut ans = 0; + + for i in 1..nums.len() { + for j in 0..i { + let x = nums[i] * nums[j]; + *cnt.entry(x).or_insert(0) += 1; + } + } + + for v in cnt.values() { + ans += (v * (v - 1)) / 2; + } + + ans << 3 + } +} diff --git a/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/Solution.rs b/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/Solution.rs index e0460ef3fa027..6deb48e8acce2 100644 --- a/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/Solution.rs +++ b/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/Solution.rs @@ -22,4 +22,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1748.Sum of Unique Elements/Solution.rs b/solution/1700-1799/1748.Sum of Unique Elements/Solution.rs index 7f91a59b4b3c5..6f2ebc6b316ad 100644 --- a/solution/1700-1799/1748.Sum of Unique Elements/Solution.rs +++ b/solution/1700-1799/1748.Sum of Unique Elements/Solution.rs @@ -12,4 +12,4 @@ impl Solution { } ans as i32 } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/Solution.rs b/solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/Solution.rs index 82d8a25b37d07..dbc4d00400393 100644 --- a/solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/Solution.rs +++ b/solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/Solution.rs @@ -10,4 +10,4 @@ impl Solution { } ans } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md index 22179ab58ecab..56ffb54d14f6d 100644 --- a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md +++ b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md @@ -189,7 +189,7 @@ impl Solution { start += 1; end -= 1; } - 0.max(end - start + 1) as i32 + (0).max(end - start + 1) as i32 } } ``` diff --git a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md index dfb2ff504bcd6..503fa79ccae1d 100644 --- a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md +++ b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md @@ -179,7 +179,7 @@ impl Solution { start += 1; end -= 1; } - 0.max(end - start + 1) as i32 + (0).max(end - start + 1) as i32 } } ``` diff --git a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/Solution.rs b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/Solution.rs index 5afbd0a848915..a09dc48aa6ee8 100644 --- a/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/Solution.rs +++ b/solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/Solution.rs @@ -14,6 +14,6 @@ impl Solution { start += 1; end -= 1; } - 0.max(end - start + 1) as i32 + (0).max(end - start + 1) as i32 } } diff --git a/solution/1700-1799/1759.Count Number of Homogenous Substrings/README.md b/solution/1700-1799/1759.Count Number of Homogenous Substrings/README.md index 0188f7bed56da..abfe7951d722c 100644 --- a/solution/1700-1799/1759.Count Number of Homogenous Substrings/README.md +++ b/solution/1700-1799/1759.Count Number of Homogenous Substrings/README.md @@ -236,7 +236,7 @@ function countHomogenous(s: string): number { ```rust impl Solution { pub fn count_homogenous(s: String) -> i32 { - const MOD: usize = 1e9 as usize + 7; + const MOD: usize = (1e9 as usize) + 7; let s = s.as_bytes(); let n = s.len(); let mut ans = 0; diff --git a/solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md b/solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md index de75b81cbac3f..6a85989be694f 100644 --- a/solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md +++ b/solution/1700-1799/1759.Count Number of Homogenous Substrings/README_EN.md @@ -218,7 +218,7 @@ function countHomogenous(s: string): number { ```rust impl Solution { pub fn count_homogenous(s: String) -> i32 { - const MOD: usize = 1e9 as usize + 7; + const MOD: usize = (1e9 as usize) + 7; let s = s.as_bytes(); let n = s.len(); let mut ans = 0; diff --git a/solution/1700-1799/1759.Count Number of Homogenous Substrings/Solution.rs b/solution/1700-1799/1759.Count Number of Homogenous Substrings/Solution.rs index 1aef1ce2ed342..81c4168e88834 100644 --- a/solution/1700-1799/1759.Count Number of Homogenous Substrings/Solution.rs +++ b/solution/1700-1799/1759.Count Number of Homogenous Substrings/Solution.rs @@ -1,6 +1,6 @@ impl Solution { pub fn count_homogenous(s: String) -> i32 { - const MOD: usize = 1e9 as usize + 7; + const MOD: usize = (1e9 as usize) + 7; let s = s.as_bytes(); let n = s.len(); let mut ans = 0; diff --git a/solution/1700-1799/1765.Map of Highest Peak/README.md b/solution/1700-1799/1765.Map of Highest Peak/README.md index 22018a5cb5117..a3094eb792fe5 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/README.md +++ b/solution/1700-1799/1765.Map of Highest Peak/README.md @@ -272,7 +272,7 @@ impl Solution { let m = is_water[0].len(); let mut ret_vec = vec![vec![-1; m]; n]; let mut q: VecDeque<(usize, usize)> = VecDeque::new(); - let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)]; + let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0, 1)]; // Initialize the return vector for i in 0..n { @@ -292,12 +292,13 @@ impl Solution { // Traverse through the vis pair for d in &vis_pair { let (dx, dy) = *d; - if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) { - if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 { + if Self::check_bounds((x as i32) + dx, (y as i32) + dy, n as i32, m as i32) { + if ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] == -1 { // This cell hasn't been visited, update its height - ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1; + ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] = + ret_vec[x][y] + 1; // Enqueue the current cell - q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize)); + q.push_back((((x as i32) + dx) as usize, ((y as i32) + dy) as usize)); } } } diff --git a/solution/1700-1799/1765.Map of Highest Peak/README_EN.md b/solution/1700-1799/1765.Map of Highest Peak/README_EN.md index a529a6788ef26..b6ccacf1cb27d 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/README_EN.md +++ b/solution/1700-1799/1765.Map of Highest Peak/README_EN.md @@ -264,7 +264,7 @@ impl Solution { let m = is_water[0].len(); let mut ret_vec = vec![vec![-1; m]; n]; let mut q: VecDeque<(usize, usize)> = VecDeque::new(); - let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)]; + let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0, 1)]; // Initialize the return vector for i in 0..n { @@ -284,12 +284,13 @@ impl Solution { // Traverse through the vis pair for d in &vis_pair { let (dx, dy) = *d; - if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) { - if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 { + if Self::check_bounds((x as i32) + dx, (y as i32) + dy, n as i32, m as i32) { + if ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] == -1 { // This cell hasn't been visited, update its height - ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1; + ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] = + ret_vec[x][y] + 1; // Enqueue the current cell - q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize)); + q.push_back((((x as i32) + dx) as usize, ((y as i32) + dy) as usize)); } } } diff --git a/solution/1700-1799/1765.Map of Highest Peak/Solution.rs b/solution/1700-1799/1765.Map of Highest Peak/Solution.rs index 754962b16d436..4ab73d5fddf89 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/Solution.rs +++ b/solution/1700-1799/1765.Map of Highest Peak/Solution.rs @@ -7,7 +7,7 @@ impl Solution { let m = is_water[0].len(); let mut ret_vec = vec![vec![-1; m]; n]; let mut q: VecDeque<(usize, usize)> = VecDeque::new(); - let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0 ,1)]; + let vis_pair: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, -1), (0, 1)]; // Initialize the return vector for i in 0..n { @@ -27,12 +27,13 @@ impl Solution { // Traverse through the vis pair for d in &vis_pair { let (dx, dy) = *d; - if Self::check_bounds(x as i32 + dx, y as i32 + dy, n as i32, m as i32) { - if ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] == -1 { + if Self::check_bounds((x as i32) + dx, (y as i32) + dy, n as i32, m as i32) { + if ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] == -1 { // This cell hasn't been visited, update its height - ret_vec[(x as i32 + dx) as usize][(y as i32 + dy) as usize] = ret_vec[x][y] + 1; + ret_vec[((x as i32) + dx) as usize][((y as i32) + dy) as usize] = + ret_vec[x][y] + 1; // Enqueue the current cell - q.push_back(((x as i32 + dx) as usize, (y as i32 + dy) as usize)); + q.push_back((((x as i32) + dx) as usize, ((y as i32) + dy) as usize)); } } } @@ -45,4 +46,4 @@ impl Solution { fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { i >= 0 && i < n && j >= 0 && j < m } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md index a8d758f5a0890..5f5f941f3963b 100644 --- a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md @@ -314,7 +314,10 @@ impl Solution { } right[i] = right[i + 1] + count; } - (0..n).into_iter().map(|i| left[i] + right[i]).collect() + (0..n) + .into_iter() + .map(|i| left[i] + right[i]) + .collect() } } ``` diff --git a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md index 499bdbb07957d..14511624f2ec9 100644 --- a/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md +++ b/solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md @@ -296,7 +296,10 @@ impl Solution { } right[i] = right[i + 1] + count; } - (0..n).into_iter().map(|i| left[i] + right[i]).collect() + (0..n) + .into_iter() + .map(|i| left[i] + right[i]) + .collect() } } ``` diff --git a/solution/1700-1799/1773.Count Items Matching a Rule/README.md b/solution/1700-1799/1773.Count Items Matching a Rule/README.md index 283e5a0f90226..4b2f781d620ed 100644 --- a/solution/1700-1799/1773.Count Items Matching a Rule/README.md +++ b/solution/1700-1799/1773.Count Items Matching a Rule/README.md @@ -146,14 +146,11 @@ function countMatches(items: string[][], ruleKey: string, ruleValue: string): nu ```rust impl Solution { pub fn count_matches(items: Vec>, rule_key: String, rule_value: String) -> i32 { - let key = if rule_key == "type" { - 0 - } else if rule_key == "color" { - 1 - } else { - 2 - }; - items.iter().filter(|v| v[key] == rule_value).count() as i32 + let key = if rule_key == "type" { 0 } else if rule_key == "color" { 1 } else { 2 }; + items + .iter() + .filter(|v| v[key] == rule_value) + .count() as i32 } } ``` diff --git a/solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md b/solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md index 77e06849b9d0b..6857c0eb8c49d 100644 --- a/solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md +++ b/solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md @@ -128,14 +128,11 @@ function countMatches(items: string[][], ruleKey: string, ruleValue: string): nu ```rust impl Solution { pub fn count_matches(items: Vec>, rule_key: String, rule_value: String) -> i32 { - let key = if rule_key == "type" { - 0 - } else if rule_key == "color" { - 1 - } else { - 2 - }; - items.iter().filter(|v| v[key] == rule_value).count() as i32 + let key = if rule_key == "type" { 0 } else if rule_key == "color" { 1 } else { 2 }; + items + .iter() + .filter(|v| v[key] == rule_value) + .count() as i32 } } ``` diff --git a/solution/1700-1799/1773.Count Items Matching a Rule/Solution.rs b/solution/1700-1799/1773.Count Items Matching a Rule/Solution.rs index 641af5a81f21f..3c2f860a1ecfd 100644 --- a/solution/1700-1799/1773.Count Items Matching a Rule/Solution.rs +++ b/solution/1700-1799/1773.Count Items Matching a Rule/Solution.rs @@ -1,12 +1,9 @@ impl Solution { pub fn count_matches(items: Vec>, rule_key: String, rule_value: String) -> i32 { - let key = if rule_key == "type" { - 0 - } else if rule_key == "color" { - 1 - } else { - 2 - }; - items.iter().filter(|v| v[key] == rule_value).count() as i32 + let key = if rule_key == "type" { 0 } else if rule_key == "color" { 1 } else { 2 }; + items + .iter() + .filter(|v| v[key] == rule_value) + .count() as i32 } } diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs index ec7093a114f0d..3a8ed24efa10c 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs @@ -1,20 +1,20 @@ -use std::collections::HashMap; - -impl Solution { - pub fn maximum_beauty(flowers: Vec) -> i32 { - let mut s = vec![0; flowers.len() + 1]; - let mut d = HashMap::new(); - let mut ans = i32::MIN; - - for (i, &v) in flowers.iter().enumerate() { - if let Some(&j) = d.get(&v) { - ans = ans.max(s[i] - s[j + 1] + v * 2); - } else { - d.insert(v, i); - } - s[i + 1] = s[i] + v.max(0); - } - - ans - } -} +use std::collections::HashMap; + +impl Solution { + pub fn maximum_beauty(flowers: Vec) -> i32 { + let mut s = vec![0; flowers.len() + 1]; + let mut d = HashMap::new(); + let mut ans = i32::MIN; + + for (i, &v) in flowers.iter().enumerate() { + if let Some(&j) = d.get(&v) { + ans = ans.max(s[i] - s[j + 1] + v * 2); + } else { + d.insert(v, i); + } + s[i + 1] = s[i] + v.max(0); + } + + ans + } +} diff --git a/solution/1700-1799/1797.Design Authentication Manager/README.md b/solution/1700-1799/1797.Design Authentication Manager/README.md index 00d37d86c630f..5b83ae891e3a6 100644 --- a/solution/1700-1799/1797.Design Authentication Manager/README.md +++ b/solution/1700-1799/1797.Design Authentication Manager/README.md @@ -303,12 +303,12 @@ impl AuthenticationManager { } fn count_unexpired_tokens(&self, current_time: i32) -> i32 { - self.map.values().filter(|&time| *time > current_time).count() as i32 + self.map + .values() + .filter(|&time| *time > current_time) + .count() as i32 } -} - - -/** +}/** * Your AuthenticationManager object will be instantiated and called as such: * let obj = AuthenticationManager::new(timeToLive); * obj.generate(tokenId, currentTime); diff --git a/solution/1700-1799/1797.Design Authentication Manager/README_EN.md b/solution/1700-1799/1797.Design Authentication Manager/README_EN.md index 714ffb0ae4646..b0a12cbf17e1e 100644 --- a/solution/1700-1799/1797.Design Authentication Manager/README_EN.md +++ b/solution/1700-1799/1797.Design Authentication Manager/README_EN.md @@ -281,12 +281,12 @@ impl AuthenticationManager { } fn count_unexpired_tokens(&self, current_time: i32) -> i32 { - self.map.values().filter(|&time| *time > current_time).count() as i32 + self.map + .values() + .filter(|&time| *time > current_time) + .count() as i32 } -} - - -/** +}/** * Your AuthenticationManager object will be instantiated and called as such: * let obj = AuthenticationManager::new(timeToLive); * obj.generate(tokenId, currentTime); diff --git a/solution/1700-1799/1797.Design Authentication Manager/Solution.rs b/solution/1700-1799/1797.Design Authentication Manager/Solution.rs index 2c8213ed035b2..fdadb4bf07161 100644 --- a/solution/1700-1799/1797.Design Authentication Manager/Solution.rs +++ b/solution/1700-1799/1797.Design Authentication Manager/Solution.rs @@ -28,15 +28,15 @@ impl AuthenticationManager { } fn count_unexpired_tokens(&self, current_time: i32) -> i32 { - self.map.values().filter(|&time| *time > current_time).count() as i32 + self.map + .values() + .filter(|&time| *time > current_time) + .count() as i32 } -} - - -/** +}/** * Your AuthenticationManager object will be instantiated and called as such: * let obj = AuthenticationManager::new(timeToLive); * obj.generate(tokenId, currentTime); * obj.renew(tokenId, currentTime); * let ret_3: i32 = obj.count_unexpired_tokens(currentTime); - */ \ No newline at end of file + */ diff --git a/solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md b/solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md index 07b366155f8aa..dc0e5c358a38a 100644 --- a/solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md +++ b/solution/1800-1899/1812.Determine Color of a Chessboard Square/README.md @@ -132,7 +132,7 @@ function squareIsWhite(coordinates: string): boolean { impl Solution { pub fn square_is_white(coordinates: String) -> bool { let s = coordinates.as_bytes(); - s[0] + s[1] & 1 == 1 + ((s[0] + s[1]) & 1) == 1 } } ``` diff --git a/solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md b/solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md index f4d6f9acca03e..5aaf1b496589f 100644 --- a/solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md +++ b/solution/1800-1899/1812.Determine Color of a Chessboard Square/README_EN.md @@ -114,7 +114,7 @@ function squareIsWhite(coordinates: string): boolean { impl Solution { pub fn square_is_white(coordinates: String) -> bool { let s = coordinates.as_bytes(); - s[0] + s[1] & 1 == 1 + ((s[0] + s[1]) & 1) == 1 } } ``` diff --git a/solution/1800-1899/1812.Determine Color of a Chessboard Square/Solution.rs b/solution/1800-1899/1812.Determine Color of a Chessboard Square/Solution.rs index 3849b582059e1..a56d8959416ea 100644 --- a/solution/1800-1899/1812.Determine Color of a Chessboard Square/Solution.rs +++ b/solution/1800-1899/1812.Determine Color of a Chessboard Square/Solution.rs @@ -1,6 +1,6 @@ impl Solution { pub fn square_is_white(coordinates: String) -> bool { let s = coordinates.as_bytes(); - s[0] + s[1] & 1 == 1 + ((s[0] + s[1]) & 1) == 1 } } diff --git a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README.md b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README.md index b3a8245807ecf..2d25801b4b6ad 100644 --- a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README.md +++ b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README.md @@ -160,7 +160,7 @@ impl Solution { let mut ans = 0; let mut max = 0; for &v in nums.iter() { - ans += 0.max(max + 1 - v); + ans += (0).max(max + 1 - v); max = v.max(max + 1); } ans diff --git a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md index 1d9f63faaadb4..bdbbaff652bef 100644 --- a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md +++ b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md @@ -145,7 +145,7 @@ impl Solution { let mut ans = 0; let mut max = 0; for &v in nums.iter() { - ans += 0.max(max + 1 - v); + ans += (0).max(max + 1 - v); max = v.max(max + 1); } ans diff --git a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.rs b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.rs index 2379917ad71c2..5c12101a396a1 100644 --- a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.rs +++ b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.rs @@ -3,7 +3,7 @@ impl Solution { let mut ans = 0; let mut max = 0; for &v in nums.iter() { - ans += 0.max(max + 1 - v); + ans += (0).max(max + 1 - v); max = v.max(max + 1); } ans diff --git a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md index 5be17ea8bd8dc..cd6b16bc3cfa5 100644 --- a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md +++ b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md @@ -173,7 +173,7 @@ impl Solution { let r = v[2].pow(2); let mut count = 0; for p in points.iter() { - if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r { + if (p[0] - cx).pow(2) + (p[1] - cy).pow(2) <= r { count += 1; } } diff --git a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md index 6cbe4c7f3b350..f2ba1e08c875f 100644 --- a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md +++ b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md @@ -162,7 +162,7 @@ impl Solution { let r = v[2].pow(2); let mut count = 0; for p in points.iter() { - if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r { + if (p[0] - cx).pow(2) + (p[1] - cy).pow(2) <= r { count += 1; } } diff --git a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/Solution.rs b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/Solution.rs index 8a50444b290bb..0a4443f9623ec 100644 --- a/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/Solution.rs +++ b/solution/1800-1899/1828.Queries on Number of Points Inside a Circle/Solution.rs @@ -8,7 +8,7 @@ impl Solution { let r = v[2].pow(2); let mut count = 0; for p in points.iter() { - if ((p[0] - cx).pow(2) + (p[1] - cy).pow(2)) <= r { + if (p[0] - cx).pow(2) + (p[1] - cy).pow(2) <= r { count += 1; } } diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md index 96bfa01608b0f..b05ffb6e1b257 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md @@ -204,7 +204,7 @@ impl Solution { pub fn check_if_pangram(sentence: String) -> bool { let mut mark = 0; for c in sentence.as_bytes() { - mark |= 1 << *c - b'a'; + mark |= 1 << (*c - b'a'); } mark == (1 << 26) - 1 } diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md index a6d98fb5addcc..47c1b44624817 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md @@ -178,7 +178,7 @@ impl Solution { pub fn check_if_pangram(sentence: String) -> bool { let mut mark = 0; for c in sentence.as_bytes() { - mark |= 1 << *c - b'a'; + mark |= 1 << (*c - b'a'); } mark == (1 << 26) - 1 } diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.rs b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.rs index d41305e55d5c4..3017c642b8163 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.rs +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.rs @@ -2,7 +2,7 @@ impl Solution { pub fn check_if_pangram(sentence: String) -> bool { let mut mark = 0; for c in sentence.as_bytes() { - mark |= 1 << *c - b'a'; + mark |= 1 << (*c - b'a'); } mark == (1 << 26) - 1 } diff --git a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README.md b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README.md index bc679e13665f5..f2d942b28438e 100644 --- a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README.md +++ b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README.md @@ -323,7 +323,7 @@ impl Solution { right = mid; } } - res = res.max((left - i - 1) as i32) + res = res.max((left - i - 1) as i32); } res } @@ -339,9 +339,9 @@ impl Solution { let mut j = 0; for i in 0..m { while j < n && nums1[i] <= nums2[j] { - j += 1 + j += 1; } - res = res.max((j - i - 1) as i32) + res = res.max((j - i - 1) as i32); } res } diff --git a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README_EN.md b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README_EN.md index 6925ea0b00fce..c1f20700deca8 100644 --- a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README_EN.md +++ b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README_EN.md @@ -301,7 +301,7 @@ impl Solution { right = mid; } } - res = res.max((left - i - 1) as i32) + res = res.max((left - i - 1) as i32); } res } @@ -317,9 +317,9 @@ impl Solution { let mut j = 0; for i in 0..m { while j < n && nums1[i] <= nums2[j] { - j += 1 + j += 1; } - res = res.max((j - i - 1) as i32) + res = res.max((j - i - 1) as i32); } res } diff --git a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/Solution.rs b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/Solution.rs index 9199f8d77016e..6b35b53665f32 100644 --- a/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/Solution.rs +++ b/solution/1800-1899/1855.Maximum Distance Between a Pair of Values/Solution.rs @@ -6,10 +6,10 @@ impl Solution { let mut j = 0; for i in 0..m { while j < n && nums1[i] <= nums2[j] { - j += 1 + j += 1; } - res = res.max((j - i - 1) as i32) + res = res.max((j - i - 1) as i32); } res } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md index 45c4cc15f7a03..5d82cfe9e7291 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md @@ -269,12 +269,12 @@ impl Solution { let n = dist.len(); let check = |speed| { - let mut cur = 0.; + let mut cur = 0.0; for (i, &d) in dist.iter().enumerate() { if i == n - 1 { - cur += d as f64 / speed as f64; + cur += (d as f64) / (speed as f64); } else { - cur += (d as f64 / speed as f64).ceil(); + cur += ((d as f64) / (speed as f64)).ceil(); } } cur <= hour diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md index b5f7b71f352be..588262b9abd13 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md @@ -244,12 +244,12 @@ impl Solution { let n = dist.len(); let check = |speed| { - let mut cur = 0.; + let mut cur = 0.0; for (i, &d) in dist.iter().enumerate() { if i == n - 1 { - cur += d as f64 / speed as f64; + cur += (d as f64) / (speed as f64); } else { - cur += (d as f64 / speed as f64).ceil(); + cur += ((d as f64) / (speed as f64)).ceil(); } } cur <= hour diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.rs b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.rs index eaab58e318c74..018def82d7bd1 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.rs +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.rs @@ -3,12 +3,12 @@ impl Solution { let n = dist.len(); let check = |speed| { - let mut cur = 0.; + let mut cur = 0.0; for (i, &d) in dist.iter().enumerate() { if i == n - 1 { - cur += d as f64 / speed as f64; + cur += (d as f64) / (speed as f64); } else { - cur += (d as f64 / speed as f64).ceil(); + cur += ((d as f64) / (speed as f64)).ceil(); } } cur <= hour diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md index 90f77b9ef3c0a..65675ec21e361 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md @@ -178,7 +178,7 @@ impl Solution { fn calc(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes() { - res = res * 10 + (c - b'a') as i32; + res = res * 10 + ((c - b'a') as i32); } res } diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md index ade7d661ff847..f62ef2176aaf9 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md @@ -172,7 +172,7 @@ impl Solution { fn calc(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes() { - res = res * 10 + (c - b'a') as i32; + res = res * 10 + ((c - b'a') as i32); } res } diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs index 12a7fe6897b45..d7efedab6c5c1 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs @@ -2,7 +2,7 @@ impl Solution { fn calc(s: &String) -> i32 { let mut res = 0; for c in s.as_bytes() { - res = res * 10 + (c - b'a') as i32; + res = res * 10 + ((c - b'a') as i32); } res } diff --git a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md index 35bc232766e40..8d150771ebcc6 100644 --- a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md +++ b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md @@ -198,9 +198,10 @@ impl Solution { }) .collect(); - pre_sum - .binary_search(&(k as i64 % pre_sum.last().unwrap())) - .map_or_else(|e| e, |v| v + 1) as i32 + pre_sum.binary_search(&((k as i64) % pre_sum.last().unwrap())).map_or_else( + |e| e, + |v| v + 1 + ) as i32 } } ``` diff --git a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md index 42b03b1736c39..d427ed28f408d 100644 --- a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md +++ b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md @@ -137,9 +137,10 @@ impl Solution { }) .collect(); - pre_sum - .binary_search(&(k as i64 % pre_sum.last().unwrap())) - .map_or_else(|e| e, |v| v + 1) as i32 + pre_sum.binary_search(&((k as i64) % pre_sum.last().unwrap())).map_or_else( + |e| e, + |v| v + 1 + ) as i32 } } ``` diff --git a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/Solution.rs b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/Solution.rs index 742a70f490d9b..da08095089d35 100644 --- a/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/Solution.rs +++ b/solution/1800-1899/1894.Find the Student that Will Replace the Chalk/Solution.rs @@ -9,8 +9,9 @@ impl Solution { }) .collect(); - pre_sum - .binary_search(&(k as i64 % pre_sum.last().unwrap())) - .map_or_else(|e| e, |v| v + 1) as i32 + pre_sum.binary_search(&((k as i64) % pre_sum.last().unwrap())).map_or_else( + |e| e, + |v| v + 1 + ) as i32 } } diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md index 7b897843715c2..ad201c3bd4e72 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README.md @@ -183,8 +183,8 @@ impl Solution { true }; for i in 1..nums.len() { - if nums[i-1] >= nums[i] { - return check(i-1) || check(i) + if nums[i - 1] >= nums[i] { + return check(i - 1) || check(i); } } true diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md index 0d6f8544a076e..fbd6c370b064e 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/README_EN.md @@ -169,8 +169,8 @@ impl Solution { true }; for i in 1..nums.len() { - if nums[i-1] >= nums[i] { - return check(i-1) || check(i) + if nums[i - 1] >= nums[i] { + return check(i - 1) || check(i); } } true diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs index d7a4d69136a77..0908b1f88652b 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.rs @@ -15,10 +15,10 @@ impl Solution { true }; for i in 1..nums.len() { - if nums[i-1] >= nums[i] { - return check(i-1) || check(i) + if nums[i - 1] >= nums[i] { + return check(i - 1) || check(i); } } true } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1920.Build Array from Permutation/README.md b/solution/1900-1999/1920.Build Array from Permutation/README.md index e036421a86472..55f85bae913eb 100644 --- a/solution/1900-1999/1920.Build Array from Permutation/README.md +++ b/solution/1900-1999/1920.Build Array from Permutation/README.md @@ -128,7 +128,9 @@ function buildArray(nums: number[]): number[] { ```rust impl Solution { pub fn build_array(nums: Vec) -> Vec { - nums.iter().map(|&v| nums[v as usize]).collect() + nums.iter() + .map(|&v| nums[v as usize]) + .collect() } } ``` diff --git a/solution/1900-1999/1920.Build Array from Permutation/README_EN.md b/solution/1900-1999/1920.Build Array from Permutation/README_EN.md index f65d72673d3b9..367932c7fddec 100644 --- a/solution/1900-1999/1920.Build Array from Permutation/README_EN.md +++ b/solution/1900-1999/1920.Build Array from Permutation/README_EN.md @@ -123,7 +123,9 @@ function buildArray(nums: number[]): number[] { ```rust impl Solution { pub fn build_array(nums: Vec) -> Vec { - nums.iter().map(|&v| nums[v as usize]).collect() + nums.iter() + .map(|&v| nums[v as usize]) + .collect() } } ``` diff --git a/solution/1900-1999/1920.Build Array from Permutation/Solution.rs b/solution/1900-1999/1920.Build Array from Permutation/Solution.rs index 4a31e9c504e42..9102b43d9b80c 100644 --- a/solution/1900-1999/1920.Build Array from Permutation/Solution.rs +++ b/solution/1900-1999/1920.Build Array from Permutation/Solution.rs @@ -1,5 +1,7 @@ impl Solution { pub fn build_array(nums: Vec) -> Vec { - nums.iter().map(|&v| nums[v as usize]).collect() + nums.iter() + .map(|&v| nums[v as usize]) + .collect() } } diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md index 92b921b62f4b8..93aac2a878718 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md @@ -188,13 +188,13 @@ impl Solution { pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 { let mut s = vec![false; 26]; for c in broken_letters.chars() { - s[c as usize - 'a' as usize] = true; + s[(c as usize) - ('a' as usize)] = true; } let mut ans = 0; let words = text.split_whitespace(); for w in words { for c in w.chars() { - if s[c as usize - 'a' as usize] { + if s[(c as usize) - ('a' as usize)] { ans -= 1; break; } diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md index bf2a309f643c5..a44d0c346b73c 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md @@ -181,13 +181,13 @@ impl Solution { pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 { let mut s = vec![false; 26]; for c in broken_letters.chars() { - s[c as usize - 'a' as usize] = true; + s[(c as usize) - ('a' as usize)] = true; } let mut ans = 0; let words = text.split_whitespace(); for w in words { for c in w.chars() { - if s[c as usize - 'a' as usize] { + if s[(c as usize) - ('a' as usize)] { ans -= 1; break; } diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.rs b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.rs index 6aad80a0d30f5..2ac81034029b9 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.rs +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.rs @@ -1,20 +1,20 @@ -impl Solution { - pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 { - let mut s = vec![false; 26]; - for c in broken_letters.chars() { - s[c as usize - 'a' as usize] = true; - } - let mut ans = 0; - let words = text.split_whitespace(); - for w in words { - for c in w.chars() { - if s[c as usize - 'a' as usize] { - ans -= 1; - break; - } - } - ans += 1; - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 { + let mut s = vec![false; 26]; + for c in broken_letters.chars() { + s[(c as usize) - ('a' as usize)] = true; + } + let mut ans = 0; + let words = text.split_whitespace(); + for w in words { + for c in w.chars() { + if s[(c as usize) - ('a' as usize)] { + ans -= 1; + break; + } + } + ans += 1; + } + ans + } +} diff --git a/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.rs b/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.rs index 8d87074204b36..a957bbcbdd5a5 100644 --- a/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.rs +++ b/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn add_rungs(rungs: Vec, dist: i32) -> i32 { - let mut ans = 0; - let mut prev = 0; - - for &x in rungs.iter() { - ans += (x - prev - 1) / dist; - prev = x; - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn add_rungs(rungs: Vec, dist: i32) -> i32 { + let mut ans = 0; + let mut prev = 0; + + for &x in rungs.iter() { + ans += (x - prev - 1) / dist; + prev = x; + } + + ans + } +} diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs index f42912594c198..4b4a4dce68760 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs @@ -25,4 +25,4 @@ impl Solution { } d_set[x as usize] } -} \ No newline at end of file +} diff --git a/solution/2000-2099/2000.Reverse Prefix of Word/Solution.rs b/solution/2000-2099/2000.Reverse Prefix of Word/Solution.rs index 4bdffb76e2fa9..efbd8352323f6 100644 --- a/solution/2000-2099/2000.Reverse Prefix of Word/Solution.rs +++ b/solution/2000-2099/2000.Reverse Prefix of Word/Solution.rs @@ -5,4 +5,4 @@ impl Solution { None => word, } } -} \ No newline at end of file +} diff --git a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README.md b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README.md index b4c5cdddd7626..0d284d5cce617 100644 --- a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README.md +++ b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README.md @@ -285,12 +285,18 @@ func smallestMissingValueSubtree(parents []int, nums []int) []int { ```rust impl Solution { pub fn smallest_missing_value_subtree(parents: Vec, nums: Vec) -> Vec { - fn dfs(i: usize, vis: &mut Vec, has: &mut Vec, g: &Vec>, nums: &Vec) { + fn dfs( + i: usize, + vis: &mut Vec, + has: &mut Vec, + g: &Vec>, + nums: &Vec + ) { if vis[i] { return; } vis[i] = true; - if nums[i] < has.len() as i32 { + if nums[i] < (has.len() as i32) { has[nums[i] as usize] = true; } for &j in &g[i] { diff --git a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README_EN.md b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README_EN.md index 69627a06c403c..5fc3e00da5e39 100644 --- a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README_EN.md +++ b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/README_EN.md @@ -274,12 +274,18 @@ func smallestMissingValueSubtree(parents []int, nums []int) []int { ```rust impl Solution { pub fn smallest_missing_value_subtree(parents: Vec, nums: Vec) -> Vec { - fn dfs(i: usize, vis: &mut Vec, has: &mut Vec, g: &Vec>, nums: &Vec) { + fn dfs( + i: usize, + vis: &mut Vec, + has: &mut Vec, + g: &Vec>, + nums: &Vec + ) { if vis[i] { return; } vis[i] = true; - if nums[i] < has.len() as i32 { + if nums[i] < (has.len() as i32) { has[nums[i] as usize] = true; } for &j in &g[i] { diff --git a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/Solution.rs b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/Solution.rs index ef09d47ae102d..2cca0b52ea2a0 100644 --- a/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/Solution.rs +++ b/solution/2000-2099/2003.Smallest Missing Genetic Value in Each Subtree/Solution.rs @@ -1,45 +1,51 @@ -impl Solution { - pub fn smallest_missing_value_subtree(parents: Vec, nums: Vec) -> Vec { - fn dfs(i: usize, vis: &mut Vec, has: &mut Vec, g: &Vec>, nums: &Vec) { - if vis[i] { - return; - } - vis[i] = true; - if nums[i] < has.len() as i32 { - has[nums[i] as usize] = true; - } - for &j in &g[i] { - dfs(j, vis, has, g, nums); - } - } - - let n = nums.len(); - let mut ans = vec![1; n]; - let mut g: Vec> = vec![vec![]; n]; - let mut idx = -1; - for (i, &p) in parents.iter().enumerate() { - if i > 0 { - g[p as usize].push(i); - } - if nums[i] == 1 { - idx = i as i32; - } - } - if idx == -1 { - return ans; - } - let mut vis = vec![false; n]; - let mut has = vec![false; (n + 2) as usize]; - let mut i = 2; - let mut idx_mut = idx; - while idx_mut != -1 { - dfs(idx_mut as usize, &mut vis, &mut has, &g, &nums); - while has[i] { - i += 1; - } - ans[idx_mut as usize] = i as i32; - idx_mut = parents[idx_mut as usize]; - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn smallest_missing_value_subtree(parents: Vec, nums: Vec) -> Vec { + fn dfs( + i: usize, + vis: &mut Vec, + has: &mut Vec, + g: &Vec>, + nums: &Vec + ) { + if vis[i] { + return; + } + vis[i] = true; + if nums[i] < (has.len() as i32) { + has[nums[i] as usize] = true; + } + for &j in &g[i] { + dfs(j, vis, has, g, nums); + } + } + + let n = nums.len(); + let mut ans = vec![1; n]; + let mut g: Vec> = vec![vec![]; n]; + let mut idx = -1; + for (i, &p) in parents.iter().enumerate() { + if i > 0 { + g[p as usize].push(i); + } + if nums[i] == 1 { + idx = i as i32; + } + } + if idx == -1 { + return ans; + } + let mut vis = vec![false; n]; + let mut has = vec![false; (n + 2) as usize]; + let mut i = 2; + let mut idx_mut = idx; + while idx_mut != -1 { + dfs(idx_mut as usize, &mut vis, &mut has, &g, &nums); + while has[i] { + i += 1; + } + ans[idx_mut as usize] = i as i32; + idx_mut = parents[idx_mut as usize]; + } + ans + } +} diff --git a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md index a7cd966db7ebc..99fa51b0d2a1d 100644 --- a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md +++ b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md @@ -261,7 +261,7 @@ impl Solution { res += arr[(num - k) as usize]; } if num + k <= 100 { - res += arr[(num + k) as usize] + res += arr[(num + k) as usize]; } arr[num as usize] += 1; } diff --git a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md index 3128d30829ea9..250117a66977a 100644 --- a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md +++ b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md @@ -238,7 +238,7 @@ impl Solution { res += arr[(num - k) as usize]; } if num + k <= 100 { - res += arr[(num + k) as usize] + res += arr[(num + k) as usize]; } arr[num as usize] += 1; } diff --git a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.rs b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.rs index 9b89890a861d0..3bf3edbc1e5f1 100644 --- a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.rs +++ b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.rs @@ -7,7 +7,7 @@ impl Solution { res += arr[(num - k) as usize]; } if num + k <= 100 { - res += arr[(num + k) as usize] + res += arr[(num + k) as usize]; } arr[num as usize] += 1; } diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md index a7fb1702d180b..15a9add2c4227 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md @@ -225,11 +225,11 @@ impl Solution { let mut ans = n; for i in 0..m { - let j = match nums.binary_search(&(nums[i] + n as i32)) { + let j = match nums.binary_search(&(nums[i] + (n as i32))) { Ok(idx) => idx, Err(idx) => idx, }; - ans = std::cmp::min(ans, n - (j - i)) + ans = std::cmp::min(ans, n - (j - i)); } ans as i32 diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md index 30d21ece66eda..359e49b92443b 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md @@ -198,11 +198,11 @@ impl Solution { let mut ans = n; for i in 0..m { - let j = match nums.binary_search(&(nums[i] + n as i32)) { + let j = match nums.binary_search(&(nums[i] + (n as i32))) { Ok(idx) => idx, Err(idx) => idx, }; - ans = std::cmp::min(ans, n - (j - i)) + ans = std::cmp::min(ans, n - (j - i)); } ans as i32 diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs index 7e0043590f303..cf5aa096b2964 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs @@ -12,13 +12,13 @@ impl Solution { let mut ans = n; for i in 0..m { - let j = match nums.binary_search(&(nums[i] + n as i32)) { + let j = match nums.binary_search(&(nums[i] + (n as i32))) { Ok(idx) => idx, Err(idx) => idx, }; - ans = std::cmp::min(ans, n - (j - i)) + ans = std::cmp::min(ans, n - (j - i)); } ans as i32 } -} \ No newline at end of file +} diff --git a/solution/2000-2099/2028.Find Missing Observations/README.md b/solution/2000-2099/2028.Find Missing Observations/README.md index 9b5fd52a846cf..088c494eaf743 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README.md +++ b/solution/2000-2099/2028.Find Missing Observations/README.md @@ -164,7 +164,7 @@ impl Solution { let sum = sum as usize; let max = n * 6; let min = n; - if (sum + max) < mean * len || (sum + min) > mean * len { + if sum + max < mean * len || sum + min > mean * len { return vec![]; } diff --git a/solution/2000-2099/2028.Find Missing Observations/README_EN.md b/solution/2000-2099/2028.Find Missing Observations/README_EN.md index 85745c9306eb0..85a332415c0f2 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README_EN.md +++ b/solution/2000-2099/2028.Find Missing Observations/README_EN.md @@ -136,7 +136,7 @@ impl Solution { let sum = sum as usize; let max = n * 6; let min = n; - if (sum + max) < mean * len || (sum + min) > mean * len { + if sum + max < mean * len || sum + min > mean * len { return vec![]; } diff --git a/solution/2000-2099/2028.Find Missing Observations/Solution.rs b/solution/2000-2099/2028.Find Missing Observations/Solution.rs index 06bb0f508523b..f2a186aa447ea 100644 --- a/solution/2000-2099/2028.Find Missing Observations/Solution.rs +++ b/solution/2000-2099/2028.Find Missing Observations/Solution.rs @@ -7,7 +7,7 @@ impl Solution { let sum = sum as usize; let max = n * 6; let min = n; - if (sum + max) < mean * len || (sum + min) > mean * len { + if sum + max < mean * len || sum + min > mean * len { return vec![]; } diff --git a/solution/2000-2099/2032.Two Out of Three/README.md b/solution/2000-2099/2032.Two Out of Three/README.md index 16fed076fcbaf..c79925e7d4296 100644 --- a/solution/2000-2099/2032.Two Out of Three/README.md +++ b/solution/2000-2099/2032.Two Out of Three/README.md @@ -172,23 +172,32 @@ impl Solution { .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums2 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums3 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); let mut ans = Vec::new(); - count.iter().enumerate().for_each(|(i, v)| { - if *v >= 2 { - ans.push(i as i32); - } - }); + count + .iter() + .enumerate() + .for_each(|(i, v)| { + if *v >= 2 { + ans.push(i as i32); + } + }); ans } } diff --git a/solution/2000-2099/2032.Two Out of Three/README_EN.md b/solution/2000-2099/2032.Two Out of Three/README_EN.md index 4cdb2fb8293b7..e1db346fe7782 100644 --- a/solution/2000-2099/2032.Two Out of Three/README_EN.md +++ b/solution/2000-2099/2032.Two Out of Three/README_EN.md @@ -156,23 +156,32 @@ impl Solution { .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums2 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums3 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); let mut ans = Vec::new(); - count.iter().enumerate().for_each(|(i, v)| { - if *v >= 2 { - ans.push(i as i32); - } - }); + count + .iter() + .enumerate() + .for_each(|(i, v)| { + if *v >= 2 { + ans.push(i as i32); + } + }); ans } } diff --git a/solution/2000-2099/2032.Two Out of Three/Solution.rs b/solution/2000-2099/2032.Two Out of Three/Solution.rs index 4e4677e308e15..cd043ba4152f3 100644 --- a/solution/2000-2099/2032.Two Out of Three/Solution.rs +++ b/solution/2000-2099/2032.Two Out of Three/Solution.rs @@ -6,23 +6,32 @@ impl Solution { .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums2 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); nums3 .into_iter() .collect::>() .iter() - .for_each(|&v| count[v as usize] += 1); + .for_each(|&v| { + count[v as usize] += 1; + }); let mut ans = Vec::new(); - count.iter().enumerate().for_each(|(i, v)| { - if *v >= 2 { - ans.push(i as i32); - } - }); + count + .iter() + .enumerate() + .for_each(|(i, v)| { + if *v >= 2 { + ans.push(i as i32); + } + }); ans } } diff --git a/solution/2000-2099/2043.Simple Bank System/README.md b/solution/2000-2099/2043.Simple Bank System/README.md index 26a19b0add364..8ec504b2ad7d3 100644 --- a/solution/2000-2099/2043.Simple Bank System/README.md +++ b/solution/2000-2099/2043.Simple Bank System/README.md @@ -325,7 +325,7 @@ impl Bank { } fn deposit(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -334,7 +334,7 @@ impl Bank { } fn withdraw(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -344,9 +344,7 @@ impl Bank { self.balance[account - 1] -= money; true } -} - -/** +}/** * Your Bank object will be instantiated and called as such: * let obj = Bank::new(balance); * let ret_1: bool = obj.transfer(account1, account2, money); diff --git a/solution/2000-2099/2043.Simple Bank System/README_EN.md b/solution/2000-2099/2043.Simple Bank System/README_EN.md index 7e504c94562c5..cdcb8b526f6e7 100644 --- a/solution/2000-2099/2043.Simple Bank System/README_EN.md +++ b/solution/2000-2099/2043.Simple Bank System/README_EN.md @@ -304,7 +304,7 @@ impl Bank { } fn deposit(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -313,7 +313,7 @@ impl Bank { } fn withdraw(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -323,9 +323,7 @@ impl Bank { self.balance[account - 1] -= money; true } -} - -/** +}/** * Your Bank object will be instantiated and called as such: * let obj = Bank::new(balance); * let ret_1: bool = obj.transfer(account1, account2, money); diff --git a/solution/2000-2099/2043.Simple Bank System/Solution.rs b/solution/2000-2099/2043.Simple Bank System/Solution.rs index 3e433f05999a0..a32cd0507fa8b 100644 --- a/solution/2000-2099/2043.Simple Bank System/Solution.rs +++ b/solution/2000-2099/2043.Simple Bank System/Solution.rs @@ -25,7 +25,7 @@ impl Bank { } fn deposit(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -34,7 +34,7 @@ impl Bank { } fn withdraw(&mut self, account: i32, money: i64) -> bool { - let (account, n) = (account as usize, self.balance.len()); + let (account, n) = (account as usize, self.balance.len()); if n < account { return false; } @@ -44,12 +44,10 @@ impl Bank { self.balance[account - 1] -= money; true } -} - -/** +}/** * Your Bank object will be instantiated and called as such: * let obj = Bank::new(balance); * let ret_1: bool = obj.transfer(account1, account2, money); * let ret_2: bool = obj.deposit(account, money); * let ret_3: bool = obj.withdraw(account, money); - */ \ No newline at end of file + */ diff --git a/solution/2100-2199/2100.Find Good Days to Rob the Bank/README.md b/solution/2100-2199/2100.Find Good Days to Rob the Bank/README.md index 5fbded8331dfa..5963316f39031 100644 --- a/solution/2100-2199/2100.Find Good Days to Rob the Bank/README.md +++ b/solution/2100-2199/2100.Find Good Days to Rob the Bank/README.md @@ -224,17 +224,17 @@ impl Solution { Ordering::Less => -1, Ordering::Greater => 1, Ordering::Equal => 0, - } + }; } let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]); for i in 1..=n { - a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 }; - b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 }; + a[i] = a[i - 1] + (if g[i - 1] == 1 { 1 } else { 0 }); + b[i] = b[i - 1] + (if g[i - 1] == -1 { 1 } else { 0 }); } let mut res = vec![]; for i in time..n - time { if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 { - res.push((i) as i32); + res.push(i as i32); } } res diff --git a/solution/2100-2199/2100.Find Good Days to Rob the Bank/README_EN.md b/solution/2100-2199/2100.Find Good Days to Rob the Bank/README_EN.md index 8125a318c1ff5..7a8b08208983f 100644 --- a/solution/2100-2199/2100.Find Good Days to Rob the Bank/README_EN.md +++ b/solution/2100-2199/2100.Find Good Days to Rob the Bank/README_EN.md @@ -212,17 +212,17 @@ impl Solution { Ordering::Less => -1, Ordering::Greater => 1, Ordering::Equal => 0, - } + }; } let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]); for i in 1..=n { - a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 }; - b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 }; + a[i] = a[i - 1] + (if g[i - 1] == 1 { 1 } else { 0 }); + b[i] = b[i - 1] + (if g[i - 1] == -1 { 1 } else { 0 }); } let mut res = vec![]; for i in time..n - time { if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 { - res.push((i) as i32); + res.push(i as i32); } } res diff --git a/solution/2100-2199/2100.Find Good Days to Rob the Bank/Solution.rs b/solution/2100-2199/2100.Find Good Days to Rob the Bank/Solution.rs index adc9af91b0f10..0361db4ab2810 100644 --- a/solution/2100-2199/2100.Find Good Days to Rob the Bank/Solution.rs +++ b/solution/2100-2199/2100.Find Good Days to Rob the Bank/Solution.rs @@ -13,17 +13,17 @@ impl Solution { Ordering::Less => -1, Ordering::Greater => 1, Ordering::Equal => 0, - } + }; } let (mut a, mut b) = (vec![0; n + 1], vec![0; n + 1]); for i in 1..=n { - a[i] = a[i - 1] + if g[i - 1] == 1 { 1 } else { 0 }; - b[i] = b[i - 1] + if g[i - 1] == -1 { 1 } else { 0 }; + a[i] = a[i - 1] + (if g[i - 1] == 1 { 1 } else { 0 }); + b[i] = b[i - 1] + (if g[i - 1] == -1 { 1 } else { 0 }); } let mut res = vec![]; for i in time..n - time { if a[i + 1] - a[i + 1 - time] == 0 && b[i + 1 + time] - b[i + 1] == 0 { - res.push((i) as i32); + res.push(i as i32); } } res diff --git a/solution/2100-2199/2103.Rings and Rods/README.md b/solution/2100-2199/2103.Rings and Rods/README.md index 7ff5f669bdff4..f32271565383e 100644 --- a/solution/2100-2199/2103.Rings and Rods/README.md +++ b/solution/2100-2199/2103.Rings and Rods/README.md @@ -207,11 +207,14 @@ impl Solution { for i in (0..cs.len()).step_by(2) { let c = cs[i] as usize; - let j = cs[i + 1] as usize - '0' as usize; + let j = (cs[i + 1] as usize) - ('0' as usize); mask[j] |= d[c]; } - mask.iter().filter(|&&x| x == 7).count() as i32 + mask + .iter() + .filter(|&&x| x == 7) + .count() as i32 } } ``` diff --git a/solution/2100-2199/2103.Rings and Rods/README_EN.md b/solution/2100-2199/2103.Rings and Rods/README_EN.md index 556873e549c4c..beb4526729f73 100644 --- a/solution/2100-2199/2103.Rings and Rods/README_EN.md +++ b/solution/2100-2199/2103.Rings and Rods/README_EN.md @@ -197,11 +197,14 @@ impl Solution { for i in (0..cs.len()).step_by(2) { let c = cs[i] as usize; - let j = cs[i + 1] as usize - '0' as usize; + let j = (cs[i + 1] as usize) - ('0' as usize); mask[j] |= d[c]; } - mask.iter().filter(|&&x| x == 7).count() as i32 + mask + .iter() + .filter(|&&x| x == 7) + .count() as i32 } } ``` diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.rs b/solution/2100-2199/2103.Rings and Rods/Solution.rs index 6196aea8f7483..00aa8e1c74ac0 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.rs +++ b/solution/2100-2199/2103.Rings and Rods/Solution.rs @@ -1,20 +1,23 @@ -impl Solution { - pub fn count_points(rings: String) -> i32 { - let mut d: [i32; 90] = [0; 90]; - d['R' as usize] = 1; - d['G' as usize] = 2; - d['B' as usize] = 4; - - let mut mask: [i32; 10] = [0; 10]; - - let cs: Vec = rings.chars().collect(); - - for i in (0..cs.len()).step_by(2) { - let c = cs[i] as usize; - let j = cs[i + 1] as usize - '0' as usize; - mask[j] |= d[c]; - } - - mask.iter().filter(|&&x| x == 7).count() as i32 - } -} \ No newline at end of file +impl Solution { + pub fn count_points(rings: String) -> i32 { + let mut d: [i32; 90] = [0; 90]; + d['R' as usize] = 1; + d['G' as usize] = 2; + d['B' as usize] = 4; + + let mut mask: [i32; 10] = [0; 10]; + + let cs: Vec = rings.chars().collect(); + + for i in (0..cs.len()).step_by(2) { + let c = cs[i] as usize; + let j = (cs[i + 1] as usize) - ('0' as usize); + mask[j] |= d[c]; + } + + mask + .iter() + .filter(|&&x| x == 7) + .count() as i32 + } +} diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md index 374f810e52de2..d162597029d47 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md @@ -178,7 +178,7 @@ impl Solution { let mut ans = cnt.len() as i32; for i in k as usize..n { - *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; + *cnt.entry(candies[i - (k as usize)]).or_insert(0) += 1; if let Some(x) = cnt.get_mut(&candies[i]) { *x -= 1; if *x == 0 { diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md index e73ab838f78bb..3a678668ff9d1 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md @@ -169,7 +169,7 @@ impl Solution { let mut ans = cnt.len() as i32; for i in k as usize..n { - *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; + *cnt.entry(candies[i - (k as usize)]).or_insert(0) += 1; if let Some(x) = cnt.get_mut(&candies[i]) { *x -= 1; if *x == 0 { diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs index cd52d94851c26..3ebab0ed857b0 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs @@ -1,28 +1,28 @@ -use std::collections::HashMap; - -impl Solution { - pub fn share_candies(candies: Vec, k: i32) -> i32 { - let mut cnt = HashMap::new(); - let n = candies.len(); - - for i in k as usize..n { - *cnt.entry(candies[i]).or_insert(0) += 1; - } - - let mut ans = cnt.len() as i32; - - for i in k as usize..n { - *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; - if let Some(x) = cnt.get_mut(&candies[i]) { - *x -= 1; - if *x == 0 { - cnt.remove(&candies[i]); - } - } - - ans = ans.max(cnt.len() as i32); - } - - ans - } -} +use std::collections::HashMap; + +impl Solution { + pub fn share_candies(candies: Vec, k: i32) -> i32 { + let mut cnt = HashMap::new(); + let n = candies.len(); + + for i in k as usize..n { + *cnt.entry(candies[i]).or_insert(0) += 1; + } + + let mut ans = cnt.len() as i32; + + for i in k as usize..n { + *cnt.entry(candies[i - (k as usize)]).or_insert(0) += 1; + if let Some(x) = cnt.get_mut(&candies[i]) { + *x -= 1; + if *x == 0 { + cnt.remove(&candies[i]); + } + } + + ans = ans.max(cnt.len() as i32); + } + + ans + } +} diff --git a/solution/2100-2199/2108.Find First Palindromic String in the Array/README.md b/solution/2100-2199/2108.Find First Palindromic String in the Array/README.md index f59d9afbf2c04..70b613ac9cdd0 100644 --- a/solution/2100-2199/2108.Find First Palindromic String in the Array/README.md +++ b/solution/2100-2199/2108.Find First Palindromic String in the Array/README.md @@ -163,8 +163,8 @@ impl Solution { let s = word.as_bytes(); let mut left = 0; let mut right = s.len() - 1; - while (left < right) { - if (s[left] != s[right]) { + while left < right { + if s[left] != s[right] { break; } left += 1; diff --git a/solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md b/solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md index 787ba0e464205..e468915f37ec9 100644 --- a/solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md +++ b/solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md @@ -148,8 +148,8 @@ impl Solution { let s = word.as_bytes(); let mut left = 0; let mut right = s.len() - 1; - while (left < right) { - if (s[left] != s[right]) { + while left < right { + if s[left] != s[right] { break; } left += 1; diff --git a/solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.rs b/solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.rs index f3e6c061f0d08..cd52824cbd7c1 100644 --- a/solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.rs +++ b/solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.rs @@ -4,8 +4,8 @@ impl Solution { let s = word.as_bytes(); let mut left = 0; let mut right = s.len() - 1; - while (left < right) { - if (s[left] != s[right]) { + while left < right { + if s[left] != s[right] { break; } left += 1; diff --git a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md index 35f582deada6c..308d3ec3b9eb2 100644 --- a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md +++ b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md @@ -247,10 +247,18 @@ impl Solution { let mut j = i; while j < m { match s[j] { - b'U' => y -= 1, - b'D' => y += 1, - b'L' => x -= 1, - _ => x += 1, + b'U' => { + y -= 1; + } + b'D' => { + y += 1; + } + b'L' => { + x -= 1; + } + _ => { + x += 1; + } } if y == -1 || y == n || x == -1 || x == n { break; diff --git a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md index 1fae54c76c265..e815774e57509 100644 --- a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md +++ b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md @@ -227,10 +227,18 @@ impl Solution { let mut j = i; while j < m { match s[j] { - b'U' => y -= 1, - b'D' => y += 1, - b'L' => x -= 1, - _ => x += 1, + b'U' => { + y -= 1; + } + b'D' => { + y += 1; + } + b'L' => { + x -= 1; + } + _ => { + x += 1; + } } if y == -1 || y == n || x == -1 || x == n { break; diff --git a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.rs b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.rs index 10d0de72a4a86..7058c4fbe640c 100644 --- a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.rs +++ b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.rs @@ -9,10 +9,18 @@ impl Solution { let mut j = i; while j < m { match s[j] { - b'U' => y -= 1, - b'D' => y += 1, - b'L' => x -= 1, - _ => x += 1, + b'U' => { + y -= 1; + } + b'D' => { + y += 1; + } + b'L' => { + x -= 1; + } + _ => { + x += 1; + } } if y == -1 || y == n || x == -1 || x == n { break; diff --git a/solution/2100-2199/2132.Stamping the Grid/README.md b/solution/2100-2199/2132.Stamping the Grid/README.md index 4638ca9ea5439..4be4817d47cc5 100644 --- a/solution/2100-2199/2132.Stamping the Grid/README.md +++ b/solution/2100-2199/2132.Stamping the Grid/README.md @@ -206,7 +206,8 @@ impl Solution { // Initialize the prefix vector for i in 0..n { for j in 0..m { - prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; + prefix_vec[i + 1][j + 1] = + prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; } } @@ -220,12 +221,15 @@ impl Solution { continue; } // Otherwise, try stick the stamp - let x: usize = i + stamp_height as usize; - let y: usize = j + stamp_width as usize; + let x: usize = i + (stamp_height as usize); + let y: usize = j + (stamp_width as usize); // Check the bound if x <= n && y <= m { // If the region can be sticked (All cells are empty, which means the sum will be zero) - if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 { + if + prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == + 0 + { // Update the difference vector diff_vec[i][j] += 1; diff_vec[x][y] += 1; @@ -247,7 +251,8 @@ impl Solution { continue; } // Otherwise, check if the region is empty, by calculating the prefix sum of difference vector - check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; + check_vec[i + 1][j + 1] = + check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; if check_vec[i + 1][j + 1] == 0 { return false; } diff --git a/solution/2100-2199/2132.Stamping the Grid/README_EN.md b/solution/2100-2199/2132.Stamping the Grid/README_EN.md index 901b5f606dcb3..6994fff8c2c5b 100644 --- a/solution/2100-2199/2132.Stamping the Grid/README_EN.md +++ b/solution/2100-2199/2132.Stamping the Grid/README_EN.md @@ -194,7 +194,8 @@ impl Solution { // Initialize the prefix vector for i in 0..n { for j in 0..m { - prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; + prefix_vec[i + 1][j + 1] = + prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; } } @@ -208,12 +209,15 @@ impl Solution { continue; } // Otherwise, try stick the stamp - let x: usize = i + stamp_height as usize; - let y: usize = j + stamp_width as usize; + let x: usize = i + (stamp_height as usize); + let y: usize = j + (stamp_width as usize); // Check the bound if x <= n && y <= m { // If the region can be sticked (All cells are empty, which means the sum will be zero) - if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 { + if + prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == + 0 + { // Update the difference vector diff_vec[i][j] += 1; diff_vec[x][y] += 1; @@ -235,7 +239,8 @@ impl Solution { continue; } // Otherwise, check if the region is empty, by calculating the prefix sum of difference vector - check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; + check_vec[i + 1][j + 1] = + check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; if check_vec[i + 1][j + 1] == 0 { return false; } diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.rs b/solution/2100-2199/2132.Stamping the Grid/Solution.rs index 5677a3207ae15..d9c365f2670e2 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.rs +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.rs @@ -8,9 +8,10 @@ impl Solution { // Initialize the prefix vector for i in 0..n { for j in 0..m { - prefix_vec[i + 1][j + 1] = prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; + prefix_vec[i + 1][j + 1] = + prefix_vec[i][j + 1] + prefix_vec[i + 1][j] - prefix_vec[i][j] + grid[i][j]; } - } + } let mut diff_vec: Vec> = vec![vec![0; m + 1]; n + 1]; @@ -22,12 +23,15 @@ impl Solution { continue; } // Otherwise, try stick the stamp - let x: usize = i + stamp_height as usize; - let y: usize = j + stamp_width as usize; + let x: usize = i + (stamp_height as usize); + let y: usize = j + (stamp_width as usize); // Check the bound if x <= n && y <= m { // If the region can be sticked (All cells are empty, which means the sum will be zero) - if prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == 0 { + if + prefix_vec[x][y] - prefix_vec[x][j] - prefix_vec[i][y] + prefix_vec[i][j] == + 0 + { // Update the difference vector diff_vec[i][j] += 1; diff_vec[x][y] += 1; @@ -49,7 +53,8 @@ impl Solution { continue; } // Otherwise, check if the region is empty, by calculating the prefix sum of difference vector - check_vec[i + 1][j + 1] = check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; + check_vec[i + 1][j + 1] = + check_vec[i][j + 1] + check_vec[i + 1][j] - check_vec[i][j] + diff_vec[i][j]; if check_vec[i + 1][j + 1] == 0 { return false; } @@ -58,4 +63,4 @@ impl Solution { true } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs index 71f57a3caf484..119069e8e598a 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn earliest_full_bloom(plant_time: Vec, grow_time: Vec) -> i32 { - let mut idx: Vec = (0..plant_time.len()).collect(); - idx.sort_by_key(|&i| -&grow_time[i]); - let mut ans = 0; - let mut t = 0; - for &i in &idx { - t += plant_time[i]; - ans = ans.max(t + grow_time[i]); - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn earliest_full_bloom(plant_time: Vec, grow_time: Vec) -> i32 { + let mut idx: Vec = (0..plant_time.len()).collect(); + idx.sort_by_key(|&i| -&grow_time[i]); + let mut ans = 0; + let mut t = 0; + for &i in &idx { + t += plant_time[i]; + ans = ans.max(t + grow_time[i]); + } + ans + } +} diff --git a/solution/2100-2199/2141.Maximum Running Time of N Computers/README.md b/solution/2100-2199/2141.Maximum Running Time of N Computers/README.md index 6cf2009d1b186..6f4394a56112f 100644 --- a/solution/2100-2199/2141.Maximum Running Time of N Computers/README.md +++ b/solution/2100-2199/2141.Maximum Running Time of N Computers/README.md @@ -195,7 +195,6 @@ function maxRunTime(n: number, batteries: number[]): number { impl Solution { #[allow(dead_code)] pub fn max_run_time(n: i32, batteries: Vec) -> i64 { - // First sort the batteries let mut batteries = batteries; let m = batteries.len() as i32; @@ -210,9 +209,9 @@ impl Solution { let mut cur_height = batteries[i]; let mut ret = cur_height as i64; while extra_sum != 0 { - if i + 1 == m as usize { + if i + 1 == (m as usize) { assert!(cur_height == *batteries.last().unwrap()); - ret += extra_sum / n as i64; + ret += extra_sum / (n as i64); break; } @@ -221,18 +220,20 @@ impl Solution { continue; } - let diff = extra_sum / (i - (m - n) as usize + 1) as i64; + let diff = extra_sum / ((i - ((m - n) as usize) + 1) as i64); - if (cur_height as i64 + diff) <= batteries[i + 1] as i64 { - ret = cur_height as i64 + diff; + if (cur_height as i64) + diff <= (batteries[i + 1] as i64) { + ret = (cur_height as i64) + diff; break; } else { - extra_sum -= (batteries[i + 1] - batteries[i]) as i64 * (i - (m - n) as usize + 1) as i64; + extra_sum -= + ((batteries[i + 1] - batteries[i]) as i64) * + ((i - ((m - n) as usize) + 1) as i64); ret = batteries[i + 1] as i64; } i += 1; - if i != m as usize { + if i != (m as usize) { cur_height = batteries[i]; } } diff --git a/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md b/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md index f164f8480ab5f..f8d3e9be40943 100644 --- a/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md +++ b/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md @@ -174,7 +174,6 @@ function maxRunTime(n: number, batteries: number[]): number { impl Solution { #[allow(dead_code)] pub fn max_run_time(n: i32, batteries: Vec) -> i64 { - // First sort the batteries let mut batteries = batteries; let m = batteries.len() as i32; @@ -189,9 +188,9 @@ impl Solution { let mut cur_height = batteries[i]; let mut ret = cur_height as i64; while extra_sum != 0 { - if i + 1 == m as usize { + if i + 1 == (m as usize) { assert!(cur_height == *batteries.last().unwrap()); - ret += extra_sum / n as i64; + ret += extra_sum / (n as i64); break; } @@ -200,18 +199,20 @@ impl Solution { continue; } - let diff = extra_sum / (i - (m - n) as usize + 1) as i64; + let diff = extra_sum / ((i - ((m - n) as usize) + 1) as i64); - if (cur_height as i64 + diff) <= batteries[i + 1] as i64 { - ret = cur_height as i64 + diff; + if (cur_height as i64) + diff <= (batteries[i + 1] as i64) { + ret = (cur_height as i64) + diff; break; } else { - extra_sum -= (batteries[i + 1] - batteries[i]) as i64 * (i - (m - n) as usize + 1) as i64; + extra_sum -= + ((batteries[i + 1] - batteries[i]) as i64) * + ((i - ((m - n) as usize) + 1) as i64); ret = batteries[i + 1] as i64; } i += 1; - if i != m as usize { + if i != (m as usize) { cur_height = batteries[i]; } } diff --git a/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.rs b/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.rs index 0155d1c1c83f4..e4a5c48986648 100644 --- a/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.rs +++ b/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.rs @@ -1,8 +1,7 @@ impl Solution { #[allow(dead_code)] pub fn max_run_time(n: i32, batteries: Vec) -> i64 { - - // First sort the batteries + // First sort the batteries let mut batteries = batteries; let m = batteries.len() as i32; batteries.sort(); @@ -16,9 +15,9 @@ impl Solution { let mut cur_height = batteries[i]; let mut ret = cur_height as i64; while extra_sum != 0 { - if i + 1 == m as usize { + if i + 1 == (m as usize) { assert!(cur_height == *batteries.last().unwrap()); - ret += extra_sum / n as i64; + ret += extra_sum / (n as i64); break; } @@ -27,22 +26,24 @@ impl Solution { continue; } - let diff = extra_sum / (i - (m - n) as usize + 1) as i64; + let diff = extra_sum / ((i - ((m - n) as usize) + 1) as i64); - if (cur_height as i64 + diff) <= batteries[i + 1] as i64 { - ret = cur_height as i64 + diff; + if (cur_height as i64) + diff <= (batteries[i + 1] as i64) { + ret = (cur_height as i64) + diff; break; } else { - extra_sum -= (batteries[i + 1] - batteries[i]) as i64 * (i - (m - n) as usize + 1) as i64; + extra_sum -= + ((batteries[i + 1] - batteries[i]) as i64) * + ((i - ((m - n) as usize) + 1) as i64); ret = batteries[i + 1] as i64; } i += 1; - if i != m as usize { + if i != (m as usize) { cur_height = batteries[i]; } } ret } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md index 403448a8ba97b..cd7366ecdd5a5 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md @@ -144,7 +144,7 @@ impl Solution { let mut ans = 0; for i in 0..n - 1 { for j in i + 1..n { - if nums[i] == nums[j] && i * j % k == 0 { + if nums[i] == nums[j] && (i * j) % k == 0 { ans += 1; } } diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md index b75cc201d01dd..59dfef6d8f8d3 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md @@ -134,7 +134,7 @@ impl Solution { let mut ans = 0; for i in 0..n - 1 { for j in i + 1..n { - if nums[i] == nums[j] && i * j % k == 0 { + if nums[i] == nums[j] && (i * j) % k == 0 { ans += 1; } } diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs index be3870dc3c0b3..6f500b86e8e98 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs @@ -5,7 +5,7 @@ impl Solution { let mut ans = 0; for i in 0..n - 1 { for j in i + 1..n { - if nums[i] == nums[j] && i * j % k == 0 { + if nums[i] == nums[j] && (i * j) % k == 0 { ans += 1; } } diff --git a/solution/2100-2199/2185.Counting Words With a Given Prefix/README.md b/solution/2100-2199/2185.Counting Words With a Given Prefix/README.md index dda99e15562be..6cf87dcf6ef42 100644 --- a/solution/2100-2199/2185.Counting Words With a Given Prefix/README.md +++ b/solution/2100-2199/2185.Counting Words With a Given Prefix/README.md @@ -302,7 +302,10 @@ function prefixCount(words: string[], pref: string): number { ```rust impl Solution { pub fn prefix_count(words: Vec, pref: String) -> i32 { - words.iter().filter(|s| s.starts_with(&pref)).count() as i32 + words + .iter() + .filter(|s| s.starts_with(&pref)) + .count() as i32 } } ``` diff --git a/solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md b/solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md index 2f0ee0b2ae7b8..e07b13ff199d8 100644 --- a/solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md +++ b/solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md @@ -268,7 +268,10 @@ function prefixCount(words: string[], pref: string): number { ```rust impl Solution { pub fn prefix_count(words: Vec, pref: String) -> i32 { - words.iter().filter(|s| s.starts_with(&pref)).count() as i32 + words + .iter() + .filter(|s| s.starts_with(&pref)) + .count() as i32 } } ``` diff --git a/solution/2100-2199/2185.Counting Words With a Given Prefix/Solution.rs b/solution/2100-2199/2185.Counting Words With a Given Prefix/Solution.rs index 266dcbcd8fd49..73e470df904db 100644 --- a/solution/2100-2199/2185.Counting Words With a Given Prefix/Solution.rs +++ b/solution/2100-2199/2185.Counting Words With a Given Prefix/Solution.rs @@ -1,5 +1,8 @@ impl Solution { pub fn prefix_count(words: Vec, pref: String) -> i32 { - words.iter().filter(|s| s.starts_with(&pref)).count() as i32 + words + .iter() + .filter(|s| s.starts_with(&pref)) + .count() as i32 } } diff --git a/solution/2200-2299/2210.Count Hills and Valleys in an Array/README.md b/solution/2200-2299/2210.Count Hills and Valleys in an Array/README.md index d27a0597cc22c..7e92a218c8f5b 100644 --- a/solution/2200-2299/2210.Count Hills and Valleys in an Array/README.md +++ b/solution/2200-2299/2210.Count Hills and Valleys in an Array/README.md @@ -212,7 +212,7 @@ impl Solution { if num == next { continue; } - if num > prev && num > next || num < prev && num < next { + if (num > prev && num > next) || (num < prev && num < next) { res += 1; } prev = num; diff --git a/solution/2200-2299/2210.Count Hills and Valleys in an Array/README_EN.md b/solution/2200-2299/2210.Count Hills and Valleys in an Array/README_EN.md index 8eedb02ae551f..fc4f526b283b4 100644 --- a/solution/2200-2299/2210.Count Hills and Valleys in an Array/README_EN.md +++ b/solution/2200-2299/2210.Count Hills and Valleys in an Array/README_EN.md @@ -185,7 +185,7 @@ impl Solution { if num == next { continue; } - if num > prev && num > next || num < prev && num < next { + if (num > prev && num > next) || (num < prev && num < next) { res += 1; } prev = num; diff --git a/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.rs b/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.rs index 08872e87f2cdb..c038286bc4c29 100644 --- a/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.rs +++ b/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.rs @@ -9,7 +9,7 @@ impl Solution { if num == next { continue; } - if num > prev && num > next || num < prev && num < next { + if (num > prev && num > next) || (num < prev && num < next) { res += 1; } prev = num; diff --git a/solution/2200-2299/2212.Maximum Points in an Archery Competition/README.md b/solution/2200-2299/2212.Maximum Points in an Archery Competition/README.md index a4befd0ff3295..2351858679ca6 100644 --- a/solution/2200-2299/2212.Maximum Points in an Archery Competition/README.md +++ b/solution/2200-2299/2212.Maximum Points in an Archery Competition/README.md @@ -246,12 +246,14 @@ impl Solution { if count > alice_arrows[i] { res[i] = alice_arrows[i] + 1; let r2 = Self::dfs(alice_arrows, res, count - alice_arrows[i] - 1, i - 1); - if r2 - .iter() - .enumerate() - .map(|(i, v)| if v > &0 { i } else { 0 }) - .sum::() - > r1.iter() + if + r2 + .iter() + .enumerate() + .map(|(i, v)| if v > &0 { i } else { 0 }) + .sum::() > + r1 + .iter() .enumerate() .map(|(i, v)| if v > &0 { i } else { 0 }) .sum::() diff --git a/solution/2200-2299/2212.Maximum Points in an Archery Competition/README_EN.md b/solution/2200-2299/2212.Maximum Points in an Archery Competition/README_EN.md index 64f4120675642..6e75ffc0de934 100644 --- a/solution/2200-2299/2212.Maximum Points in an Archery Competition/README_EN.md +++ b/solution/2200-2299/2212.Maximum Points in an Archery Competition/README_EN.md @@ -228,12 +228,14 @@ impl Solution { if count > alice_arrows[i] { res[i] = alice_arrows[i] + 1; let r2 = Self::dfs(alice_arrows, res, count - alice_arrows[i] - 1, i - 1); - if r2 - .iter() - .enumerate() - .map(|(i, v)| if v > &0 { i } else { 0 }) - .sum::() - > r1.iter() + if + r2 + .iter() + .enumerate() + .map(|(i, v)| if v > &0 { i } else { 0 }) + .sum::() > + r1 + .iter() .enumerate() .map(|(i, v)| if v > &0 { i } else { 0 }) .sum::() diff --git a/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.rs b/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.rs index 873b627c72634..88bb1160b4a2b 100644 --- a/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.rs +++ b/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.rs @@ -8,12 +8,14 @@ impl Solution { if count > alice_arrows[i] { res[i] = alice_arrows[i] + 1; let r2 = Self::dfs(alice_arrows, res, count - alice_arrows[i] - 1, i - 1); - if r2 - .iter() - .enumerate() - .map(|(i, v)| if v > &0 { i } else { 0 }) - .sum::() - > r1.iter() + if + r2 + .iter() + .enumerate() + .map(|(i, v)| if v > &0 { i } else { 0 }) + .sum::() > + r1 + .iter() .enumerate() .map(|(i, v)| if v > &0 { i } else { 0 }) .sum::() diff --git a/solution/2200-2299/2215.Find the Difference of Two Arrays/README.md b/solution/2200-2299/2215.Find the Difference of Two Arrays/README.md index 049017a8be04d..a72d65d137f89 100644 --- a/solution/2200-2299/2215.Find the Difference of Two Arrays/README.md +++ b/solution/2200-2299/2215.Find the Difference of Two Arrays/README.md @@ -196,7 +196,7 @@ impl Solution { .filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) }) .collect::>() .into_iter() - .collect(), + .collect() ] } } @@ -206,7 +206,7 @@ impl Solution { impl Solution { pub fn find_difference(nums1: Vec, nums2: Vec) -> Vec> { const N: usize = 2001; - let to_index = |i| i as usize + 1000; + let to_index = |i| (i as usize) + 1000; let mut is_in_nums1 = [false; N]; let mut is_in_nums2 = [false; N]; diff --git a/solution/2200-2299/2215.Find the Difference of Two Arrays/README_EN.md b/solution/2200-2299/2215.Find the Difference of Two Arrays/README_EN.md index fd4464c855b29..4a936d05a7b02 100644 --- a/solution/2200-2299/2215.Find the Difference of Two Arrays/README_EN.md +++ b/solution/2200-2299/2215.Find the Difference of Two Arrays/README_EN.md @@ -186,7 +186,7 @@ impl Solution { .filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) }) .collect::>() .into_iter() - .collect(), + .collect() ] } } @@ -196,7 +196,7 @@ impl Solution { impl Solution { pub fn find_difference(nums1: Vec, nums2: Vec) -> Vec> { const N: usize = 2001; - let to_index = |i| i as usize + 1000; + let to_index = |i| (i as usize) + 1000; let mut is_in_nums1 = [false; N]; let mut is_in_nums2 = [false; N]; diff --git a/solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.rs b/solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.rs index 266b739780104..069fa832bc00c 100644 --- a/solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.rs +++ b/solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.rs @@ -13,7 +13,7 @@ impl Solution { .filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) }) .collect::>() .into_iter() - .collect(), + .collect() ] } } diff --git a/solution/2200-2299/2217.Find Palindrome With Fixed Length/README.md b/solution/2200-2299/2217.Find Palindrome With Fixed Length/README.md index 1ad70a90c9e7c..b24cc76171660 100644 --- a/solution/2200-2299/2217.Find Palindrome With Fixed Length/README.md +++ b/solution/2200-2299/2217.Find Palindrome With Fixed Length/README.md @@ -179,8 +179,8 @@ function kthPalindrome(queries: number[], intLength: number): number[] { ```rust impl Solution { pub fn kth_palindrome(queries: Vec, int_length: i32) -> Vec { - let is_odd = int_length & 1 == 1; - let best_num = i32::pow(10, (int_length / 2 + if is_odd { 0 } else { -1 }) as u32); + let is_odd = (int_length & 1) == 1; + let best_num = i32::pow(10, (int_length / 2 + (if is_odd { 0 } else { -1 })) as u32); let max = best_num * 9; queries .iter() @@ -192,14 +192,15 @@ impl Solution { format!( "{}{}", num, - num.to_string() + num + .to_string() .chars() .rev() .skip(if is_odd { 1 } else { 0 }) .collect::() ) - .parse() - .unwrap() + .parse() + .unwrap() }) .collect() } diff --git a/solution/2200-2299/2217.Find Palindrome With Fixed Length/README_EN.md b/solution/2200-2299/2217.Find Palindrome With Fixed Length/README_EN.md index d86957aa7fa10..4aba69b6642cb 100644 --- a/solution/2200-2299/2217.Find Palindrome With Fixed Length/README_EN.md +++ b/solution/2200-2299/2217.Find Palindrome With Fixed Length/README_EN.md @@ -169,8 +169,8 @@ function kthPalindrome(queries: number[], intLength: number): number[] { ```rust impl Solution { pub fn kth_palindrome(queries: Vec, int_length: i32) -> Vec { - let is_odd = int_length & 1 == 1; - let best_num = i32::pow(10, (int_length / 2 + if is_odd { 0 } else { -1 }) as u32); + let is_odd = (int_length & 1) == 1; + let best_num = i32::pow(10, (int_length / 2 + (if is_odd { 0 } else { -1 })) as u32); let max = best_num * 9; queries .iter() @@ -182,14 +182,15 @@ impl Solution { format!( "{}{}", num, - num.to_string() + num + .to_string() .chars() .rev() .skip(if is_odd { 1 } else { 0 }) .collect::() ) - .parse() - .unwrap() + .parse() + .unwrap() }) .collect() } diff --git a/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.rs b/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.rs index ab236c3691ff3..a0ddbf3ca2a3f 100644 --- a/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.rs +++ b/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.rs @@ -1,7 +1,7 @@ impl Solution { pub fn kth_palindrome(queries: Vec, int_length: i32) -> Vec { - let is_odd = int_length & 1 == 1; - let best_num = i32::pow(10, (int_length / 2 + if is_odd { 0 } else { -1 }) as u32); + let is_odd = (int_length & 1) == 1; + let best_num = i32::pow(10, (int_length / 2 + (if is_odd { 0 } else { -1 })) as u32); let max = best_num * 9; queries .iter() @@ -13,14 +13,15 @@ impl Solution { format!( "{}{}", num, - num.to_string() + num + .to_string() .chars() .rev() .skip(if is_odd { 1 } else { 0 }) .collect::() ) - .parse() - .unwrap() + .parse() + .unwrap() }) .collect() } diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md index 168799515e2ab..a8774d8bfb62d 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md @@ -129,7 +129,7 @@ impl Solution { pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { let mut ans: i64 = 0; for pen in 0..=total / cost1 { - ans += ((total - pen * cost1) / cost2) as i64 + 1; + ans += (((total - pen * cost1) / cost2) as i64) + 1; } ans } diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md index 91a103ee0a5a9..5a1d9a0e53f53 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md @@ -115,7 +115,7 @@ impl Solution { pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { let mut ans: i64 = 0; for pen in 0..=total / cost1 { - ans += ((total - pen * cost1) / cost2) as i64 + 1; + ans += (((total - pen * cost1) / cost2) as i64) + 1; } ans } diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs index 42eec8d80da9e..1c9ebbc89a09a 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs @@ -1,9 +1,9 @@ -impl Solution { - pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { - let mut ans: i64 = 0; - for pen in 0..=total / cost1 { - ans += ((total - pen * cost1) / cost2) as i64 + 1; - } - ans - } -} \ No newline at end of file +impl Solution { + pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { + let mut ans: i64 = 0; + for pen in 0..=total / cost1 { + ans += (((total - pen * cost1) / cost2) as i64) + 1; + } + ans + } +} diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md index f38fc42cc3551..faaf1d4c15d33 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md @@ -238,9 +238,7 @@ impl Solution { .map(|x| x) .collect(); - people.sort_by(|lhs, rhs| { - lhs.1.cmp(&rhs.1) - }); + people.sort_by(|lhs, rhs| { lhs.1.cmp(&rhs.1) }); // Initialize the difference vector let mut diff = BTreeMap::new(); @@ -248,14 +246,16 @@ impl Solution { for f in flowers { let (left, right) = (f[0], f[1]); - diff - .entry(left) - .and_modify(|x| *x += 1) + diff.entry(left) + .and_modify(|x| { + *x += 1; + }) .or_insert(1); - diff - .entry(right + 1) - .and_modify(|x| *x -= 1) + diff.entry(right + 1) + .and_modify(|x| { + *x -= 1; + }) .or_insert(-1); } diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md index 3970d31b05a90..cafa056ca2bb5 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md @@ -224,9 +224,7 @@ impl Solution { .map(|x| x) .collect(); - people.sort_by(|lhs, rhs| { - lhs.1.cmp(&rhs.1) - }); + people.sort_by(|lhs, rhs| { lhs.1.cmp(&rhs.1) }); // Initialize the difference vector let mut diff = BTreeMap::new(); @@ -234,14 +232,16 @@ impl Solution { for f in flowers { let (left, right) = (f[0], f[1]); - diff - .entry(left) - .and_modify(|x| *x += 1) + diff.entry(left) + .and_modify(|x| { + *x += 1; + }) .or_insert(1); - diff - .entry(right + 1) - .and_modify(|x| *x -= 1) + diff.entry(right + 1) + .and_modify(|x| { + *x -= 1; + }) .or_insert(-1); } diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/Solution.rs b/solution/2200-2299/2251.Number of Flowers in Full Bloom/Solution.rs index fbf7709968760..e57c70df980be 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/Solution.rs +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/Solution.rs @@ -12,9 +12,7 @@ impl Solution { .map(|x| x) .collect(); - people.sort_by(|lhs, rhs| { - lhs.1.cmp(&rhs.1) - }); + people.sort_by(|lhs, rhs| { lhs.1.cmp(&rhs.1) }); // Initialize the difference vector let mut diff = BTreeMap::new(); @@ -22,14 +20,16 @@ impl Solution { for f in flowers { let (left, right) = (f[0], f[1]); - diff - .entry(left) - .and_modify(|x| *x += 1) + diff.entry(left) + .and_modify(|x| { + *x += 1; + }) .or_insert(1); - diff - .entry(right + 1) - .and_modify(|x| *x -= 1) + diff.entry(right + 1) + .and_modify(|x| { + *x -= 1; + }) .or_insert(-1); } @@ -45,4 +45,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2278.Percentage of Letter in String/README.md b/solution/2200-2299/2278.Percentage of Letter in String/README.md index 86a1f72967b4f..5142fc681213a 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/README.md +++ b/solution/2200-2299/2278.Percentage of Letter in String/README.md @@ -122,7 +122,7 @@ impl Solution { count += 1; } } - (count * 100 / s.len()) as i32 + ((count * 100) / s.len()) as i32 } } ``` diff --git a/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md b/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md index 9861245b35430..cb5e012b0b4cb 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md +++ b/solution/2200-2299/2278.Percentage of Letter in String/README_EN.md @@ -112,7 +112,7 @@ impl Solution { count += 1; } } - (count * 100 / s.len()) as i32 + ((count * 100) / s.len()) as i32 } } ``` diff --git a/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs b/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs index d3ca2aa406ff0..5d3cee347cba6 100644 --- a/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs +++ b/solution/2200-2299/2278.Percentage of Letter in String/Solution.rs @@ -6,6 +6,6 @@ impl Solution { count += 1; } } - (count * 100 / s.len()) as i32 + ((count * 100) / s.len()) as i32 } } diff --git a/solution/2200-2299/2293.Min Max Game/README.md b/solution/2200-2299/2293.Min Max Game/README.md index 0c89db1a70e12..b883c2b1c7d37 100644 --- a/solution/2200-2299/2293.Min Max Game/README.md +++ b/solution/2200-2299/2293.Min Max Game/README.md @@ -163,11 +163,10 @@ impl Solution { while n != 1 { n >>= 1; for i in 0..n { - nums[i] = (if i & 1 == 1 { - i32::max - } else { - i32::min - })(nums[i << 1], nums[i << 1 | 1]) + nums[i] = (if (i & 1) == 1 { i32::max } else { i32::min })( + nums[i << 1], + nums[(i << 1) | 1] + ); } } nums[0] diff --git a/solution/2200-2299/2293.Min Max Game/README_EN.md b/solution/2200-2299/2293.Min Max Game/README_EN.md index 59d847aa86b4f..15436ea367e30 100644 --- a/solution/2200-2299/2293.Min Max Game/README_EN.md +++ b/solution/2200-2299/2293.Min Max Game/README_EN.md @@ -145,11 +145,10 @@ impl Solution { while n != 1 { n >>= 1; for i in 0..n { - nums[i] = (if i & 1 == 1 { - i32::max - } else { - i32::min - })(nums[i << 1], nums[i << 1 | 1]) + nums[i] = (if (i & 1) == 1 { i32::max } else { i32::min })( + nums[i << 1], + nums[(i << 1) | 1] + ); } } nums[0] diff --git a/solution/2200-2299/2293.Min Max Game/Solution.rs b/solution/2200-2299/2293.Min Max Game/Solution.rs index ae7d1801c2f40..208564da9af0d 100644 --- a/solution/2200-2299/2293.Min Max Game/Solution.rs +++ b/solution/2200-2299/2293.Min Max Game/Solution.rs @@ -4,11 +4,10 @@ impl Solution { while n != 1 { n >>= 1; for i in 0..n { - nums[i] = (if i & 1 == 1 { - i32::max - } else { - i32::min - })(nums[i << 1], nums[i << 1 | 1]) + nums[i] = (if (i & 1) == 1 { i32::max } else { i32::min })( + nums[i << 1], + nums[(i << 1) | 1] + ); } } nums[0] diff --git a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README.md b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README.md index 61531f2b23012..bb9e6922920d8 100644 --- a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README.md +++ b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README.md @@ -243,7 +243,7 @@ impl Solution { } for i in (0..26).rev() { if arr[i] == 3 { - return char::from(b'A' + i as u8).to_string(); + return char::from(b'A' + (i as u8)).to_string(); } } "".to_string() diff --git a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README_EN.md b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README_EN.md index 1c32f32c9add6..341206113e8b1 100644 --- a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README_EN.md +++ b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README_EN.md @@ -214,7 +214,7 @@ impl Solution { } for i in (0..26).rev() { if arr[i] == 3 { - return char::from(b'A' + i as u8).to_string(); + return char::from(b'A' + (i as u8)).to_string(); } } "".to_string() diff --git a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/Solution.rs b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/Solution.rs index 151965e45e77c..a45c109dd0197 100644 --- a/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/Solution.rs +++ b/solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/Solution.rs @@ -10,7 +10,7 @@ impl Solution { } for i in (0..26).rev() { if arr[i] == 3 { - return char::from(b'A' + i as u8).to_string(); + return char::from(b'A' + (i as u8)).to_string(); } } "".to_string() diff --git a/solution/2300-2399/2315.Count Asterisks/README.md b/solution/2300-2399/2315.Count Asterisks/README.md index 2d4d5c587cf00..620ef871abad4 100644 --- a/solution/2300-2399/2315.Count Asterisks/README.md +++ b/solution/2300-2399/2315.Count Asterisks/README.md @@ -160,9 +160,9 @@ impl Solution { let mut ok = 1; for &c in s.as_bytes() { if c == b'*' { - ans += ok + ans += ok; } else if c == b'|' { - ok ^= 1 + ok ^= 1; } } ans diff --git a/solution/2300-2399/2315.Count Asterisks/README_EN.md b/solution/2300-2399/2315.Count Asterisks/README_EN.md index 88928dcd339d2..45ad3f8b7150a 100644 --- a/solution/2300-2399/2315.Count Asterisks/README_EN.md +++ b/solution/2300-2399/2315.Count Asterisks/README_EN.md @@ -143,9 +143,9 @@ impl Solution { let mut ok = 1; for &c in s.as_bytes() { if c == b'*' { - ans += ok + ans += ok; } else if c == b'|' { - ok ^= 1 + ok ^= 1; } } ans diff --git a/solution/2300-2399/2315.Count Asterisks/Solution.rs b/solution/2300-2399/2315.Count Asterisks/Solution.rs index 14199db436094..240c078b2565d 100644 --- a/solution/2300-2399/2315.Count Asterisks/Solution.rs +++ b/solution/2300-2399/2315.Count Asterisks/Solution.rs @@ -4,11 +4,11 @@ impl Solution { let mut ok = 1; for &c in s.as_bytes() { if c == b'*' { - ans += ok + ans += ok; } else if c == b'|' { - ok ^= 1 + ok ^= 1; } } ans } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.rs b/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.rs index 34a2812d4344a..995751d9207d1 100644 --- a/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.rs +++ b/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.rs @@ -1,34 +1,34 @@ -impl Solution { - pub fn count_pairs(n: i32, edges: Vec>) -> i64 { - let n = n as usize; - let mut g = vec![vec![]; n]; - let mut vis = vec![false; n]; - for e in edges { - let u = e[0] as usize; - let v = e[1] as usize; - g[u].push(v); - g[v].push(u); - } - - fn dfs(g: &Vec>, vis: &mut Vec, u: usize) -> i64 { - if vis[u] { - return 0; - } - vis[u] = true; - let mut cnt = 1; - for &v in &g[u] { - cnt += dfs(g, vis, v); - } - cnt - } - - let mut ans = 0; - let mut s = 0; - for u in 0..n { - let t = dfs(&g, &mut vis, u); - ans += t * s; - s += t; - } - ans - } -} +impl Solution { + pub fn count_pairs(n: i32, edges: Vec>) -> i64 { + let n = n as usize; + let mut g = vec![vec![]; n]; + let mut vis = vec![false; n]; + for e in edges { + let u = e[0] as usize; + let v = e[1] as usize; + g[u].push(v); + g[v].push(u); + } + + fn dfs(g: &Vec>, vis: &mut Vec, u: usize) -> i64 { + if vis[u] { + return 0; + } + vis[u] = true; + let mut cnt = 1; + for &v in &g[u] { + cnt += dfs(g, vis, v); + } + cnt + } + + let mut ans = 0; + let mut s = 0; + for u in 0..n { + let t = dfs(&g, &mut vis, u); + ans += t * s; + s += t; + } + ans + } +} diff --git a/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md b/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md index 4245d0bab6c76..9a5ff4964fd8f 100644 --- a/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md +++ b/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md @@ -214,7 +214,6 @@ impl Solution { (dif + 1) / 2 + amount[2] } } - ``` ### **...** diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md b/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md index c24254fd2d2b1..2f223bf9e7605 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md @@ -359,19 +359,17 @@ class SmallestInfiniteSet { ```rust struct SmallestInfiniteSet { - counter: [bool; 1000] + counter: [bool; 1000], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SmallestInfiniteSet { - fn new() -> Self { Self { - counter: [true; 1000] + counter: [true; 1000], } } @@ -379,18 +377,16 @@ impl SmallestInfiniteSet { for i in 0..1000 { if self.counter[i] { self.counter[i] = false; - return i as i32 + 1; + return (i as i32) + 1; } } -1 } fn add_back(&mut self, num: i32) { - self.counter[num as usize - 1] = true; + self.counter[(num as usize) - 1] = true; } -} - -/** +}/** * Your SmallestInfiniteSet object will be instantiated and called as such: * let obj = SmallestInfiniteSet::new(); * let ret_1: i32 = obj.pop_smallest(); diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md b/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md index 2c7d7608d47ed..1de1949cd77b6 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md @@ -347,19 +347,17 @@ class SmallestInfiniteSet { ```rust struct SmallestInfiniteSet { - counter: [bool; 1000] + counter: [bool; 1000], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SmallestInfiniteSet { - fn new() -> Self { Self { - counter: [true; 1000] + counter: [true; 1000], } } @@ -367,18 +365,16 @@ impl SmallestInfiniteSet { for i in 0..1000 { if self.counter[i] { self.counter[i] = false; - return i as i32 + 1; + return (i as i32) + 1; } } -1 } fn add_back(&mut self, num: i32) { - self.counter[num as usize - 1] = true; + self.counter[(num as usize) - 1] = true; } -} - -/** +}/** * Your SmallestInfiniteSet object will be instantiated and called as such: * let obj = SmallestInfiniteSet::new(); * let ret_1: i32 = obj.pop_smallest(); diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs b/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs index 5658aa8c88485..c7b1d5492c354 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs @@ -1,17 +1,15 @@ struct SmallestInfiniteSet { - counter: [bool; 1000] + counter: [bool; 1000], } - /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl SmallestInfiniteSet { - fn new() -> Self { Self { - counter: [true; 1000] + counter: [true; 1000], } } @@ -19,20 +17,18 @@ impl SmallestInfiniteSet { for i in 0..1000 { if self.counter[i] { self.counter[i] = false; - return i as i32 + 1; + return (i as i32) + 1; } } -1 } fn add_back(&mut self, num: i32) { - self.counter[num as usize - 1] = true; + self.counter[(num as usize) - 1] = true; } -} - -/** +}/** * Your SmallestInfiniteSet object will be instantiated and called as such: * let obj = SmallestInfiniteSet::new(); * let ret_1: i32 = obj.pop_smallest(); * obj.add_back(num); - */ \ No newline at end of file + */ diff --git a/solution/2300-2399/2351.First Letter to Appear Twice/README.md b/solution/2300-2399/2351.First Letter to Appear Twice/README.md index a3be9da3db5b9..dd204ab527dac 100644 --- a/solution/2300-2399/2351.First Letter to Appear Twice/README.md +++ b/solution/2300-2399/2351.First Letter to Appear Twice/README.md @@ -232,10 +232,10 @@ impl Solution { pub fn repeated_character(s: String) -> char { let mut mask = 0; for &c in s.as_bytes() { - if mask & 1 << (c - b'a') as i32 != 0 { + if (mask & (1 << ((c - b'a') as i32))) != 0 { return c as char; } - mask |= 1 << (c - b'a') as i32; + mask |= 1 << ((c - b'a') as i32); } ' ' } diff --git a/solution/2300-2399/2351.First Letter to Appear Twice/README_EN.md b/solution/2300-2399/2351.First Letter to Appear Twice/README_EN.md index 1d231b93703bf..5f1375f659af9 100644 --- a/solution/2300-2399/2351.First Letter to Appear Twice/README_EN.md +++ b/solution/2300-2399/2351.First Letter to Appear Twice/README_EN.md @@ -212,10 +212,10 @@ impl Solution { pub fn repeated_character(s: String) -> char { let mut mask = 0; for &c in s.as_bytes() { - if mask & 1 << (c - b'a') as i32 != 0 { + if (mask & (1 << ((c - b'a') as i32))) != 0 { return c as char; } - mask |= 1 << (c - b'a') as i32; + mask |= 1 << ((c - b'a') as i32); } ' ' } diff --git a/solution/2300-2399/2351.First Letter to Appear Twice/Solution.rs b/solution/2300-2399/2351.First Letter to Appear Twice/Solution.rs index f7ff64d10524f..623858843b9d3 100644 --- a/solution/2300-2399/2351.First Letter to Appear Twice/Solution.rs +++ b/solution/2300-2399/2351.First Letter to Appear Twice/Solution.rs @@ -2,10 +2,10 @@ impl Solution { pub fn repeated_character(s: String) -> char { let mut mask = 0; for &c in s.as_bytes() { - if mask & 1 << (c - b'a') as i32 != 0 { + if (mask & (1 << ((c - b'a') as i32))) != 0 { return c as char; } - mask |= 1 << (c - b'a') as i32; + mask |= 1 << ((c - b'a') as i32); } ' ' } diff --git a/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.rs b/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.rs index e3dd2c2cc3d9b..a568287bed149 100644 --- a/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.rs +++ b/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.rs @@ -23,4 +23,4 @@ impl Solution { ret } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README.md b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README.md index b9868fea974a7..beaf837fbbbae 100644 --- a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README.md +++ b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README.md @@ -362,7 +362,7 @@ impl Solution { mut initial_energy: i32, mut initial_experience: i32, energy: Vec, - experience: Vec, + experience: Vec ) -> i32 { let n = energy.len(); let mut res = 0; diff --git a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README_EN.md b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README_EN.md index 6cab2ae23b479..bfb7be6f5ae7b 100644 --- a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README_EN.md +++ b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/README_EN.md @@ -336,7 +336,7 @@ impl Solution { mut initial_energy: i32, mut initial_experience: i32, energy: Vec, - experience: Vec, + experience: Vec ) -> i32 { let n = energy.len(); let mut res = 0; diff --git a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.rs b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.rs index e1b1fef2415d8..c2de3bb8c8f44 100644 --- a/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.rs +++ b/solution/2300-2399/2383.Minimum Hours of Training to Win a Competition/Solution.rs @@ -3,7 +3,7 @@ impl Solution { mut initial_energy: i32, mut initial_experience: i32, energy: Vec, - experience: Vec, + experience: Vec ) -> i32 { let n = energy.len(); let mut res = 0; diff --git a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.rs b/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.rs index 80fdb2cdd2c38..e2e4c67d6363f 100644 --- a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.rs +++ b/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.rs @@ -16,4 +16,4 @@ impl Solution { }) .collect() } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md index 04f2bdb7fdd8a..817f8fbc6cb69 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md @@ -339,13 +339,7 @@ impl Solution { let mut count = [0, 0, 0]; for s in garbage.iter() { for c in s.as_bytes().iter() { - count[if c == &b'M' { - 0 - } else if c == &b'P' { - 1 - } else { - 2 - }] += 1; + count[if c == &b'M' { 0 } else if c == &b'P' { 1 } else { 2 }] += 1; } } diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md index e29f530f1740d..d9a3dc8f2d49d 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md @@ -320,13 +320,7 @@ impl Solution { let mut count = [0, 0, 0]; for s in garbage.iter() { for c in s.as_bytes().iter() { - count[if c == &b'M' { - 0 - } else if c == &b'P' { - 1 - } else { - 2 - }] += 1; + count[if c == &b'M' { 0 } else if c == &b'P' { 1 } else { 2 }] += 1; } } diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.rs b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.rs index 4e860c9655158..9407ce85f9eb0 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.rs +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.rs @@ -5,13 +5,7 @@ impl Solution { let mut count = [0, 0, 0]; for s in garbage.iter() { for c in s.as_bytes().iter() { - count[if c == &b'M' { - 0 - } else if c == &b'P' { - 1 - } else { - 2 - }] += 1; + count[if c == &b'M' { 0 } else if c == &b'P' { 1 } else { 2 }] += 1; } } diff --git a/solution/2400-2499/2404.Most Frequent Even Element/Solution.rs b/solution/2400-2499/2404.Most Frequent Even Element/Solution.rs index 6a84f2061ed12..d0b9ddb59e2ce 100644 --- a/solution/2400-2499/2404.Most Frequent Even Element/Solution.rs +++ b/solution/2400-2499/2404.Most Frequent Even Element/Solution.rs @@ -17,4 +17,4 @@ impl Solution { } ans } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README.md b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README.md index c7ecb2b072e54..619fe7539262c 100644 --- a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README.md +++ b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README.md @@ -322,15 +322,19 @@ impl Solution { if i == vals.len() { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: vals[i][j], - left: Self::create_tree(vals, i + 1, j * 2), - right: Self::create_tree(vals, i + 1, j * 2 + 1), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[i][j], + left: Self::create_tree(vals, i + 1, j * 2), + right: Self::create_tree(vals, i + 1, j * 2 + 1), + }) + ) + ) } pub fn reverse_odd_levels( - root: Option>>, + root: Option>> ) -> Option>> { let mut queue = VecDeque::new(); queue.push_back(root); diff --git a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README_EN.md b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README_EN.md index e003545ee4456..efd3e38fba4c3 100644 --- a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README_EN.md +++ b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README_EN.md @@ -307,15 +307,19 @@ impl Solution { if i == vals.len() { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: vals[i][j], - left: Self::create_tree(vals, i + 1, j * 2), - right: Self::create_tree(vals, i + 1, j * 2 + 1), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[i][j], + left: Self::create_tree(vals, i + 1, j * 2), + right: Self::create_tree(vals, i + 1, j * 2 + 1), + }) + ) + ) } pub fn reverse_odd_levels( - root: Option>>, + root: Option>> ) -> Option>> { let mut queue = VecDeque::new(); queue.push_back(root); diff --git a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/Solution.rs b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/Solution.rs index dcdac273dc072..e0974dbeb5099 100644 --- a/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/Solution.rs +++ b/solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/Solution.rs @@ -24,15 +24,19 @@ impl Solution { if i == vals.len() { return None; } - Some(Rc::new(RefCell::new(TreeNode { - val: vals[i][j], - left: Self::create_tree(vals, i + 1, j * 2), - right: Self::create_tree(vals, i + 1, j * 2 + 1), - }))) + Some( + Rc::new( + RefCell::new(TreeNode { + val: vals[i][j], + left: Self::create_tree(vals, i + 1, j * 2), + right: Self::create_tree(vals, i + 1, j * 2 + 1), + }) + ) + ) } pub fn reverse_odd_levels( - root: Option>>, + root: Option>> ) -> Option>> { let mut queue = VecDeque::new(); queue.push_back(root); diff --git a/solution/2400-2499/2418.Sort the People/README.md b/solution/2400-2499/2418.Sort the People/README.md index 427d94a16291c..a6d2bfb8640c6 100644 --- a/solution/2400-2499/2418.Sort the People/README.md +++ b/solution/2400-2499/2418.Sort the People/README.md @@ -218,7 +218,10 @@ impl Solution { pub fn sort_people(names: Vec, heights: Vec) -> Vec { let mut combine: Vec<(String, i32)> = names.into_iter().zip(heights.into_iter()).collect(); combine.sort_by(|a, b| b.1.cmp(&a.1)); - combine.iter().map(|s| s.0.clone()).collect() + combine + .iter() + .map(|s| s.0.clone()) + .collect() } } ``` diff --git a/solution/2400-2499/2418.Sort the People/README_EN.md b/solution/2400-2499/2418.Sort the People/README_EN.md index 4a6b0e7e1e2f6..a0dc70304ebfe 100644 --- a/solution/2400-2499/2418.Sort the People/README_EN.md +++ b/solution/2400-2499/2418.Sort the People/README_EN.md @@ -202,7 +202,10 @@ impl Solution { pub fn sort_people(names: Vec, heights: Vec) -> Vec { let mut combine: Vec<(String, i32)> = names.into_iter().zip(heights.into_iter()).collect(); combine.sort_by(|a, b| b.1.cmp(&a.1)); - combine.iter().map(|s| s.0.clone()).collect() + combine + .iter() + .map(|s| s.0.clone()) + .collect() } } ``` diff --git a/solution/2400-2499/2418.Sort the People/Solution.rs b/solution/2400-2499/2418.Sort the People/Solution.rs index c22aa495a7838..55943f6892813 100644 --- a/solution/2400-2499/2418.Sort the People/Solution.rs +++ b/solution/2400-2499/2418.Sort the People/Solution.rs @@ -2,6 +2,9 @@ impl Solution { pub fn sort_people(names: Vec, heights: Vec) -> Vec { let mut combine: Vec<(String, i32)> = names.into_iter().zip(heights.into_iter()).collect(); combine.sort_by(|a, b| b.1.cmp(&a.1)); - combine.iter().map(|s| s.0.clone()).collect() + combine + .iter() + .map(|s| s.0.clone()) + .collect() } } diff --git a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README.md b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README.md index 9d8b38657d344..72fd6cb483c18 100644 --- a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README.md +++ b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README.md @@ -214,7 +214,7 @@ impl Solution { let mut pre = 0; for log in logs.iter() { let t = log[1] - pre; - if t > max || t == max && res > log[0] { + if t > max || (t == max && res > log[0]) { res = log[0]; max = t; } diff --git a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README_EN.md b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README_EN.md index 9366584b2fd7c..addc0f1e582cc 100644 --- a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README_EN.md +++ b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/README_EN.md @@ -194,7 +194,7 @@ impl Solution { let mut pre = 0; for log in logs.iter() { let t = log[1] - pre; - if t > max || t == max && res > log[0] { + if t > max || (t == max && res > log[0]) { res = log[0]; max = t; } diff --git a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/Solution.rs b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/Solution.rs index 307f1928dc9e4..430c7163a95df 100644 --- a/solution/2400-2499/2432.The Employee That Worked on the Longest Task/Solution.rs +++ b/solution/2400-2499/2432.The Employee That Worked on the Longest Task/Solution.rs @@ -5,7 +5,7 @@ impl Solution { let mut pre = 0; for log in logs.iter() { let t = log[1] - pre; - if t > max || t == max && res > log[0] { + if t > max || (t == max && res > log[0]) { res = log[0]; max = t; } diff --git a/solution/2400-2499/2437.Number of Valid Clock Times/README.md b/solution/2400-2499/2437.Number of Valid Clock Times/README.md index 5966a7120a77c..8dddbe8aabf4c 100644 --- a/solution/2400-2499/2437.Number of Valid Clock Times/README.md +++ b/solution/2400-2499/2437.Number of Valid Clock Times/README.md @@ -298,9 +298,9 @@ impl Solution { let second = s.chars().nth(1).unwrap(); for i in 0..m { - let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10; + let a = first == '?' || (first.to_digit(10).unwrap() as usize) == i / 10; - let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10; + let b = second == '?' || (second.to_digit(10).unwrap() as usize) == i % 10; if a && b { cnt += 1; diff --git a/solution/2400-2499/2437.Number of Valid Clock Times/README_EN.md b/solution/2400-2499/2437.Number of Valid Clock Times/README_EN.md index ac8957abff393..df8943cab06d9 100644 --- a/solution/2400-2499/2437.Number of Valid Clock Times/README_EN.md +++ b/solution/2400-2499/2437.Number of Valid Clock Times/README_EN.md @@ -277,9 +277,9 @@ impl Solution { let second = s.chars().nth(1).unwrap(); for i in 0..m { - let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10; + let a = first == '?' || (first.to_digit(10).unwrap() as usize) == i / 10; - let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10; + let b = second == '?' || (second.to_digit(10).unwrap() as usize) == i % 10; if a && b { cnt += 1; diff --git a/solution/2400-2499/2437.Number of Valid Clock Times/Solution.rs b/solution/2400-2499/2437.Number of Valid Clock Times/Solution.rs index 89155fef1e110..6e5b779edc37c 100644 --- a/solution/2400-2499/2437.Number of Valid Clock Times/Solution.rs +++ b/solution/2400-2499/2437.Number of Valid Clock Times/Solution.rs @@ -6,9 +6,9 @@ impl Solution { let second = s.chars().nth(1).unwrap(); for i in 0..m { - let a = first == '?' || first.to_digit(10).unwrap() as usize == i / 10; + let a = first == '?' || (first.to_digit(10).unwrap() as usize) == i / 10; - let b = second == '?' || second.to_digit(10).unwrap() as usize == i % 10; + let b = second == '?' || (second.to_digit(10).unwrap() as usize) == i % 10; if a && b { cnt += 1; @@ -20,4 +20,4 @@ impl Solution { f(&time[..2], 24) * f(&time[3..], 60) } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README.md b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README.md index 7b205d41ce0c4..1112ed6c3b8dd 100644 --- a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README.md +++ b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README.md @@ -155,7 +155,7 @@ impl Solution { let s = nums.into_iter().collect::>(); let mut ans = -1; for &x in s.iter() { - if s.contains(&(-x)) { + if s.contains(&-x) { ans = ans.max(x); } } diff --git a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README_EN.md b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README_EN.md index 4bb17e1f12903..9e1ae3a7d6021 100644 --- a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README_EN.md +++ b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README_EN.md @@ -135,7 +135,7 @@ impl Solution { let s = nums.into_iter().collect::>(); let mut ans = -1; for &x in s.iter() { - if s.contains(&(-x)) { + if s.contains(&-x) { ans = ans.max(x); } } diff --git a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/Solution.rs b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/Solution.rs index eb6075fc44ad6..6461e26e7e347 100644 --- a/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/Solution.rs +++ b/solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/Solution.rs @@ -4,7 +4,7 @@ impl Solution { let s = nums.into_iter().collect::>(); let mut ans = -1; for &x in s.iter() { - if s.contains(&(-x)) { + if s.contains(&-x) { ans = ans.max(x); } } diff --git a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README.md b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README.md index 4efaeeaf04227..a7e8a534c1e34 100644 --- a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README.md +++ b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README.md @@ -156,7 +156,7 @@ impl Solution { set.insert({ let mut item = 0; while num > 0 { - item = item * 10 + num % 10; + item = item * 10 + (num % 10); num /= 10; } item diff --git a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README_EN.md b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README_EN.md index c5104ca002057..b49cffcc9f12b 100644 --- a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README_EN.md +++ b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/README_EN.md @@ -140,7 +140,7 @@ impl Solution { set.insert({ let mut item = 0; while num > 0 { - item = item * 10 + num % 10; + item = item * 10 + (num % 10); num /= 10; } item diff --git a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/Solution.rs b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/Solution.rs index 204b2512c09c6..173618a6fdce7 100644 --- a/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/Solution.rs +++ b/solution/2400-2499/2442.Count Number of Distinct Integers After Reverse Operations/Solution.rs @@ -8,7 +8,7 @@ impl Solution { set.insert({ let mut item = 0; while num > 0 { - item = item * 10 + num % 10; + item = item * 10 + (num % 10); num /= 10; } item diff --git a/solution/2400-2499/2443.Sum of Number and Its Reverse/README.md b/solution/2400-2499/2443.Sum of Number and Its Reverse/README.md index a35c12f5369df..3e7b0e9599630 100644 --- a/solution/2400-2499/2443.Sum of Number and Its Reverse/README.md +++ b/solution/2400-2499/2443.Sum of Number and Its Reverse/README.md @@ -167,15 +167,17 @@ function sumOfNumberAndReverse(num: number): boolean { impl Solution { pub fn sum_of_number_and_reverse(num: i32) -> bool { for i in 0..=num { - if i + { - let mut t = i; - let mut j = 0; - while t > 0 { - j = j * 10 + t % 10; - t /= 10; - } - j - } == num + if + i + + ({ + let mut t = i; + let mut j = 0; + while t > 0 { + j = j * 10 + (t % 10); + t /= 10; + } + j + }) == num { return true; } diff --git a/solution/2400-2499/2443.Sum of Number and Its Reverse/README_EN.md b/solution/2400-2499/2443.Sum of Number and Its Reverse/README_EN.md index 0df82afbc5e9a..6975e4a2b9c73 100644 --- a/solution/2400-2499/2443.Sum of Number and Its Reverse/README_EN.md +++ b/solution/2400-2499/2443.Sum of Number and Its Reverse/README_EN.md @@ -149,15 +149,17 @@ function sumOfNumberAndReverse(num: number): boolean { impl Solution { pub fn sum_of_number_and_reverse(num: i32) -> bool { for i in 0..=num { - if i + { - let mut t = i; - let mut j = 0; - while t > 0 { - j = j * 10 + t % 10; - t /= 10; - } - j - } == num + if + i + + ({ + let mut t = i; + let mut j = 0; + while t > 0 { + j = j * 10 + (t % 10); + t /= 10; + } + j + }) == num { return true; } diff --git a/solution/2400-2499/2443.Sum of Number and Its Reverse/Solution.rs b/solution/2400-2499/2443.Sum of Number and Its Reverse/Solution.rs index 708c843a0b0e1..c12f69e92235f 100644 --- a/solution/2400-2499/2443.Sum of Number and Its Reverse/Solution.rs +++ b/solution/2400-2499/2443.Sum of Number and Its Reverse/Solution.rs @@ -1,15 +1,17 @@ impl Solution { pub fn sum_of_number_and_reverse(num: i32) -> bool { for i in 0..=num { - if i + { - let mut t = i; - let mut j = 0; - while t > 0 { - j = j * 10 + t % 10; - t /= 10; - } - j - } == num + if + i + + ({ + let mut t = i; + let mut j = 0; + while t > 0 { + j = j * 10 + (t % 10); + t /= 10; + } + j + }) == num { return true; } diff --git a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README.md b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README.md index 6fa0dd3180ef6..3a6155ffc0be2 100644 --- a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README.md +++ b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README.md @@ -223,7 +223,7 @@ impl Solution { if num < min_k || num > max_k { k = i; } - res += 0.max(min_index.min(max_index) - k); + res += (0).max(min_index.min(max_index) - k); } res } diff --git a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md index 25bc95d00f294..3297a845e4a65 100644 --- a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md +++ b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md @@ -202,7 +202,7 @@ impl Solution { if num < min_k || num > max_k { k = i; } - res += 0.max(min_index.min(max_index) - k); + res += (0).max(min_index.min(max_index) - k); } res } diff --git a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/Solution.rs b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/Solution.rs index a8e67717c1de7..5b0737dd0d011 100644 --- a/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/Solution.rs +++ b/solution/2400-2499/2444.Count Subarrays With Fixed Bounds/Solution.rs @@ -16,7 +16,7 @@ impl Solution { if num < min_k || num > max_k { k = i; } - res += 0.max(min_index.min(max_index) - k); + res += (0).max(min_index.min(max_index) - k); } res } diff --git a/solution/2400-2499/2446.Determine if Two Events Have Conflict/Solution.rs b/solution/2400-2499/2446.Determine if Two Events Have Conflict/Solution.rs index eb50207e3c5e8..1f257196d8a5d 100644 --- a/solution/2400-2499/2446.Determine if Two Events Have Conflict/Solution.rs +++ b/solution/2400-2499/2446.Determine if Two Events Have Conflict/Solution.rs @@ -2,4 +2,4 @@ impl Solution { pub fn have_conflict(event1: Vec, event2: Vec) -> bool { !(event1[1] < event2[0] || event1[0] > event2[1]) } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README.md b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README.md index 73a0908c6132d..f564adbb1be0e 100644 --- a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README.md +++ b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README.md @@ -257,15 +257,13 @@ impl Solution { let mut zip_vec: Vec<_> = nums.into_iter().zip(cost.into_iter()).collect(); // Sort the zip vector based on nums - zip_vec.sort_by(|lhs, rhs| { - lhs.0.cmp(&rhs.0) - }); + zip_vec.sort_by(|lhs, rhs| { lhs.0.cmp(&rhs.0) }); let (nums, cost): (Vec, Vec) = zip_vec.into_iter().unzip(); let mut sum: i64 = 0; for &c in &cost { - sum += c as i64 ; + sum += c as i64; } let middle_cost = (sum + 1) / 2; let mut cur_sum: i64 = 0; @@ -273,7 +271,7 @@ impl Solution { let n = nums.len(); while i < n { - if cost[i] as i64 + cur_sum >= middle_cost { + if (cost[i] as i64) + cur_sum >= middle_cost { break; } cur_sum += cost[i] as i64; @@ -292,7 +290,7 @@ impl Solution { if v[i] == e { continue; } - ret += (v[i] - e).abs() as i64 * c[i] as i64; + ret += ((v[i] - e).abs() as i64) * (c[i] as i64); } ret diff --git a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README_EN.md b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README_EN.md index 0f691afbbdadb..731f4cafc2410 100644 --- a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README_EN.md +++ b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/README_EN.md @@ -215,15 +215,13 @@ impl Solution { let mut zip_vec: Vec<_> = nums.into_iter().zip(cost.into_iter()).collect(); // Sort the zip vector based on nums - zip_vec.sort_by(|lhs, rhs| { - lhs.0.cmp(&rhs.0) - }); + zip_vec.sort_by(|lhs, rhs| { lhs.0.cmp(&rhs.0) }); let (nums, cost): (Vec, Vec) = zip_vec.into_iter().unzip(); let mut sum: i64 = 0; for &c in &cost { - sum += c as i64 ; + sum += c as i64; } let middle_cost = (sum + 1) / 2; let mut cur_sum: i64 = 0; @@ -231,7 +229,7 @@ impl Solution { let n = nums.len(); while i < n { - if cost[i] as i64 + cur_sum >= middle_cost { + if (cost[i] as i64) + cur_sum >= middle_cost { break; } cur_sum += cost[i] as i64; @@ -250,7 +248,7 @@ impl Solution { if v[i] == e { continue; } - ret += (v[i] - e).abs() as i64 * c[i] as i64; + ret += ((v[i] - e).abs() as i64) * (c[i] as i64); } ret diff --git a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/Solution.rs b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/Solution.rs index cbc87fdce60f6..c48200d961381 100644 --- a/solution/2400-2499/2448.Minimum Cost to Make Array Equal/Solution.rs +++ b/solution/2400-2499/2448.Minimum Cost to Make Array Equal/Solution.rs @@ -4,15 +4,13 @@ impl Solution { let mut zip_vec: Vec<_> = nums.into_iter().zip(cost.into_iter()).collect(); // Sort the zip vector based on nums - zip_vec.sort_by(|lhs, rhs| { - lhs.0.cmp(&rhs.0) - }); + zip_vec.sort_by(|lhs, rhs| { lhs.0.cmp(&rhs.0) }); let (nums, cost): (Vec, Vec) = zip_vec.into_iter().unzip(); let mut sum: i64 = 0; for &c in &cost { - sum += c as i64 ; + sum += c as i64; } let middle_cost = (sum + 1) / 2; let mut cur_sum: i64 = 0; @@ -20,13 +18,13 @@ impl Solution { let n = nums.len(); while i < n { - if cost[i] as i64 + cur_sum >= middle_cost { + if (cost[i] as i64) + cur_sum >= middle_cost { break; } cur_sum += cost[i] as i64; i += 1; } - + Self::compute_manhattan_dis(&nums, &cost, nums[i]) } @@ -39,9 +37,9 @@ impl Solution { if v[i] == e { continue; } - ret += (v[i] - e).abs() as i64 * c[i] as i64; + ret += ((v[i] - e).abs() as i64) * (c[i] as i64); } ret } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2451.Odd String Difference/README.md b/solution/2400-2499/2451.Odd String Difference/README.md index a975b9425439a..b0aed8013c6e4 100644 --- a/solution/2400-2499/2451.Odd String Difference/README.md +++ b/solution/2400-2499/2451.Odd String Difference/README.md @@ -214,14 +214,19 @@ impl Solution { for w in words { let bytes: Vec = w - .bytes() - .zip(w.bytes().skip(1)) - .map(|(current, next)| (next - current) as i32) - .collect(); - - let s: String = bytes.iter().map(|&b| char::from(b as u8)).collect(); - - h.entry(s).or_insert(vec![]).push(w); + .bytes() + .zip(w.bytes().skip(1)) + .map(|(current, next)| (next - current) as i32) + .collect(); + + let s: String = bytes + .iter() + .map(|&b| char::from(b as u8)) + .collect(); + + h.entry(s) + .or_insert(vec![]) + .push(w); } for strs in h.values() { diff --git a/solution/2400-2499/2451.Odd String Difference/README_EN.md b/solution/2400-2499/2451.Odd String Difference/README_EN.md index e140a4073cbb8..3417e1f63a03b 100644 --- a/solution/2400-2499/2451.Odd String Difference/README_EN.md +++ b/solution/2400-2499/2451.Odd String Difference/README_EN.md @@ -198,14 +198,19 @@ impl Solution { for w in words { let bytes: Vec = w - .bytes() - .zip(w.bytes().skip(1)) - .map(|(current, next)| (next - current) as i32) - .collect(); - - let s: String = bytes.iter().map(|&b| char::from(b as u8)).collect(); - - h.entry(s).or_insert(vec![]).push(w); + .bytes() + .zip(w.bytes().skip(1)) + .map(|(current, next)| (next - current) as i32) + .collect(); + + let s: String = bytes + .iter() + .map(|&b| char::from(b as u8)) + .collect(); + + h.entry(s) + .or_insert(vec![]) + .push(w); } for strs in h.values() { diff --git a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README.md b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README.md index a5cda3e220352..0f9fc1c57b42d 100644 --- a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README.md +++ b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README.md @@ -175,16 +175,16 @@ impl Solution { impl Solution { pub fn average_value(nums: Vec) -> i32 { let filtered_nums: Vec = nums - .iter() - .cloned() - .filter(|&n| n % 6 == 0) - .collect(); + .iter() + .cloned() + .filter(|&n| n % 6 == 0) + .collect(); if filtered_nums.is_empty() { return 0; } - filtered_nums.iter().sum::() / filtered_nums.len() as i32 + filtered_nums.iter().sum::() / (filtered_nums.len() as i32) } } ``` diff --git a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README_EN.md b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README_EN.md index 4cdfdee097fc7..f91e21b95e028 100644 --- a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README_EN.md +++ b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/README_EN.md @@ -159,16 +159,16 @@ impl Solution { impl Solution { pub fn average_value(nums: Vec) -> i32 { let filtered_nums: Vec = nums - .iter() - .cloned() - .filter(|&n| n % 6 == 0) - .collect(); + .iter() + .cloned() + .filter(|&n| n % 6 == 0) + .collect(); if filtered_nums.is_empty() { return 0; } - filtered_nums.iter().sum::() / filtered_nums.len() as i32 + filtered_nums.iter().sum::() / (filtered_nums.len() as i32) } } ``` diff --git a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/Solution.rs b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/Solution.rs index a440d5c85d274..7c81f1bafa556 100644 --- a/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/Solution.rs +++ b/solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/Solution.rs @@ -13,4 +13,4 @@ impl Solution { } s / n } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2460.Apply Operations to an Array/Solution.rs b/solution/2400-2499/2460.Apply Operations to an Array/Solution.rs index 5413db81a7d61..2cba009d90f98 100644 --- a/solution/2400-2499/2460.Apply Operations to an Array/Solution.rs +++ b/solution/2400-2499/2460.Apply Operations to an Array/Solution.rs @@ -19,4 +19,4 @@ impl Solution { nums } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2465.Number of Distinct Averages/Solution.rs b/solution/2400-2499/2465.Number of Distinct Averages/Solution.rs index 8d4461da0b689..49ff2acb2eeb9 100644 --- a/solution/2400-2499/2465.Number of Distinct Averages/Solution.rs +++ b/solution/2400-2499/2465.Number of Distinct Averages/Solution.rs @@ -21,4 +21,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md b/solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md index cedd02eb85e73..8535640ea0f5e 100644 --- a/solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md +++ b/solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md @@ -373,7 +373,7 @@ impl Solution { let mut a = 0; for v in cnt.values() { let b = n - a - v; - ans += v * a * b;; + ans += v * a * b; a += v; } ans as i32 @@ -414,7 +414,6 @@ impl Solution { let n = nums.len(); for i in 1..n - 1 { - let mut l = 0; let mut r = i; while l < r { @@ -439,7 +438,7 @@ impl Solution { } let k = r; - ans += j * (n - k) + ans += j * (n - k); } ans as i32 diff --git a/solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md b/solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md index 5e4f4f2c1f75d..65fe9f005c141 100644 --- a/solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md +++ b/solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md @@ -341,7 +341,7 @@ impl Solution { let mut a = 0; for v in cnt.values() { let b = n - a - v; - ans += v * a * b;; + ans += v * a * b; a += v; } ans as i32 @@ -382,7 +382,6 @@ impl Solution { let n = nums.len(); for i in 1..n - 1 { - let mut l = 0; let mut r = i; while l < r { @@ -407,7 +406,7 @@ impl Solution { } let k = r; - ans += j * (n - k) + ans += j * (n - k); } ans as i32 diff --git a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/Solution.rs b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/Solution.rs index b048ad3902238..9540fe809a7a5 100644 --- a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/Solution.rs +++ b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/Solution.rs @@ -5,4 +5,4 @@ impl Solution { } n >> 1 } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2483.Minimum Penalty for a Shop/README.md b/solution/2400-2499/2483.Minimum Penalty for a Shop/README.md index 54c781f8711ff..4a4b7793bd70c 100644 --- a/solution/2400-2499/2483.Minimum Penalty for a Shop/README.md +++ b/solution/2400-2499/2483.Minimum Penalty for a Shop/README.md @@ -159,13 +159,13 @@ impl Solution { // Initialize the vector for (i, c) in customers.chars().enumerate() { - prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 }; + prefix_sum[i + 1] = prefix_sum[i] + (if c == 'Y' { 1 } else { 0 }); } // Calculate the answer for i in 0..=n { - if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 { - penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32; + if penalty > ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32) { + penalty = ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32); ret = i as i32; } } diff --git a/solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md b/solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md index dbb41f882267b..1e10edf5f50f3 100644 --- a/solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md +++ b/solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md @@ -141,13 +141,13 @@ impl Solution { // Initialize the vector for (i, c) in customers.chars().enumerate() { - prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 }; + prefix_sum[i + 1] = prefix_sum[i] + (if c == 'Y' { 1 } else { 0 }); } // Calculate the answer for i in 0..=n { - if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 { - penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32; + if penalty > ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32) { + penalty = ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32); ret = i as i32; } } diff --git a/solution/2400-2499/2483.Minimum Penalty for a Shop/Solution.rs b/solution/2400-2499/2483.Minimum Penalty for a Shop/Solution.rs index e27d60eef5ee0..2dc8cbb49a234 100644 --- a/solution/2400-2499/2483.Minimum Penalty for a Shop/Solution.rs +++ b/solution/2400-2499/2483.Minimum Penalty for a Shop/Solution.rs @@ -8,17 +8,17 @@ impl Solution { // Initialize the vector for (i, c) in customers.chars().enumerate() { - prefix_sum[i + 1] = prefix_sum[i] + if c == 'Y' { 1 } else { 0 }; + prefix_sum[i + 1] = prefix_sum[i] + (if c == 'Y' { 1 } else { 0 }); } // Calculate the answer for i in 0..=n { - if penalty > (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32 { - penalty = (prefix_sum[n] - prefix_sum[i]) as i32 + (i - prefix_sum[i]) as i32; + if penalty > ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32) { + penalty = ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32); ret = i as i32; } } ret } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2485.Find the Pivot Integer/README.md b/solution/2400-2499/2485.Find the Pivot Integer/README.md index 14c63b5e542be..c11b87948684a 100644 --- a/solution/2400-2499/2485.Find the Pivot Integer/README.md +++ b/solution/2400-2499/2485.Find the Pivot Integer/README.md @@ -228,7 +228,7 @@ class Solution { ```rust impl Solution { pub fn pivot_integer(n: i32) -> i32 { - let y = n * (n + 1) / 2; + let y = (n * (n + 1)) / 2; let x = (y as f64).sqrt() as i32; if x * x == y { diff --git a/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md b/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md index ec21dd81aeab1..ffc2625458cf4 100644 --- a/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md +++ b/solution/2400-2499/2485.Find the Pivot Integer/README_EN.md @@ -191,7 +191,7 @@ class Solution { ```rust impl Solution { pub fn pivot_integer(n: i32) -> i32 { - let y = n * (n + 1) / 2; + let y = (n * (n + 1)) / 2; let x = (y as f64).sqrt() as i32; if x * x == y { diff --git a/solution/2400-2499/2485.Find the Pivot Integer/Solution.rs b/solution/2400-2499/2485.Find the Pivot Integer/Solution.rs index ca82054322979..0eb9ad6745a39 100644 --- a/solution/2400-2499/2485.Find the Pivot Integer/Solution.rs +++ b/solution/2400-2499/2485.Find the Pivot Integer/Solution.rs @@ -1,6 +1,6 @@ impl Solution { pub fn pivot_integer(n: i32) -> i32 { - let y = n * (n + 1) / 2; + let y = (n * (n + 1)) / 2; let x = (y as f64).sqrt() as i32; if x * x == y { @@ -9,4 +9,4 @@ impl Solution { -1 } -} \ No newline at end of file +} diff --git a/solution/2400-2499/2490.Circular Sentence/Solution.rs b/solution/2400-2499/2490.Circular Sentence/Solution.rs index 72ec824da5d40..023a6fdc1bfa6 100644 --- a/solution/2400-2499/2490.Circular Sentence/Solution.rs +++ b/solution/2400-2499/2490.Circular Sentence/Solution.rs @@ -1,12 +1,12 @@ -impl Solution { - pub fn is_circular_sentence(sentence: String) -> bool { - let ss: Vec = sentence.split(' ').map(String::from).collect(); - let n = ss.len(); - for i in 0..n { - if ss[i].as_bytes()[ss[i].len() - 1] != ss[(i + 1) % n].as_bytes()[0] { - return false; - } - } - return true; - } -} \ No newline at end of file +impl Solution { + pub fn is_circular_sentence(sentence: String) -> bool { + let ss: Vec = sentence.split(' ').map(String::from).collect(); + let n = ss.len(); + for i in 0..n { + if ss[i].as_bytes()[ss[i].len() - 1] != ss[(i + 1) % n].as_bytes()[0] { + return false; + } + } + return true; + } +} diff --git a/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md b/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md index b3a557c5338cb..cdecbb47900dd 100644 --- a/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md +++ b/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md @@ -219,10 +219,10 @@ impl Solution { for c in s.chars() { if c >= 'a' && c <= 'z' { x = s.len(); - break + break; } - x = x * 10 + (c as u8 - b'0') as usize + x = x * 10 + (((c as u8) - b'0') as usize); } x as i32 @@ -252,7 +252,7 @@ impl Solution { match s.parse::() { Ok(v) => { ans = max(ans, v); - }, + } Err(_) => { ans = max(ans, s.len() as i32); } diff --git a/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md b/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md index f09b14f26dc3b..ad3e442f0f362 100644 --- a/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md +++ b/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md @@ -202,10 +202,10 @@ impl Solution { for c in s.chars() { if c >= 'a' && c <= 'z' { x = s.len(); - break + break; } - x = x * 10 + (c as u8 - b'0') as usize + x = x * 10 + (((c as u8) - b'0') as usize); } x as i32 @@ -235,7 +235,7 @@ impl Solution { match s.parse::() { Ok(v) => { ans = max(ans, v); - }, + } Err(_) => { ans = max(ans, s.len() as i32); } diff --git a/solution/2500-2599/2500.Delete Greatest Value in Each Row/Solution.rs b/solution/2500-2599/2500.Delete Greatest Value in Each Row/Solution.rs index ba6094986e047..ed1004361362e 100644 --- a/solution/2500-2599/2500.Delete Greatest Value in Each Row/Solution.rs +++ b/solution/2500-2599/2500.Delete Greatest Value in Each Row/Solution.rs @@ -20,4 +20,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2506.Count Pairs Of Similar Strings/README.md b/solution/2500-2599/2506.Count Pairs Of Similar Strings/README.md index 5691b25c53dfa..016357e7ac6a6 100644 --- a/solution/2500-2599/2506.Count Pairs Of Similar Strings/README.md +++ b/solution/2500-2599/2506.Count Pairs Of Similar Strings/README.md @@ -158,7 +158,7 @@ impl Solution { let mut v = 0; for c in w.chars() { - v |= (1 << (c as u8 - b'a')) + v |= 1 << ((c as u8) - b'a'); } ans += hash.get(&v).unwrap_or(&0); diff --git a/solution/2500-2599/2506.Count Pairs Of Similar Strings/README_EN.md b/solution/2500-2599/2506.Count Pairs Of Similar Strings/README_EN.md index 64811b54dfb0b..e5f585c08adef 100644 --- a/solution/2500-2599/2506.Count Pairs Of Similar Strings/README_EN.md +++ b/solution/2500-2599/2506.Count Pairs Of Similar Strings/README_EN.md @@ -143,7 +143,7 @@ impl Solution { let mut v = 0; for c in w.chars() { - v |= (1 << (c as u8 - b'a')) + v |= 1 << ((c as u8) - b'a'); } ans += hash.get(&v).unwrap_or(&0); diff --git a/solution/2500-2599/2506.Count Pairs Of Similar Strings/Solution.rs b/solution/2500-2599/2506.Count Pairs Of Similar Strings/Solution.rs index 829d0449b139c..dcbce9070638f 100644 --- a/solution/2500-2599/2506.Count Pairs Of Similar Strings/Solution.rs +++ b/solution/2500-2599/2506.Count Pairs Of Similar Strings/Solution.rs @@ -9,7 +9,7 @@ impl Solution { let mut v = 0; for c in w.chars() { - v |= (1 << (c as u8 - b'a')) + v |= 1 << ((c as u8) - b'a'); } ans += hash.get(&v).unwrap_or(&0); @@ -18,4 +18,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README.md b/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README.md index 138d78206d173..7889cea05ecfe 100644 --- a/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README.md +++ b/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README.md @@ -212,8 +212,20 @@ impl Solution { let mut ans = 0; let mut i = 0; - while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) { - if let Some((jdx, _)) = forts.iter().enumerate().skip(idx+1).find(|&(_, &x)| x != 0) { + while + let Some((idx, &value)) = forts + .iter() + .enumerate() + .skip(i) + .find(|&(_, &x)| x != 0) + { + if + let Some((jdx, _)) = forts + .iter() + .enumerate() + .skip(idx + 1) + .find(|&(_, &x)| x != 0) + { if value + forts[jdx] == 0 { ans = ans.max(jdx - idx - 1); } diff --git a/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README_EN.md b/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README_EN.md index d8bc186b8af3a..9fed7059d8a73 100644 --- a/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README_EN.md +++ b/solution/2500-2599/2511.Maximum Enemy Forts That Can Be Captured/README_EN.md @@ -198,8 +198,20 @@ impl Solution { let mut ans = 0; let mut i = 0; - while let Some((idx, &value)) = forts.iter().enumerate().skip(i).find(|&(_, &x)| x != 0) { - if let Some((jdx, _)) = forts.iter().enumerate().skip(idx+1).find(|&(_, &x)| x != 0) { + while + let Some((idx, &value)) = forts + .iter() + .enumerate() + .skip(i) + .find(|&(_, &x)| x != 0) + { + if + let Some((jdx, _)) = forts + .iter() + .enumerate() + .skip(idx + 1) + .find(|&(_, &x)| x != 0) + { if value + forts[jdx] == 0 { ans = ans.max(jdx - idx - 1); } diff --git a/solution/2500-2599/2512.Reward Top K Students/README.md b/solution/2500-2599/2512.Reward Top K Students/README.md index ce582a2993428..15fc215a3d73a 100644 --- a/solution/2500-2599/2512.Reward Top K Students/README.md +++ b/solution/2500-2599/2512.Reward Top K Students/README.md @@ -256,14 +256,14 @@ function topStudents( ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn top_students( positive_feedback: Vec, negative_feedback: Vec, report: Vec, student_id: Vec, - k: i32, + k: i32 ) -> Vec { let n = student_id.len(); let ps = positive_feedback.iter().collect::>(); @@ -289,7 +289,10 @@ impl Solution { } b.1.cmp(&a.1) }); - t.iter().map(|v| v.0).collect::>()[0..k as usize].to_vec() + t.iter() + .map(|v| v.0) + .collect::>() + [0..k as usize].to_vec() } } ``` diff --git a/solution/2500-2599/2512.Reward Top K Students/README_EN.md b/solution/2500-2599/2512.Reward Top K Students/README_EN.md index c9853482e2910..3900c7ddffae6 100644 --- a/solution/2500-2599/2512.Reward Top K Students/README_EN.md +++ b/solution/2500-2599/2512.Reward Top K Students/README_EN.md @@ -248,14 +248,14 @@ function topStudents( ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn top_students( positive_feedback: Vec, negative_feedback: Vec, report: Vec, student_id: Vec, - k: i32, + k: i32 ) -> Vec { let n = student_id.len(); let ps = positive_feedback.iter().collect::>(); @@ -281,7 +281,10 @@ impl Solution { } b.1.cmp(&a.1) }); - t.iter().map(|v| v.0).collect::>()[0..k as usize].to_vec() + t.iter() + .map(|v| v.0) + .collect::>() + [0..k as usize].to_vec() } } ``` diff --git a/solution/2500-2599/2512.Reward Top K Students/Solution.rs b/solution/2500-2599/2512.Reward Top K Students/Solution.rs index f1987361646cf..00890be7a7c70 100644 --- a/solution/2500-2599/2512.Reward Top K Students/Solution.rs +++ b/solution/2500-2599/2512.Reward Top K Students/Solution.rs @@ -1,11 +1,11 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; impl Solution { pub fn top_students( positive_feedback: Vec, negative_feedback: Vec, report: Vec, student_id: Vec, - k: i32, + k: i32 ) -> Vec { let n = student_id.len(); let ps = positive_feedback.iter().collect::>(); @@ -31,6 +31,9 @@ impl Solution { } b.1.cmp(&a.1) }); - t.iter().map(|v| v.0).collect::>()[0..k as usize].to_vec() + t.iter() + .map(|v| v.0) + .collect::>() + [0..k as usize].to_vec() } } diff --git a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md index 021a69377e1db..1023fe2ce5866 100644 --- a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md +++ b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md @@ -174,7 +174,9 @@ impl Solution { let start_index = start_index as usize; let n = words.len(); for i in 0..=n >> 1 { - if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target + if + words[(start_index - i + n) % n] == target || + words[(start_index + i) % n] == target { return i as i32; } @@ -193,8 +195,8 @@ impl Solution { for (i, w) in words.iter().enumerate() { if *w == target { - let t = (i as i32 - start_index).abs(); - ans = min(ans, min(t as usize, words.len() - t as usize)); + let t = ((i as i32) - start_index).abs(); + ans = min(ans, min(t as usize, words.len() - (t as usize))); } } diff --git a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md index 06ca6a88fb915..828acf1dd3a4d 100644 --- a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md +++ b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md @@ -161,7 +161,9 @@ impl Solution { let start_index = start_index as usize; let n = words.len(); for i in 0..=n >> 1 { - if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target + if + words[(start_index - i + n) % n] == target || + words[(start_index + i) % n] == target { return i as i32; } @@ -180,8 +182,8 @@ impl Solution { for (i, w) in words.iter().enumerate() { if *w == target { - let t = (i as i32 - start_index).abs(); - ans = min(ans, min(t as usize, words.len() - t as usize)); + let t = ((i as i32) - start_index).abs(); + ans = min(ans, min(t as usize, words.len() - (t as usize))); } } diff --git a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/Solution.rs b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/Solution.rs index f9f0ac0910815..14c0f92154042 100644 --- a/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/Solution.rs +++ b/solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/Solution.rs @@ -3,7 +3,9 @@ impl Solution { let start_index = start_index as usize; let n = words.len(); for i in 0..=n >> 1 { - if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target + if + words[(start_index - i + n) % n] == target || + words[(start_index + i) % n] == target { return i as i32; } diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md index fe85b79ac0f4f..5a1a682b04ec8 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md @@ -165,10 +165,11 @@ impl Solution { ```rust impl Solution { pub fn count_digits(num: i32) -> i32 { - num.to_string() + num + .to_string() .chars() .filter(|&c| c != '0') - .filter(|&c| num % c.to_digit(10).unwrap() as i32 == 0) + .filter(|&c| num % (c.to_digit(10).unwrap() as i32) == 0) .count() as i32 } } diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md index 6cddb8a99f601..dba09b82e6dfb 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md @@ -158,10 +158,11 @@ impl Solution { ```rust impl Solution { pub fn count_digits(num: i32) -> i32 { - num.to_string() + num + .to_string() .chars() .filter(|&c| c != '0') - .filter(|&c| num % c.to_digit(10).unwrap() as i32 == 0) + .filter(|&c| num % (c.to_digit(10).unwrap() as i32) == 0) .count() as i32 } } diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/README.md b/solution/2500-2599/2525.Categorize Box According to Criteria/README.md index 7bf81c1052b85..1eb3dbb1c987f 100644 --- a/solution/2500-2599/2525.Categorize Box According to Criteria/README.md +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/README.md @@ -253,7 +253,7 @@ function categorizeBox(length: number, width: number, height: number, mass: numb ```rust impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { - let v = length as i64 * width as i64 * height as i64; + let v = (length as i64) * (width as i64) * (height as i64); let mut i = 0; if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 { @@ -274,7 +274,11 @@ impl Solution { impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { let v = length * width * height; - let bulky = length >= 10000 || width >= 10000 || height >= 10000 || length as i64 * width as i64 * height as i64 >= 1000000000; + let bulky = + length >= 10000 || + width >= 10000 || + height >= 10000 || + (length as i64) * (width as i64) * (height as i64) >= 1000000000; let heavy = mass >= 100; diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md b/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md index 3803a1000109c..6b3395a4cbcdb 100644 --- a/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md @@ -243,7 +243,7 @@ function categorizeBox(length: number, width: number, height: number, mass: numb ```rust impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { - let v = length as i64 * width as i64 * height as i64; + let v = (length as i64) * (width as i64) * (height as i64); let mut i = 0; if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 { @@ -264,7 +264,11 @@ impl Solution { impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { let v = length * width * height; - let bulky = length >= 10000 || width >= 10000 || height >= 10000 || length as i64 * width as i64 * height as i64 >= 1000000000; + let bulky = + length >= 10000 || + width >= 10000 || + height >= 10000 || + (length as i64) * (width as i64) * (height as i64) >= 1000000000; let heavy = mass >= 100; diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.rs b/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.rs index fa38bde4480f1..4cc413c8ecf1e 100644 --- a/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.rs +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.rs @@ -1,6 +1,6 @@ impl Solution { pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String { - let v = length as i64 * width as i64 * height as i64; + let v = (length as i64) * (width as i64) * (height as i64); let mut i = 0; if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 { @@ -14,4 +14,4 @@ impl Solution { let d = vec!["Neither", "Bulky", "Heavy", "Both"]; d[i].to_string() } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md index d24f624ff2ff7..e102ac7807008 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README.md @@ -283,7 +283,7 @@ impl Solution { if n > 0 { a += 1; } else if n < 0 { - b += 1 + b += 1; } } diff --git a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md index 3e30476cc1070..f913dd7225da5 100644 --- a/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md +++ b/solution/2500-2599/2529.Maximum Count of Positive Integer and Negative Integer/README_EN.md @@ -260,7 +260,7 @@ impl Solution { if n > 0 { a += 1; } else if n < 0 { - b += 1 + b += 1; } } diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.rs b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.rs index 2cd9659f08458..8a0225418c261 100644 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.rs +++ b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.rs @@ -1,17 +1,17 @@ -use std::collections::BinaryHeap; - -impl Solution { - pub fn max_kelements(nums: Vec, k: i32) -> i64 { - let mut pq = BinaryHeap::from(nums); - let mut ans = 0; - let mut k = k; - while k > 0 { - if let Some(v) = pq.pop() { - ans += v as i64; - pq.push((v + 2) / 3); - k -= 1; - } - } - ans - } -} \ No newline at end of file +use std::collections::BinaryHeap; + +impl Solution { + pub fn max_kelements(nums: Vec, k: i32) -> i64 { + let mut pq = BinaryHeap::from(nums); + let mut ans = 0; + let mut k = k; + while k > 0 { + if let Some(v) = pq.pop() { + ans += v as i64; + pq.push((v + 2) / 3); + k -= 1; + } + } + ans + } +} diff --git a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md index 66d45fbe500f7..5c2efc512539f 100644 --- a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md +++ b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md @@ -174,7 +174,16 @@ impl Solution { impl Solution { pub fn difference_of_sum(nums: Vec) -> i32 { let a: i32 = nums.iter().sum(); - let b: i32 = nums.iter().map(|&n| n.to_string().chars().map(|c| c.to_digit(10).unwrap() as i32).sum::()).sum(); + let b: i32 = nums + .iter() + .map(|&n| + n + .to_string() + .chars() + .map(|c| c.to_digit(10).unwrap() as i32) + .sum::() + ) + .sum(); (a - b).abs() } } diff --git a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README_EN.md b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README_EN.md index ae5ec6b62f4f3..4df01ac3e2a11 100644 --- a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README_EN.md +++ b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README_EN.md @@ -164,7 +164,16 @@ impl Solution { impl Solution { pub fn difference_of_sum(nums: Vec) -> i32 { let a: i32 = nums.iter().sum(); - let b: i32 = nums.iter().map(|&n| n.to_string().chars().map(|c| c.to_digit(10).unwrap() as i32).sum::()).sum(); + let b: i32 = nums + .iter() + .map(|&n| + n + .to_string() + .chars() + .map(|c| c.to_digit(10).unwrap() as i32) + .sum::() + ) + .sum(); (a - b).abs() } } diff --git a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/Solution.rs b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/Solution.rs index f944110672e54..7b265d4053c2e 100644 --- a/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/Solution.rs +++ b/solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/Solution.rs @@ -9,6 +9,6 @@ impl Solution { num /= 10; } } - ans + ans } } diff --git a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README.md b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README.md index 5c3cbdb75abe4..a123093d2044c 100644 --- a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README.md +++ b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README.md @@ -204,7 +204,12 @@ impl Solution { let k = k as i64; let n = nums1.len(); if k == 0 { - return if nums1.iter().enumerate().all(|(i, &v)| v == nums2[i]) { + return if + nums1 + .iter() + .enumerate() + .all(|(i, &v)| v == nums2[i]) + { 0 } else { -1 diff --git a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README_EN.md b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README_EN.md index 54cf401323074..803a3b941f478 100644 --- a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README_EN.md +++ b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README_EN.md @@ -196,7 +196,12 @@ impl Solution { let k = k as i64; let n = nums1.len(); if k == 0 { - return if nums1.iter().enumerate().all(|(i, &v)| v == nums2[i]) { + return if + nums1 + .iter() + .enumerate() + .all(|(i, &v)| v == nums2[i]) + { 0 } else { -1 diff --git a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/Solution.rs b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/Solution.rs index 2188b990ed06e..88cea21652f3f 100644 --- a/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/Solution.rs +++ b/solution/2500-2599/2541.Minimum Operations to Make Array Equal II/Solution.rs @@ -3,7 +3,12 @@ impl Solution { let k = k as i64; let n = nums1.len(); if k == 0 { - return if nums1.iter().enumerate().all(|(i, &v)| v == nums2[i]) { + return if + nums1 + .iter() + .enumerate() + .all(|(i, &v)| v == nums2[i]) + { 0 } else { -1 diff --git a/solution/2500-2599/2544.Alternating Digit Sum/README.md b/solution/2500-2599/2544.Alternating Digit Sum/README.md index 0c20c3ad87208..4571283598fd4 100644 --- a/solution/2500-2599/2544.Alternating Digit Sum/README.md +++ b/solution/2500-2599/2544.Alternating Digit Sum/README.md @@ -173,7 +173,7 @@ impl Solution { let mut sign = 1; for c in format!("{}", n).chars() { - let x = c.to_digit(10).unwrap()as i32; + let x = c.to_digit(10).unwrap() as i32; ans += x * sign; sign *= -1; } diff --git a/solution/2500-2599/2544.Alternating Digit Sum/README_EN.md b/solution/2500-2599/2544.Alternating Digit Sum/README_EN.md index ee0f5f8ea39b7..0c33e6a7ebc7e 100644 --- a/solution/2500-2599/2544.Alternating Digit Sum/README_EN.md +++ b/solution/2500-2599/2544.Alternating Digit Sum/README_EN.md @@ -163,7 +163,7 @@ impl Solution { let mut sign = 1; for c in format!("{}", n).chars() { - let x = c.to_digit(10).unwrap()as i32; + let x = c.to_digit(10).unwrap() as i32; ans += x * sign; sign *= -1; } diff --git a/solution/2500-2599/2549.Count Distinct Numbers on Board/Solution.rs b/solution/2500-2599/2549.Count Distinct Numbers on Board/Solution.rs index 35943c6c56150..06a1e621e1d29 100644 --- a/solution/2500-2599/2549.Count Distinct Numbers on Board/Solution.rs +++ b/solution/2500-2599/2549.Count Distinct Numbers on Board/Solution.rs @@ -6,4 +6,4 @@ impl Solution { n - 1 } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2553.Separate the Digits in an Array/README.md b/solution/2500-2599/2553.Separate the Digits in an Array/README.md index 606857dadc4da..085b95e4c3364 100644 --- a/solution/2500-2599/2553.Separate the Digits in an Array/README.md +++ b/solution/2500-2599/2553.Separate the Digits in an Array/README.md @@ -169,7 +169,9 @@ impl Solution { t.push(num % 10); num /= 10; } - t.into_iter().rev().for_each(|v| ans.push(v)); + t.into_iter() + .rev() + .for_each(|v| ans.push(v)); } ans } diff --git a/solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md b/solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md index 6be282540660d..a1e2a841ebf67 100644 --- a/solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md +++ b/solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md @@ -155,7 +155,9 @@ impl Solution { t.push(num % 10); num /= 10; } - t.into_iter().rev().for_each(|v| ans.push(v)); + t.into_iter() + .rev() + .for_each(|v| ans.push(v)); } ans } diff --git a/solution/2500-2599/2553.Separate the Digits in an Array/Solution.rs b/solution/2500-2599/2553.Separate the Digits in an Array/Solution.rs index 3822e78d36270..b8c7593f39cdc 100644 --- a/solution/2500-2599/2553.Separate the Digits in an Array/Solution.rs +++ b/solution/2500-2599/2553.Separate the Digits in an Array/Solution.rs @@ -8,7 +8,9 @@ impl Solution { t.push(num % 10); num /= 10; } - t.into_iter().rev().for_each(|v| ans.push(v)); + t.into_iter() + .rev() + .for_each(|v| ans.push(v)); } ans } diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.rs b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.rs index f2e4a685205eb..64cb02cde7e1a 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.rs +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.rs @@ -16,4 +16,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2562.Find the Array Concatenation Value/README.md b/solution/2500-2599/2562.Find the Array Concatenation Value/README.md index e0962c8500f48..96423dbef5673 100644 --- a/solution/2500-2599/2562.Find the Array Concatenation Value/README.md +++ b/solution/2500-2599/2562.Find the Array Concatenation Value/README.md @@ -204,7 +204,9 @@ impl Solution { let mut n = nums.len(); for i in 0..n / 2 { - ans += format!("{}{}", nums[i], nums[n - i - 1]).parse::().unwrap(); + ans += format!("{}{}", nums[i], nums[n - i - 1]) + .parse::() + .unwrap(); } if n % 2 != 0 { diff --git a/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md b/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md index ca85244f679e4..e599f7e4c12ba 100644 --- a/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md +++ b/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md @@ -203,7 +203,9 @@ impl Solution { let mut n = nums.len(); for i in 0..n / 2 { - ans += format!("{}{}", nums[i], nums[n - i - 1]).parse::().unwrap(); + ans += format!("{}{}", nums[i], nums[n - i - 1]) + .parse::() + .unwrap(); } if n % 2 != 0 { diff --git a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README.md b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README.md index 2b945bfeed51f..aa11345ea171f 100644 --- a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README.md +++ b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README.md @@ -183,10 +183,7 @@ function minMaxDifference(num: number): number { impl Solution { pub fn min_max_difference(num: i32) -> i32 { let s = num.to_string(); - let min = s - .replace(char::from(s.as_bytes()[0]), "0") - .parse::() - .unwrap(); + let min = s.replace(char::from(s.as_bytes()[0]), "0").parse::().unwrap(); for &c in s.as_bytes() { if c != b'9' { return s.replace(c, "9").parse().unwrap() - min; diff --git a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README_EN.md b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README_EN.md index 2bc82fbae9635..5dc94e93cf41a 100644 --- a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README_EN.md +++ b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/README_EN.md @@ -161,10 +161,7 @@ function minMaxDifference(num: number): number { impl Solution { pub fn min_max_difference(num: i32) -> i32 { let s = num.to_string(); - let min = s - .replace(char::from(s.as_bytes()[0]), "0") - .parse::() - .unwrap(); + let min = s.replace(char::from(s.as_bytes()[0]), "0").parse::().unwrap(); for &c in s.as_bytes() { if c != b'9' { return s.replace(c, "9").parse().unwrap() - min; diff --git a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/Solution.rs b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/Solution.rs index fb7f839d904e0..9f694214fbcbb 100644 --- a/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/Solution.rs +++ b/solution/2500-2599/2566.Maximum Difference by Remapping a Digit/Solution.rs @@ -1,10 +1,7 @@ impl Solution { pub fn min_max_difference(num: i32) -> i32 { let s = num.to_string(); - let min = s - .replace(char::from(s.as_bytes()[0]), "0") - .parse::() - .unwrap(); + let min = s.replace(char::from(s.as_bytes()[0]), "0").parse::().unwrap(); for &c in s.as_bytes() { if c != b'9' { return s.replace(c, "9").parse().unwrap() - min; diff --git a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README.md b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README.md index 1632fe981d960..4c9c3131d289b 100644 --- a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README.md +++ b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README.md @@ -143,9 +143,7 @@ impl Solution { pub fn minimize_sum(mut nums: Vec) -> i32 { nums.sort(); let n = nums.len(); - (nums[n - 1] - nums[2]) - .min(nums[n - 2] - nums[1]) - .min(nums[n - 3] - nums[0]) + (nums[n - 1] - nums[2]).min(nums[n - 2] - nums[1]).min(nums[n - 3] - nums[0]) } } ``` diff --git a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README_EN.md b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README_EN.md index af1ced1c0b056..80c88445eef38 100644 --- a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README_EN.md +++ b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/README_EN.md @@ -114,9 +114,7 @@ impl Solution { pub fn minimize_sum(mut nums: Vec) -> i32 { nums.sort(); let n = nums.len(); - (nums[n - 1] - nums[2]) - .min(nums[n - 2] - nums[1]) - .min(nums[n - 3] - nums[0]) + (nums[n - 1] - nums[2]).min(nums[n - 2] - nums[1]).min(nums[n - 3] - nums[0]) } } ``` diff --git a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/Solution.rs b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/Solution.rs index 0146c812fe049..1a8f9ea9ef89e 100644 --- a/solution/2500-2599/2567.Minimum Score by Changing Two Elements/Solution.rs +++ b/solution/2500-2599/2567.Minimum Score by Changing Two Elements/Solution.rs @@ -2,8 +2,6 @@ impl Solution { pub fn minimize_sum(mut nums: Vec) -> i32 { nums.sort(); let n = nums.len(); - (nums[n - 1] - nums[2]) - .min(nums[n - 2] - nums[1]) - .min(nums[n - 3] - nums[0]) + (nums[n - 1] - nums[2]).min(nums[n - 2] - nums[1]).min(nums[n - 3] - nums[0]) } } diff --git a/solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/Solution.rs b/solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/Solution.rs index 4d9f0bdfd4695..5f23f66e563c3 100644 --- a/solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/Solution.rs +++ b/solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/Solution.rs @@ -19,4 +19,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/README.md b/solution/2500-2599/2574.Left and Right Sum Differences/README.md index 66272437b944e..7257b3e713d9d 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/README.md +++ b/solution/2500-2599/2574.Left and Right Sum Differences/README.md @@ -202,7 +202,6 @@ impl Solution { let mut ans = vec![]; for i in 0..nums.len() { - let mut left = 0; for j in 0..i { left += nums[j]; diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md b/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md index 6744a780c85ac..293017582ee0a 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md +++ b/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md @@ -179,7 +179,6 @@ impl Solution { let mut ans = vec![]; for i in 0..nums.len() { - let mut left = 0; for j in 0..i { left += nums[j]; diff --git a/solution/2500-2599/2578.Split With Minimum Sum/README.md b/solution/2500-2599/2578.Split With Minimum Sum/README.md index 78c5541c8516b..c5400f084235f 100644 --- a/solution/2500-2599/2578.Split With Minimum Sum/README.md +++ b/solution/2500-2599/2578.Split With Minimum Sum/README.md @@ -259,7 +259,7 @@ impl Solution { let mut n = 0; while num != 0 { - cnt[num as usize % 10] += 1; + cnt[(num as usize) % 10] += 1; num /= 10; n += 1; } @@ -272,7 +272,7 @@ impl Solution { } cnt[j] -= 1; - ans[i & 1] = ans[i & 1] * 10 + j as i32; + ans[i & 1] = ans[i & 1] * 10 + (j as i32); } ans[0] + ans[1] @@ -288,7 +288,7 @@ impl Solution { let mut ans = vec![0; 2]; for (i, c) in s.iter().enumerate() { - ans[i & 1] = ans[i & 1] * 10 + (c - b'0') as i32; + ans[i & 1] = ans[i & 1] * 10 + ((c - b'0') as i32); } ans[0] + ans[1] diff --git a/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md b/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md index 1ef594e0b8a66..3eb14a7797f69 100644 --- a/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md +++ b/solution/2500-2599/2578.Split With Minimum Sum/README_EN.md @@ -249,7 +249,7 @@ impl Solution { let mut n = 0; while num != 0 { - cnt[num as usize % 10] += 1; + cnt[(num as usize) % 10] += 1; num /= 10; n += 1; } @@ -262,7 +262,7 @@ impl Solution { } cnt[j] -= 1; - ans[i & 1] = ans[i & 1] * 10 + j as i32; + ans[i & 1] = ans[i & 1] * 10 + (j as i32); } ans[0] + ans[1] @@ -278,7 +278,7 @@ impl Solution { let mut ans = vec![0; 2]; for (i, c) in s.iter().enumerate() { - ans[i & 1] = ans[i & 1] * 10 + (c - b'0') as i32; + ans[i & 1] = ans[i & 1] * 10 + ((c - b'0') as i32); } ans[0] + ans[1] diff --git a/solution/2500-2599/2578.Split With Minimum Sum/Solution.rs b/solution/2500-2599/2578.Split With Minimum Sum/Solution.rs index 03f9943515536..05ded3c50570d 100644 --- a/solution/2500-2599/2578.Split With Minimum Sum/Solution.rs +++ b/solution/2500-2599/2578.Split With Minimum Sum/Solution.rs @@ -5,9 +5,9 @@ impl Solution { let mut ans = vec![0; 2]; for (i, c) in s.iter().enumerate() { - ans[i & 1] = ans[i & 1] * 10 + (c - b'0') as i32; + ans[i & 1] = ans[i & 1] * 10 + ((c - b'0') as i32); } ans[0] + ans[1] } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md b/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md index 758be48bb4bc3..b72c96a16a078 100644 --- a/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/README.md @@ -107,7 +107,7 @@ function coloredCells(n: number): number { ```rust impl Solution { pub fn colored_cells(n: i32) -> i64 { - 2 * (n as i64) * (n as i64 - 1) + 1 + 2 * (n as i64) * ((n as i64) - 1) + 1 } } ``` diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md b/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md index 5fc9e95f1b25a..996f8d781e658 100644 --- a/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/README_EN.md @@ -99,7 +99,7 @@ function coloredCells(n: number): number { ```rust impl Solution { pub fn colored_cells(n: i32) -> i64 { - 2 * (n as i64) * (n as i64 - 1) + 1 + 2 * (n as i64) * ((n as i64) - 1) + 1 } } ``` diff --git a/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.rs b/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.rs index a8d7269bcecb9..e421d712a47f5 100644 --- a/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.rs +++ b/solution/2500-2599/2579.Count Total Number of Colored Cells/Solution.rs @@ -1,5 +1,5 @@ -impl Solution { - pub fn colored_cells(n: i32) -> i64 { - 2 * (n as i64) * (n as i64 - 1) + 1 - } -} \ No newline at end of file +impl Solution { + pub fn colored_cells(n: i32) -> i64 { + 2 * (n as i64) * ((n as i64) - 1) + 1 + } +} diff --git a/solution/2500-2599/2582.Pass the Pillow/README.md b/solution/2500-2599/2582.Pass the Pillow/README.md index 5b2dd3d0c0844..714651f8af3fd 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README.md +++ b/solution/2500-2599/2582.Pass the Pillow/README.md @@ -224,8 +224,8 @@ impl Solution { let mut k = time / (n - 1); let mut _mod = time % (n - 1); - if k & 1 == 1 { - return n - _mod + if (k & 1) == 1 { + return n - _mod; } _mod + 1 diff --git a/solution/2500-2599/2582.Pass the Pillow/README_EN.md b/solution/2500-2599/2582.Pass the Pillow/README_EN.md index c3ee8c3eb31cb..6ed0a734663ef 100644 --- a/solution/2500-2599/2582.Pass the Pillow/README_EN.md +++ b/solution/2500-2599/2582.Pass the Pillow/README_EN.md @@ -211,8 +211,8 @@ impl Solution { let mut k = time / (n - 1); let mut _mod = time % (n - 1); - if k & 1 == 1 { - return n - _mod + if (k & 1) == 1 { + return n - _mod; } _mod + 1 diff --git a/solution/2500-2599/2582.Pass the Pillow/Solution.rs b/solution/2500-2599/2582.Pass the Pillow/Solution.rs index 0a0a222434bfd..1f72f9bcdcbd4 100644 --- a/solution/2500-2599/2582.Pass the Pillow/Solution.rs +++ b/solution/2500-2599/2582.Pass the Pillow/Solution.rs @@ -3,10 +3,10 @@ impl Solution { let mut k = time / (n - 1); let mut _mod = time % (n - 1); - if k & 1 == 1 { - return n - _mod + if (k & 1) == 1 { + return n - _mod; } _mod + 1 } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs index 84801564c6704..ae93d70adf5d0 100644 --- a/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs +++ b/solution/2500-2599/2586.Count the Number of Vowel Strings in Range/Solution.rs @@ -14,4 +14,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs index 02a714069ab73..551e2bfa01613 100644 --- a/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs +++ b/solution/2500-2599/2587.Rearrange Array to Maximize Prefix Score/Solution.rs @@ -1,13 +1,13 @@ -impl Solution { - pub fn max_score(mut nums: Vec) -> i32 { - nums.sort_by(|a, b| b.cmp(a)); - let mut s: i64 = 0; - for (i, &x) in nums.iter().enumerate() { - s += x as i64; - if s <= 0 { - return i as i32; - } - } - nums.len() as i32 - } -} \ No newline at end of file +impl Solution { + pub fn max_score(mut nums: Vec) -> i32 { + nums.sort_by(|a, b| b.cmp(a)); + let mut s: i64 = 0; + for (i, &x) in nums.iter().enumerate() { + s += x as i64; + if s <= 0 { + return i as i32; + } + } + nums.len() as i32 + } +} diff --git a/solution/2500-2599/2590.Design a Todo List/README.md b/solution/2500-2599/2590.Design a Todo List/README.md index be1cc93adf3fc..95b1788e9f1ba 100644 --- a/solution/2500-2599/2590.Design a Todo List/README.md +++ b/solution/2500-2599/2590.Design a Todo List/README.md @@ -204,7 +204,7 @@ class TodoList { ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; #[derive(Clone)] struct Task { @@ -229,15 +229,24 @@ impl TodoList { } } - fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + fn add_task( + &mut self, + user_id: i32, + task_description: String, + due_date: i32, + tags: Vec + ) -> i32 { if self.user_map.contains_key(&user_id) { // Just add the task - self.user_map.get_mut(&user_id).unwrap().push(Task { - task_id: self.id, - description: task_description, - tags: tags.into_iter().collect::>(), - due_date, - }); + self.user_map + .get_mut(&user_id) + .unwrap() + .push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); // Increase the global id self.id += 1; return self.id - 1; @@ -250,22 +259,23 @@ impl TodoList { description: task_description, tags: tags.into_iter().collect::>(), due_date, - }], + }] ); self.id += 1; self.id - 1 } fn get_all_tasks(&self, user_id: i32) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -274,15 +284,16 @@ impl TodoList { } fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -292,10 +303,16 @@ impl TodoList { } fn complete_task(&mut self, user_id: i32, task_id: i32) { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return; } - self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + self.user_map + .get_mut(&user_id) + .unwrap() + .retain(|x| (*x).task_id != task_id); } } ``` diff --git a/solution/2500-2599/2590.Design a Todo List/README_EN.md b/solution/2500-2599/2590.Design a Todo List/README_EN.md index 5f20a592a146b..1f6926e5656dc 100644 --- a/solution/2500-2599/2590.Design a Todo List/README_EN.md +++ b/solution/2500-2599/2590.Design a Todo List/README_EN.md @@ -194,7 +194,7 @@ class TodoList { ### **Rust** ```rust -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; #[derive(Clone)] struct Task { @@ -219,15 +219,24 @@ impl TodoList { } } - fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + fn add_task( + &mut self, + user_id: i32, + task_description: String, + due_date: i32, + tags: Vec + ) -> i32 { if self.user_map.contains_key(&user_id) { // Just add the task - self.user_map.get_mut(&user_id).unwrap().push(Task { - task_id: self.id, - description: task_description, - tags: tags.into_iter().collect::>(), - due_date, - }); + self.user_map + .get_mut(&user_id) + .unwrap() + .push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); // Increase the global id self.id += 1; return self.id - 1; @@ -240,22 +249,23 @@ impl TodoList { description: task_description, tags: tags.into_iter().collect::>(), due_date, - }], + }] ); self.id += 1; self.id - 1 } fn get_all_tasks(&self, user_id: i32) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -264,15 +274,16 @@ impl TodoList { } fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -282,10 +293,16 @@ impl TodoList { } fn complete_task(&mut self, user_id: i32, task_id: i32) { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return; } - self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + self.user_map + .get_mut(&user_id) + .unwrap() + .retain(|x| (*x).task_id != task_id); } } ``` diff --git a/solution/2500-2599/2590.Design a Todo List/Solution.rs b/solution/2500-2599/2590.Design a Todo List/Solution.rs index 08ded6ed325bf..e46160ba09c27 100644 --- a/solution/2500-2599/2590.Design a Todo List/Solution.rs +++ b/solution/2500-2599/2590.Design a Todo List/Solution.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{ HashMap, HashSet }; #[derive(Clone)] struct Task { @@ -23,15 +23,24 @@ impl TodoList { } } - fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + fn add_task( + &mut self, + user_id: i32, + task_description: String, + due_date: i32, + tags: Vec + ) -> i32 { if self.user_map.contains_key(&user_id) { // Just add the task - self.user_map.get_mut(&user_id).unwrap().push(Task { - task_id: self.id, - description: task_description, - tags: tags.into_iter().collect::>(), - due_date, - }); + self.user_map + .get_mut(&user_id) + .unwrap() + .push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); // Increase the global id self.id += 1; return self.id - 1; @@ -44,22 +53,23 @@ impl TodoList { description: task_description, tags: tags.into_iter().collect::>(), due_date, - }], + }] ); self.id += 1; self.id - 1 } fn get_all_tasks(&self, user_id: i32) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -68,15 +78,16 @@ impl TodoList { } fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return vec![]; } // Get the task vector let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); // Sort by due date - ret_vec.sort_by(|lhs, rhs| { - lhs.due_date.cmp(&rhs.due_date) - }); + ret_vec.sort_by(|lhs, rhs| { lhs.due_date.cmp(&rhs.due_date) }); // Return the description vector ret_vec .into_iter() @@ -86,9 +97,15 @@ impl TodoList { } fn complete_task(&mut self, user_id: i32, task_id: i32) { - if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + if + !self.user_map.contains_key(&user_id) || + self.user_map.get(&user_id).unwrap().is_empty() + { return; } - self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + self.user_map + .get_mut(&user_id) + .unwrap() + .retain(|x| (*x).task_id != task_id); } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md b/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md index f16c2585f7cd2..55c8660c82ecf 100644 --- a/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/README.md @@ -167,15 +167,15 @@ function distMoney(money: number, children: number): number { impl Solution { pub fn dist_money(money: i32, children: i32) -> i32 { if money < children { - return -1 + return -1; } if money > children * 8 { - return children - 1 + return children - 1; } if money == children * 8 - 4 { - return children - 2 + return children - 2; } (money - children) / 7 diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md index 1ad448f239881..8d859db8d070f 100644 --- a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md @@ -159,15 +159,15 @@ function distMoney(money: number, children: number): number { impl Solution { pub fn dist_money(money: i32, children: i32) -> i32 { if money < children { - return -1 + return -1; } if money > children * 8 { - return children - 1 + return children - 1; } if money == children * 8 - 4 { - return children - 2 + return children - 2; } (money - children) / 7 diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.rs b/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.rs index 36fa4da95377d..989c3033f46e9 100644 --- a/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.rs +++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/Solution.rs @@ -1,17 +1,17 @@ impl Solution { pub fn dist_money(money: i32, children: i32) -> i32 { if money < children { - return -1 + return -1; } if money > children * 8 { - return children - 1 + return children - 1; } if money == children * 8 - 4 { - return children - 2 + return children - 2; } (money - children) / 7 } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.rs b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.rs index dcd7114980f38..0c19d1c9ce05e 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.rs +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.rs @@ -12,4 +12,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/README.md b/solution/2600-2699/2600.K Items With the Maximum Sum/README.md index 7de3ffad0ec93..64bc3b9d1a91a 100644 --- a/solution/2600-2699/2600.K Items With the Maximum Sum/README.md +++ b/solution/2600-2699/2600.K Items With the Maximum Sum/README.md @@ -170,13 +170,18 @@ public class Solution { ```rust impl Solution { - pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 { + pub fn k_items_with_maximum_sum( + num_ones: i32, + num_zeros: i32, + num_neg_ones: i32, + k: i32 + ) -> i32 { if num_ones > k { - return k + return k; } if num_ones + num_zeros > k { - return num_ones + return num_ones; } num_ones - (k - num_ones - num_zeros) diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md b/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md index 8b121587ec5a1..9d251225cc9fb 100644 --- a/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md +++ b/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md @@ -149,13 +149,18 @@ public class Solution { ```rust impl Solution { - pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 { + pub fn k_items_with_maximum_sum( + num_ones: i32, + num_zeros: i32, + num_neg_ones: i32, + k: i32 + ) -> i32 { if num_ones > k { - return k + return k; } if num_ones + num_zeros > k { - return num_ones + return num_ones; } num_ones - (k - num_ones - num_zeros) diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.rs b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.rs index e1c6cc5163834..19093962f4269 100644 --- a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.rs +++ b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.rs @@ -1,13 +1,18 @@ impl Solution { - pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 { + pub fn k_items_with_maximum_sum( + num_ones: i32, + num_zeros: i32, + num_neg_ones: i32, + k: i32 + ) -> i32 { if num_ones > k { - return k + return k; } if num_ones + num_zeros > k { - return num_ones + return num_ones; } num_ones - (k - num_ones - num_zeros) } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md index cd84b50bc48b2..292b7e8de99d8 100644 --- a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md +++ b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md @@ -417,7 +417,6 @@ impl Solution { } else { ans = std::cmp::min(ans, std::cmp::min(a * 10 + b, b * 10 + a)); } - } } @@ -458,7 +457,6 @@ impl Solution { } } - std::cmp::min(a * 10 + b, b * 10 + a) } } diff --git a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md index a73854dd2998e..d2093f0a0d6bb 100644 --- a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md +++ b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md @@ -409,7 +409,6 @@ impl Solution { } else { ans = std::cmp::min(ans, std::cmp::min(a * 10 + b, b * 10 + a)); } - } } @@ -450,7 +449,6 @@ impl Solution { } } - std::cmp::min(a * 10 + b, b * 10 + a) } } diff --git a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/Solution.rs b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/Solution.rs index 2672efc69d3f0..df721423252e4 100644 --- a/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/Solution.rs +++ b/solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/Solution.rs @@ -29,7 +29,6 @@ impl Solution { } } - std::cmp::min(a * 10 + b, b * 10 + a) } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md index 69c32d411993c..039a40356ae73 100644 --- a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md @@ -316,7 +316,7 @@ impl Solution { if s.as_bytes()[k] == b'1' { cnt += 1; } else if cnt > 0 { - return false + return false; } } @@ -359,7 +359,7 @@ impl Solution { zero += 1; } else { one += 1; - ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2) + ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2); } } diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.rs b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.rs index c2b80367f64c2..a71bb46372784 100644 --- a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.rs +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.rs @@ -13,10 +13,10 @@ impl Solution { zero += 1; } else { one += 1; - ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2) + ans = std::cmp::max(ans, std::cmp::min(zero, one) * 2); } } ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2614.Prime In Diagonal/Solution.rs b/solution/2600-2699/2614.Prime In Diagonal/Solution.rs index 5dd3598e58bf8..2b3714ebc428d 100644 --- a/solution/2600-2699/2614.Prime In Diagonal/Solution.rs +++ b/solution/2600-2699/2614.Prime In Diagonal/Solution.rs @@ -29,4 +29,4 @@ impl Solution { true } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.rs b/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.rs index 74ee6c03c449e..0590659479aaf 100644 --- a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.rs +++ b/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.rs @@ -11,4 +11,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2643.Row With Maximum Ones/README.md b/solution/2600-2699/2643.Row With Maximum Ones/README.md index 2e4ce6434cbc5..f7b3f6225bf8a 100644 --- a/solution/2600-2699/2643.Row With Maximum Ones/README.md +++ b/solution/2600-2699/2643.Row With Maximum Ones/README.md @@ -166,7 +166,10 @@ impl Solution { let mut ans = vec![0, 0]; for (i, row) in mat.iter().enumerate() { - let cnt = row.iter().filter(|&v| *v == 1).count() as i32; + let cnt = row + .iter() + .filter(|&v| *v == 1) + .count() as i32; if ans[1] < cnt { ans[0] = i as i32; ans[1] = cnt; diff --git a/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md b/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md index 4ac5909a2425c..d741993dfc2f2 100644 --- a/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md +++ b/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md @@ -151,7 +151,10 @@ impl Solution { let mut ans = vec![0, 0]; for (i, row) in mat.iter().enumerate() { - let cnt = row.iter().filter(|&v| *v == 1).count() as i32; + let cnt = row + .iter() + .filter(|&v| *v == 1) + .count() as i32; if ans[1] < cnt { ans[0] = i as i32; ans[1] = cnt; diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs b/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs index 4ede733da57ca..7fab8e2e5b7b7 100644 --- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs +++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs @@ -3,7 +3,10 @@ impl Solution { let mut ans = vec![0, 0]; for (i, row) in mat.iter().enumerate() { - let cnt = row.iter().filter(|&v| *v == 1).count() as i32; + let cnt = row + .iter() + .filter(|&v| *v == 1) + .count() as i32; if ans[1] < cnt { ans[0] = i as i32; ans[1] = cnt; @@ -12,4 +15,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2644.Find the Maximum Divisibility Score/Solution.rs b/solution/2600-2699/2644.Find the Maximum Divisibility Score/Solution.rs index b57a6dfdde4bc..a9da678b8913c 100644 --- a/solution/2600-2699/2644.Find the Maximum Divisibility Score/Solution.rs +++ b/solution/2600-2699/2644.Find the Maximum Divisibility Score/Solution.rs @@ -20,4 +20,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2651.Calculate Delayed Arrival Time/Solution.rs b/solution/2600-2699/2651.Calculate Delayed Arrival Time/Solution.rs index 948d63a08181c..fd6c25a2e9e60 100644 --- a/solution/2600-2699/2651.Calculate Delayed Arrival Time/Solution.rs +++ b/solution/2600-2699/2651.Calculate Delayed Arrival Time/Solution.rs @@ -2,4 +2,4 @@ impl Solution { pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> i32 { (arrival_time + delayed_time) % 24 } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2652.Sum Multiples/README.md b/solution/2600-2699/2652.Sum Multiples/README.md index 250799568069c..3ad019390dddc 100644 --- a/solution/2600-2699/2652.Sum Multiples/README.md +++ b/solution/2600-2699/2652.Sum Multiples/README.md @@ -220,9 +220,7 @@ impl Solution { ```rust impl Solution { pub fn sum_of_multiples(n: i32) -> i32 { - (1..=n) - .filter(|&x| x % 3 == 0 || x % 5 == 0 || x % 7 == 0) - .sum() + (1..=n).filter(|&x| (x % 3 == 0 || x % 5 == 0 || x % 7 == 0)).sum() } } ``` @@ -232,7 +230,7 @@ impl Solution { pub fn sum_of_multiples(n: i32) -> i32 { fn f(x: i32, n: i32) -> i32 { let m = n / x; - (x + m * x) * m / 2 + ((x + m * x) * m) / 2 } f(3, n) + f(5, n) + f(7, n) - f(3 * 5, n) - f(3 * 7, n) - f(5 * 7, n) + f(3 * 5 * 7, n) diff --git a/solution/2600-2699/2652.Sum Multiples/README_EN.md b/solution/2600-2699/2652.Sum Multiples/README_EN.md index 834efd58ba43e..af9ed1f4614df 100644 --- a/solution/2600-2699/2652.Sum Multiples/README_EN.md +++ b/solution/2600-2699/2652.Sum Multiples/README_EN.md @@ -212,9 +212,7 @@ impl Solution { ```rust impl Solution { pub fn sum_of_multiples(n: i32) -> i32 { - (1..=n) - .filter(|&x| x % 3 == 0 || x % 5 == 0 || x % 7 == 0) - .sum() + (1..=n).filter(|&x| (x % 3 == 0 || x % 5 == 0 || x % 7 == 0)).sum() } } ``` @@ -224,7 +222,7 @@ impl Solution { pub fn sum_of_multiples(n: i32) -> i32 { fn f(x: i32, n: i32) -> i32 { let m = n / x; - (x + m * x) * m / 2 + ((x + m * x) * m) / 2 } f(3, n) + f(5, n) + f(7, n) - f(3 * 5, n) - f(3 * 7, n) - f(5 * 7, n) + f(3 * 5 * 7, n) diff --git a/solution/2600-2699/2652.Sum Multiples/Solution.rs b/solution/2600-2699/2652.Sum Multiples/Solution.rs index 92be03e5e3628..5024c3c5aac31 100644 --- a/solution/2600-2699/2652.Sum Multiples/Solution.rs +++ b/solution/2600-2699/2652.Sum Multiples/Solution.rs @@ -10,4 +10,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README.md b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README.md index 8450a59749e70..0c6d21eafa6f7 100644 --- a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README.md +++ b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README.md @@ -139,7 +139,7 @@ impl Solution { } } - (0 + k - 1) * k / 2 + k * mx + ((0 + k - 1) * k) / 2 + k * mx } } ``` @@ -149,7 +149,7 @@ impl Solution { pub fn maximize_sum(nums: Vec, k: i32) -> i32 { let mx = *nums.iter().max().unwrap_or(&0); - (0 + k - 1) * k / 2 + k * mx + ((0 + k - 1) * k) / 2 + k * mx } } ``` diff --git a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README_EN.md b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README_EN.md index 0506bc58ed19e..f85a04046246e 100644 --- a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README_EN.md +++ b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/README_EN.md @@ -138,7 +138,7 @@ impl Solution { } } - (0 + k - 1) * k / 2 + k * mx + ((0 + k - 1) * k) / 2 + k * mx } } ``` @@ -148,7 +148,7 @@ impl Solution { pub fn maximize_sum(nums: Vec, k: i32) -> i32 { let mx = *nums.iter().max().unwrap_or(&0); - (0 + k - 1) * k / 2 + k * mx + ((0 + k - 1) * k) / 2 + k * mx } } ``` diff --git a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/Solution.rs b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/Solution.rs index 00ca4b0314362..e0831a9ce4670 100644 --- a/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/Solution.rs +++ b/solution/2600-2699/2656.Maximum Sum With Exactly K Elements/Solution.rs @@ -8,6 +8,6 @@ impl Solution { } } - (0 + k - 1) * k / 2 + k * mx + ((0 + k - 1) * k) / 2 + k * mx } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README.md b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README.md index 22c6579e9bd9f..43b4f3be1c88b 100644 --- a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README.md +++ b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README.md @@ -214,9 +214,9 @@ impl Solution { let p1 = f(&player1); let p2 = f(&player2); if p1 > p2 { - return 1 + return 1; } else if p1 < p2 { - return 2 + return 2; } else { 0 } diff --git a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README_EN.md b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README_EN.md index 1f19e15d3f80c..b181546e91dd8 100644 --- a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README_EN.md +++ b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/README_EN.md @@ -206,9 +206,9 @@ impl Solution { let p1 = f(&player1); let p2 = f(&player2); if p1 > p2 { - return 1 + return 1; } else if p1 < p2 { - return 2 + return 2; } else { 0 } diff --git a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/Solution.rs b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/Solution.rs index 2fbfa7ec240f5..dbfc83cbbb3bc 100644 --- a/solution/2600-2699/2660.Determine the Winner of a Bowling Game/Solution.rs +++ b/solution/2600-2699/2660.Determine the Winner of a Bowling Game/Solution.rs @@ -17,11 +17,11 @@ impl Solution { let p1 = f(&player1); let p2 = f(&player2); if p1 > p2 { - return 1 + return 1; } else if p1 < p2 { - return 2 + return 2; } else { 0 } } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2670.Find the Distinct Difference Array/Solution.rs b/solution/2600-2699/2670.Find the Distinct Difference Array/Solution.rs index ec43003b20f60..78ccbb62109b2 100644 --- a/solution/2600-2699/2670.Find the Distinct Difference Array/Solution.rs +++ b/solution/2600-2699/2670.Find the Distinct Difference Array/Solution.rs @@ -20,4 +20,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs b/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs index a0a893b2e43fb..31713a2db248c 100644 --- a/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs +++ b/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs @@ -12,4 +12,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2679.Sum in a Matrix/README.md b/solution/2600-2699/2679.Sum in a Matrix/README.md index 2d1b811678a7f..6b142816fb107 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/README.md +++ b/solution/2600-2699/2679.Sum in a Matrix/README.md @@ -159,11 +159,18 @@ impl Solution { for row in nums.iter_mut() { row.sort(); } - let transposed: Vec> = (0..nums[0].len()).map(|i| { - nums.iter().map(|row| row[i]).collect() - }).collect(); - - transposed.iter().map(|row| row.iter().max().unwrap()).sum() + let transposed: Vec> = (0..nums[0].len()) + .map(|i| { + nums.iter() + .map(|row| row[i]) + .collect() + }) + .collect(); + + transposed + .iter() + .map(|row| row.iter().max().unwrap()) + .sum() } } ``` diff --git a/solution/2600-2699/2679.Sum in a Matrix/README_EN.md b/solution/2600-2699/2679.Sum in a Matrix/README_EN.md index bfe7e31ee9e41..1c781248bf5e7 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/README_EN.md +++ b/solution/2600-2699/2679.Sum in a Matrix/README_EN.md @@ -140,11 +140,18 @@ impl Solution { for row in nums.iter_mut() { row.sort(); } - let transposed: Vec> = (0..nums[0].len()).map(|i| { - nums.iter().map(|row| row[i]).collect() - }).collect(); - - transposed.iter().map(|row| row.iter().max().unwrap()).sum() + let transposed: Vec> = (0..nums[0].len()) + .map(|i| { + nums.iter() + .map(|row| row[i]) + .collect() + }) + .collect(); + + transposed + .iter() + .map(|row| row.iter().max().unwrap()) + .sum() } } ``` diff --git a/solution/2600-2699/2679.Sum in a Matrix/Solution.rs b/solution/2600-2699/2679.Sum in a Matrix/Solution.rs index db77668fd0814..93212861342c3 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/Solution.rs +++ b/solution/2600-2699/2679.Sum in a Matrix/Solution.rs @@ -1,19 +1,19 @@ -impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums.clone(); - for row in nums.iter_mut() { - row.sort(); - } - - let mut ans = 0; - for j in 0..nums[0].len() { - let mut mx = 0; - for row in &nums { - mx = mx.max(row[j]); - } - ans += mx; - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn matrix_sum(nums: Vec>) -> i32 { + let mut nums = nums.clone(); + for row in nums.iter_mut() { + row.sort(); + } + + let mut ans = 0; + for j in 0..nums[0].len() { + let mut mx = 0; + for row in &nums { + mx = mx.max(row[j]); + } + ans += mx; + } + + ans + } +} diff --git a/solution/2600-2699/2680.Maximum OR/README.md b/solution/2600-2699/2680.Maximum OR/README.md index c2e9cfa768885..4ba893f32abc0 100644 --- a/solution/2600-2699/2680.Maximum OR/README.md +++ b/solution/2600-2699/2680.Maximum OR/README.md @@ -163,7 +163,7 @@ impl Solution { let mut suf = vec![0; n + 1]; for i in (0..n).rev() { - suf[i] = suf[i + 1] | nums[i] as i64; + suf[i] = suf[i + 1] | (nums[i] as i64); } let mut ans = 0i64; diff --git a/solution/2600-2699/2680.Maximum OR/README_EN.md b/solution/2600-2699/2680.Maximum OR/README_EN.md index a34090a7539db..17fc0c7e7370e 100644 --- a/solution/2600-2699/2680.Maximum OR/README_EN.md +++ b/solution/2600-2699/2680.Maximum OR/README_EN.md @@ -153,7 +153,7 @@ impl Solution { let mut suf = vec![0; n + 1]; for i in (0..n).rev() { - suf[i] = suf[i + 1] | nums[i] as i64; + suf[i] = suf[i + 1] | (nums[i] as i64); } let mut ans = 0i64; diff --git a/solution/2600-2699/2680.Maximum OR/Solution.rs b/solution/2600-2699/2680.Maximum OR/Solution.rs index a7368ff329ba6..3513ea838c896 100644 --- a/solution/2600-2699/2680.Maximum OR/Solution.rs +++ b/solution/2600-2699/2680.Maximum OR/Solution.rs @@ -1,20 +1,20 @@ -impl Solution { - pub fn maximum_or(nums: Vec, k: i32) -> i64 { - let n = nums.len(); - let mut suf = vec![0; n + 1]; - - for i in (0..n).rev() { - suf[i] = suf[i + 1] | nums[i] as i64; - } - - let mut ans = 0i64; - let mut pre = 0i64; - let k64 = k as i64; - for i in 0..n { - ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]); - pre |= nums[i] as i64; - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn maximum_or(nums: Vec, k: i32) -> i64 { + let n = nums.len(); + let mut suf = vec![0; n + 1]; + + for i in (0..n).rev() { + suf[i] = suf[i + 1] | (nums[i] as i64); + } + + let mut ans = 0i64; + let mut pre = 0i64; + let k64 = k as i64; + for i in 0..n { + ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]); + pre |= nums[i] as i64; + } + + ans + } +} diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md b/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md index b8fea68b480d6..f240945a59087 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md @@ -166,7 +166,7 @@ impl Solution { let mut p = 1; while !vis[i] { vis[i] = true; - i = (i + p * k as usize) % n as usize; + i = (i + p * (k as usize)) % (n as usize); p += 1; } diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md index c7e36b7babe5b..173f9ae4a6adb 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md @@ -147,7 +147,7 @@ impl Solution { let mut p = 1; while !vis[i] { vis[i] = true; - i = (i + p * k as usize) % n as usize; + i = (i + p * (k as usize)) % (n as usize); p += 1; } diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.rs b/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.rs index 66fc9b3a7ad41..9c662a1efc039 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.rs +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.rs @@ -6,7 +6,7 @@ impl Solution { let mut p = 1; while !vis[i] { vis[i] = true; - i = (i + p * k as usize) % n as usize; + i = (i + p * (k as usize)) % (n as usize); p += 1; } @@ -19,4 +19,4 @@ impl Solution { ans } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2696.Minimum String Length After Removing Substrings/Solution.rs b/solution/2600-2699/2696.Minimum String Length After Removing Substrings/Solution.rs index 7610a5fd27502..997424c0de551 100644 --- a/solution/2600-2699/2696.Minimum String Length After Removing Substrings/Solution.rs +++ b/solution/2600-2699/2696.Minimum String Length After Removing Substrings/Solution.rs @@ -18,4 +18,4 @@ impl Solution { ans.len() as i32 } -} \ No newline at end of file +} diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs index 930cc065c88b2..6925d311c162b 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs @@ -19,4 +19,4 @@ impl Solution { String::from_utf8(b).unwrap() } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2706.Buy Two Chocolates/README.md b/solution/2700-2799/2706.Buy Two Chocolates/README.md index 337620897b6e8..44c151db72ca0 100644 --- a/solution/2700-2799/2706.Buy Two Chocolates/README.md +++ b/solution/2700-2799/2706.Buy Two Chocolates/README.md @@ -128,10 +128,7 @@ impl Solution { pub fn buy_choco(mut prices: Vec, money: i32) -> i32 { prices.sort(); - let sum = prices - .iter() - .take(2) - .sum::(); + let sum = prices.iter().take(2).sum::(); if sum > money { return money; diff --git a/solution/2700-2799/2706.Buy Two Chocolates/README_EN.md b/solution/2700-2799/2706.Buy Two Chocolates/README_EN.md index f5882ccaeb978..d89e62fe3e730 100644 --- a/solution/2700-2799/2706.Buy Two Chocolates/README_EN.md +++ b/solution/2700-2799/2706.Buy Two Chocolates/README_EN.md @@ -120,10 +120,7 @@ impl Solution { pub fn buy_choco(mut prices: Vec, money: i32) -> i32 { prices.sort(); - let sum = prices - .iter() - .take(2) - .sum::(); + let sum = prices.iter().take(2).sum::(); if sum > money { return money; diff --git a/solution/2700-2799/2706.Buy Two Chocolates/Solution.rs b/solution/2700-2799/2706.Buy Two Chocolates/Solution.rs index e9532d5e2caef..f74e4c654f782 100644 --- a/solution/2700-2799/2706.Buy Two Chocolates/Solution.rs +++ b/solution/2700-2799/2706.Buy Two Chocolates/Solution.rs @@ -9,4 +9,4 @@ impl Solution { money - sum } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2707.Extra Characters in a String/Solution.rs b/solution/2700-2799/2707.Extra Characters in a String/Solution.rs index 0465c2dec01a0..80051c32beb3c 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/Solution.rs +++ b/solution/2700-2799/2707.Extra Characters in a String/Solution.rs @@ -25,4 +25,4 @@ impl Solution { dp[n] } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2710.Remove Trailing Zeros From a String/README.md b/solution/2700-2799/2710.Remove Trailing Zeros From a String/README.md index aafd257579419..87a623b063fa3 100644 --- a/solution/2700-2799/2710.Remove Trailing Zeros From a String/README.md +++ b/solution/2700-2799/2710.Remove Trailing Zeros From a String/README.md @@ -130,12 +130,12 @@ impl Solution { impl Solution { pub fn remove_trailing_zeros(num: String) -> String { num.chars() - .rev() - .skip_while(|&c| c == '0') - .collect::() - .chars() - .rev() - .collect::() + .rev() + .skip_while(|&c| c == '0') + .collect::() + .chars() + .rev() + .collect::() } } ``` diff --git a/solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md b/solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md index 26aa553f85c02..5a75372d38155 100644 --- a/solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md +++ b/solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md @@ -116,12 +116,12 @@ impl Solution { impl Solution { pub fn remove_trailing_zeros(num: String) -> String { num.chars() - .rev() - .skip_while(|&c| c == '0') - .collect::() - .chars() - .rev() - .collect::() + .rev() + .skip_while(|&c| c == '0') + .collect::() + .chars() + .rev() + .collect::() } } ``` diff --git a/solution/2700-2799/2710.Remove Trailing Zeros From a String/Solution.rs b/solution/2700-2799/2710.Remove Trailing Zeros From a String/Solution.rs index 742d561d62b61..8454972216df4 100644 --- a/solution/2700-2799/2710.Remove Trailing Zeros From a String/Solution.rs +++ b/solution/2700-2799/2710.Remove Trailing Zeros From a String/Solution.rs @@ -8,4 +8,4 @@ impl Solution { num[..i + 1].to_string() } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2716.Minimize String Length/Solution.rs b/solution/2700-2799/2716.Minimize String Length/Solution.rs index 5e31eb6e7f2d7..642a8a0079ebb 100644 --- a/solution/2700-2799/2716.Minimize String Length/Solution.rs +++ b/solution/2700-2799/2716.Minimize String Length/Solution.rs @@ -10,4 +10,4 @@ impl Solution { set.len() as i32 } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/README.md b/solution/2700-2799/2717.Semi-Ordered Permutation/README.md index 90358af57a0d2..85dc0836d7779 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/README.md +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/README.md @@ -174,7 +174,7 @@ impl Solution { if nums[idx] == 1 { i = idx; } - if nums[idx] == n as i32 { + if nums[idx] == (n as i32) { j = idx; } } @@ -193,12 +193,22 @@ impl Solution { impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { let n = nums.len(); - let i = nums.iter().enumerate().find(|&(_, &v)| v == 1).map(|(i, _)| i).unwrap(); - let j = nums.iter().enumerate().find(|&(_, &v)| v == n as i32).map(|(i, _)| i).unwrap(); + let i = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == 1) + .map(|(i, _)| i) + .unwrap(); + let j = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == (n as i32)) + .map(|(i, _)| i) + .unwrap(); let mut ans = i - 1 + n - j; if i > j { - ans = i - 1 + n - j - 1; + ans = i - 1 + n - j - 1; } ans as i32 diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md b/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md index de3c1db0bf0f4..e01c401d5c615 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md @@ -156,7 +156,7 @@ impl Solution { if nums[idx] == 1 { i = idx; } - if nums[idx] == n as i32 { + if nums[idx] == (n as i32) { j = idx; } } @@ -175,12 +175,22 @@ impl Solution { impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { let n = nums.len(); - let i = nums.iter().enumerate().find(|&(_, &v)| v == 1).map(|(i, _)| i).unwrap(); - let j = nums.iter().enumerate().find(|&(_, &v)| v == n as i32).map(|(i, _)| i).unwrap(); + let i = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == 1) + .map(|(i, _)| i) + .unwrap(); + let j = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == (n as i32)) + .map(|(i, _)| i) + .unwrap(); let mut ans = i - 1 + n - j; if i > j { - ans = i - 1 + n - j - 1; + ans = i - 1 + n - j - 1; } ans as i32 diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs b/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs index 656f27474064b..2518893fa692a 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs @@ -1,14 +1,24 @@ impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { let n = nums.len(); - let i = nums.iter().enumerate().find(|&(_, &v)| v == 1).map(|(i, _)| i).unwrap(); - let j = nums.iter().enumerate().find(|&(_, &v)| v == n as i32).map(|(i, _)| i).unwrap(); + let i = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == 1) + .map(|(i, _)| i) + .unwrap(); + let j = nums + .iter() + .enumerate() + .find(|&(_, &v)| v == (n as i32)) + .map(|(i, _)| i) + .unwrap(); let mut ans = i - 1 + n - j; if i > j { - ans = i - 1 + n - j - 1; + ans = i - 1 + n - j - 1; } ans as i32 } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2729.Check if The Number is Fascinating/README.md b/solution/2700-2799/2729.Check if The Number is Fascinating/README.md index bb1b0118c7219..8b8150801c3f9 100644 --- a/solution/2700-2799/2729.Check if The Number is Fascinating/README.md +++ b/solution/2700-2799/2729.Check if The Number is Fascinating/README.md @@ -131,7 +131,7 @@ impl Solution { let mut cnt = vec![0; 10]; for c in s.chars() { - let t = c as usize - '0' as usize; + let t = (c as usize) - ('0' as usize); cnt[t] += 1; if cnt[t] > 1 { return false; @@ -173,7 +173,7 @@ impl Solution { for k in 1..=9 { if !hash.contains_key(&k) || hash[&k] > 1 { - return false + return false; } } diff --git a/solution/2700-2799/2729.Check if The Number is Fascinating/README_EN.md b/solution/2700-2799/2729.Check if The Number is Fascinating/README_EN.md index cd27391eada2e..326b56c8f6c9b 100644 --- a/solution/2700-2799/2729.Check if The Number is Fascinating/README_EN.md +++ b/solution/2700-2799/2729.Check if The Number is Fascinating/README_EN.md @@ -117,7 +117,7 @@ impl Solution { let mut cnt = vec![0; 10]; for c in s.chars() { - let t = c as usize - '0' as usize; + let t = (c as usize) - ('0' as usize); cnt[t] += 1; if cnt[t] > 1 { return false; @@ -159,7 +159,7 @@ impl Solution { for k in 1..=9 { if !hash.contains_key(&k) || hash[&k] > 1 { - return false + return false; } } diff --git a/solution/2700-2799/2729.Check if The Number is Fascinating/Solutions.rs b/solution/2700-2799/2729.Check if The Number is Fascinating/Solutions.rs index ca924bcc7265b..6520146ebcb20 100644 --- a/solution/2700-2799/2729.Check if The Number is Fascinating/Solutions.rs +++ b/solution/2700-2799/2729.Check if The Number is Fascinating/Solutions.rs @@ -4,7 +4,7 @@ impl Solution { let mut cnt = vec![0; 10]; for c in s.chars() { - let t = c as usize - '0' as usize; + let t = (c as usize) - ('0' as usize); cnt[t] += 1; if cnt[t] > 1 { return false; @@ -13,4 +13,4 @@ impl Solution { cnt[0] == 0 && s.len() == 9 } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.rs b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.rs index 2000ab43b2602..f20403def6ec4 100644 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.rs +++ b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.rs @@ -20,4 +20,4 @@ impl Solution { -1 } -} \ No newline at end of file +} diff --git a/solution/2700-2799/2739.Total Distance Traveled/Solution.rs b/solution/2700-2799/2739.Total Distance Traveled/Solution.rs index edf1ec36ab224..93ef72a95a321 100644 --- a/solution/2700-2799/2739.Total Distance Traveled/Solution.rs +++ b/solution/2700-2799/2739.Total Distance Traveled/Solution.rs @@ -1,19 +1,19 @@ -impl Solution { - pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 { - let mut cur = 0; - let mut ans = 0; - - while main_tank > 0 { - cur += 1; - main_tank -= 1; - ans += 10; - - if cur % 5 == 0 && additional_tank > 0 { - additional_tank -= 1; - main_tank += 1; - } - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 { + let mut cur = 0; + let mut ans = 0; + + while main_tank > 0 { + cur += 1; + main_tank -= 1; + ans += 10; + + if cur % 5 == 0 && additional_tank > 0 { + additional_tank -= 1; + main_tank += 1; + } + } + + ans + } +} diff --git a/solution/2700-2799/2742.Painting the Walls/README.md b/solution/2700-2799/2742.Painting the Walls/README.md index 96ada8e051bea..7d5bc8fd93a45 100644 --- a/solution/2700-2799/2742.Painting the Walls/README.md +++ b/solution/2700-2799/2742.Painting the Walls/README.md @@ -182,7 +182,14 @@ impl Solution { } #[allow(dead_code)] - fn dfs(record_vec: &mut Vec>, i: i32, j: i32, n: i32, time: &Vec, cost: &Vec) -> i32 { + fn dfs( + record_vec: &mut Vec>, + i: i32, + j: i32, + n: i32, + time: &Vec, + cost: &Vec + ) -> i32 { if n - i <= j - n { // All the remaining walls can be printed at no cost // Just return 0 @@ -196,7 +203,8 @@ impl Solution { if record_vec[i as usize][j as usize] == -1 { // This record hasn't been written record_vec[i as usize][j as usize] = std::cmp::min( - Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + cost[i as usize], + Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + + cost[i as usize], Self::dfs(record_vec, i + 1, j - 1, n, time, cost) ); } diff --git a/solution/2700-2799/2742.Painting the Walls/README_EN.md b/solution/2700-2799/2742.Painting the Walls/README_EN.md index 500011d4ab62d..77573471b46b3 100644 --- a/solution/2700-2799/2742.Painting the Walls/README_EN.md +++ b/solution/2700-2799/2742.Painting the Walls/README_EN.md @@ -174,7 +174,14 @@ impl Solution { } #[allow(dead_code)] - fn dfs(record_vec: &mut Vec>, i: i32, j: i32, n: i32, time: &Vec, cost: &Vec) -> i32 { + fn dfs( + record_vec: &mut Vec>, + i: i32, + j: i32, + n: i32, + time: &Vec, + cost: &Vec + ) -> i32 { if n - i <= j - n { // All the remaining walls can be printed at no cost // Just return 0 @@ -188,7 +195,8 @@ impl Solution { if record_vec[i as usize][j as usize] == -1 { // This record hasn't been written record_vec[i as usize][j as usize] = std::cmp::min( - Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + cost[i as usize], + Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + + cost[i as usize], Self::dfs(record_vec, i + 1, j - 1, n, time, cost) ); } diff --git a/solution/2700-2799/2742.Painting the Walls/Solution.rs b/solution/2700-2799/2742.Painting the Walls/Solution.rs index 4673b33bb54d9..d517f74a2d2c7 100644 --- a/solution/2700-2799/2742.Painting the Walls/Solution.rs +++ b/solution/2700-2799/2742.Painting the Walls/Solution.rs @@ -7,7 +7,14 @@ impl Solution { } #[allow(dead_code)] - fn dfs(record_vec: &mut Vec>, i: i32, j: i32, n: i32, time: &Vec, cost: &Vec) -> i32 { + fn dfs( + record_vec: &mut Vec>, + i: i32, + j: i32, + n: i32, + time: &Vec, + cost: &Vec + ) -> i32 { if n - i <= j - n { // All the remaining walls can be printed at no cost // Just return 0 @@ -21,10 +28,11 @@ impl Solution { if record_vec[i as usize][j as usize] == -1 { // This record hasn't been written record_vec[i as usize][j as usize] = std::cmp::min( - Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + cost[i as usize], + Self::dfs(record_vec, i + 1, j + time[i as usize], n, time, cost) + + cost[i as usize], Self::dfs(record_vec, i + 1, j - 1, n, time, cost) ); } record_vec[i as usize][j as usize] } -} \ No newline at end of file +} diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md b/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md index 7ae40e99e3dcc..646901d137e06 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/README.md @@ -287,7 +287,7 @@ function canSplitArray(nums: number[], m: number): boolean { impl Solution { pub fn can_split_array(nums: Vec, m: i32) -> bool { let n = nums.len(); - if (n <= 2) { + if n <= 2 { return true; } for i in 1..n { diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md b/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md index 4b3532c2cb06e..3a6dcfe787776 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/README_EN.md @@ -238,7 +238,7 @@ function canSplitArray(nums: number[], m: number): boolean { impl Solution { pub fn can_split_array(nums: Vec, m: i32) -> bool { let n = nums.len(); - if (n <= 2) { + if n <= 2 { return true; } for i in 1..n { diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs index 17fff0d17f5ce..c89b955dae3c0 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.rs @@ -1,7 +1,7 @@ impl Solution { pub fn can_split_array(nums: Vec, m: i32) -> bool { let n = nums.len(); - if (n <= 2) { + if n <= 2 { return true; } for i in 1..n { diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md b/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md index 4f8fd18173843..e417154d91bf8 100644 --- a/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/README.md @@ -563,11 +563,11 @@ impl Solution { } vis[i][j] = true; let n = g.len(); - i == n - 1 && j == n - 1 - || i != 0 && Self::dfs(i - 1, j, v, g, vis) - || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) - || j != 0 && Self::dfs(i, j - 1, v, g, vis) - || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + (i == n - 1 && j == n - 1) || + (i != 0 && Self::dfs(i - 1, j, v, g, vis)) || + (i != n - 1 && Self::dfs(i + 1, j, v, g, vis)) || + (j != 0 && Self::dfs(i, j - 1, v, g, vis)) || + (j != n - 1 && Self::dfs(i, j + 1, v, g, vis)) } pub fn maximum_safeness_factor(grid: Vec>) -> i32 { diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md b/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md index 235bf90853a3e..586838c351e07 100644 --- a/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/README_EN.md @@ -552,11 +552,11 @@ impl Solution { } vis[i][j] = true; let n = g.len(); - i == n - 1 && j == n - 1 - || i != 0 && Self::dfs(i - 1, j, v, g, vis) - || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) - || j != 0 && Self::dfs(i, j - 1, v, g, vis) - || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + (i == n - 1 && j == n - 1) || + (i != 0 && Self::dfs(i - 1, j, v, g, vis)) || + (i != n - 1 && Self::dfs(i + 1, j, v, g, vis)) || + (j != 0 && Self::dfs(i, j - 1, v, g, vis)) || + (j != n - 1 && Self::dfs(i, j + 1, v, g, vis)) } pub fn maximum_safeness_factor(grid: Vec>) -> i32 { diff --git a/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs b/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs index b5fc358c5a402..7ea01852f3f85 100644 --- a/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs +++ b/solution/2800-2899/2812.Find the Safest Path in a Grid/Solution.rs @@ -6,11 +6,11 @@ impl Solution { } vis[i][j] = true; let n = g.len(); - i == n - 1 && j == n - 1 - || i != 0 && Self::dfs(i - 1, j, v, g, vis) - || i != n - 1 && Self::dfs(i + 1, j, v, g, vis) - || j != 0 && Self::dfs(i, j - 1, v, g, vis) - || j != n - 1 && Self::dfs(i, j + 1, v, g, vis) + (i == n - 1 && j == n - 1) || + (i != 0 && Self::dfs(i - 1, j, v, g, vis)) || + (i != n - 1 && Self::dfs(i + 1, j, v, g, vis)) || + (j != 0 && Self::dfs(i, j - 1, v, g, vis)) || + (j != n - 1 && Self::dfs(i, j + 1, v, g, vis)) } pub fn maximum_safeness_factor(grid: Vec>) -> i32 { diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/README.md b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md index 7d300d32785a9..8635707cce086 100644 --- a/solution/2800-2899/2898.Maximum Linear Stock Score/README.md +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README.md @@ -152,7 +152,7 @@ impl Solution { let mut cnt: HashMap = HashMap::new(); for (i, x) in prices.iter().enumerate() { - let key = (*x) as i32 - (i as i32); + let key = (*x as i32) - (i as i32); let count = cnt.entry(key).or_insert(0); *count += *x as i64; } diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md b/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md index eaced4ea89c29..a22d304cd0712 100644 --- a/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/README_EN.md @@ -142,7 +142,7 @@ impl Solution { let mut cnt: HashMap = HashMap::new(); for (i, x) in prices.iter().enumerate() { - let key = (*x) as i32 - (i as i32); + let key = (*x as i32) - (i as i32); let count = cnt.entry(key).or_insert(0); *count += *x as i64; } diff --git a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs index 55ce7c70e2747..e3f27592f4457 100644 --- a/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs +++ b/solution/2800-2899/2898.Maximum Linear Stock Score/Solution.rs @@ -5,11 +5,11 @@ impl Solution { let mut cnt: HashMap = HashMap::new(); for (i, x) in prices.iter().enumerate() { - let key = (*x) as i32 - (i as i32); + let key = (*x as i32) - (i as i32); let count = cnt.entry(key).or_insert(0); *count += *x as i64; } *cnt.values().max().unwrap_or(&0) } -} \ No newline at end of file +} diff --git a/solution/2800-2899/2899.Last Visited Integers/README.md b/solution/2800-2899/2899.Last Visited Integers/README.md index bf57d9266e898..3124f3515bf49 100644 --- a/solution/2800-2899/2899.Last Visited Integers/README.md +++ b/solution/2800-2899/2899.Last Visited Integers/README.md @@ -190,7 +190,7 @@ impl Solution { for w in words { if w == "prev" { k += 1; - let i = nums.len() as i32 - k; + let i = (nums.len() as i32) - k; ans.push(if i < 0 { -1 } else { nums[i as usize] }); } else { k = 0; diff --git a/solution/2800-2899/2899.Last Visited Integers/README_EN.md b/solution/2800-2899/2899.Last Visited Integers/README_EN.md index 23a2b39552d29..5267739e590e8 100644 --- a/solution/2800-2899/2899.Last Visited Integers/README_EN.md +++ b/solution/2800-2899/2899.Last Visited Integers/README_EN.md @@ -180,7 +180,7 @@ impl Solution { for w in words { if w == "prev" { k += 1; - let i = nums.len() as i32 - k; + let i = (nums.len() as i32) - k; ans.push(if i < 0 { -1 } else { nums[i as usize] }); } else { k = 0; diff --git a/solution/2800-2899/2899.Last Visited Integers/Solution.rs b/solution/2800-2899/2899.Last Visited Integers/Solution.rs index c32e155383d3f..b27dbece5b313 100644 --- a/solution/2800-2899/2899.Last Visited Integers/Solution.rs +++ b/solution/2800-2899/2899.Last Visited Integers/Solution.rs @@ -1,20 +1,20 @@ -impl Solution { - pub fn last_visited_integers(words: Vec) -> Vec { - let mut nums: Vec = Vec::new(); - let mut ans: Vec = Vec::new(); - let mut k = 0; - - for w in words { - if w == "prev" { - k += 1; - let i = nums.len() as i32 - k; - ans.push(if i < 0 { -1 } else { nums[i as usize] }); - } else { - k = 0; - nums.push(w.parse::().unwrap()); - } - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn last_visited_integers(words: Vec) -> Vec { + let mut nums: Vec = Vec::new(); + let mut ans: Vec = Vec::new(); + let mut k = 0; + + for w in words { + if w == "prev" { + k += 1; + let i = (nums.len() as i32) - k; + ans.push(if i < 0 { -1 } else { nums[i as usize] }); + } else { + k = 0; + nums.push(w.parse::().unwrap()); + } + } + + ans + } +} diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md index 614ea171a7ce2..e5645172783be 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md @@ -144,7 +144,11 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { let mut ans = vec![]; for i in 0..n { diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md index fb9ab6379010b..7aaac16d981b3 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md @@ -135,7 +135,11 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { let mut ans = vec![]; for i in 0..n { diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs index a0dd4fa8c36b5..84c6385f2149d 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs @@ -1,13 +1,17 @@ -impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { - let mut ans = vec![]; - - for i in 0..n { - if i == 0 || groups[i as usize] != groups[(i - 1) as usize] { - ans.push(words[i as usize].clone()); - } - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { + let mut ans = vec![]; + + for i in 0..n { + if i == 0 || groups[i as usize] != groups[(i - 1) as usize] { + ans.push(words[i as usize].clone()); + } + } + + ans + } +} diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md index 132acb87de5f9..5d7e8097006df 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md @@ -353,9 +353,18 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { fn check(s: &str, t: &str) -> bool { - s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 + s.len() == t.len() && + s + .chars() + .zip(t.chars()) + .filter(|(a, b)| a != b) + .count() == 1 } let n = n as usize; diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md index 0d03406199177..3bfdc6178e752 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md @@ -343,9 +343,18 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { fn check(s: &str, t: &str) -> bool { - s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 + s.len() == t.len() && + s + .chars() + .zip(t.chars()) + .filter(|(a, b)| a != b) + .count() == 1 } let n = n as usize; diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs index ab0ab3148ead7..1ed2888baa22a 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs @@ -1,42 +1,51 @@ -impl Solution { - pub fn get_words_in_longest_subsequence(n: i32, words: Vec, groups: Vec) -> Vec { - fn check(s: &str, t: &str) -> bool { - s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 - } - - let n = n as usize; - - let mut f = vec![1; n]; - let mut g = vec![-1; n]; - - let mut mx = 1; - - for i in 0..n { - let x = groups[i] as usize; - for j in 0..i { - let y = groups[j] as usize; - if x != y && f[i] < f[j] + 1 && check(&words[i], &words[j]) { - f[i] = f[j] + 1; - g[i] = j as i32; - mx = mx.max(f[i]); - } - } - } - - let mut ans = vec![]; - let mut i = n - 1; - - while f[i] != mx { - i -= 1; - } - - let mut j = i as i32; - while j >= 0 { - ans.push(words[j as usize].clone()); - j = g[j as usize]; - } - - ans.reverse(); - ans - } -} \ No newline at end of file +impl Solution { + pub fn get_words_in_longest_subsequence( + n: i32, + words: Vec, + groups: Vec + ) -> Vec { + fn check(s: &str, t: &str) -> bool { + s.len() == t.len() && + s + .chars() + .zip(t.chars()) + .filter(|(a, b)| a != b) + .count() == 1 + } + + let n = n as usize; + + let mut f = vec![1; n]; + let mut g = vec![-1; n]; + + let mut mx = 1; + + for i in 0..n { + let x = groups[i] as usize; + for j in 0..i { + let y = groups[j] as usize; + if x != y && f[i] < f[j] + 1 && check(&words[i], &words[j]) { + f[i] = f[j] + 1; + g[i] = j as i32; + mx = mx.max(f[i]); + } + } + } + + let mut ans = vec![]; + let mut i = n - 1; + + while f[i] != mx { + i -= 1; + } + + let mut j = i as i32; + while j >= 0 { + ans.push(words[j as usize].clone()); + j = g[j as usize]; + } + + ans.reverse(); + ans + } +} diff --git a/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.rs b/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.rs index 9eafb91c0a842..1a70423c9f98e 100644 --- a/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.rs +++ b/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.rs @@ -1,29 +1,29 @@ -impl Solution { - pub fn find_indices(nums: Vec, index_difference: i32, value_difference: i32) -> Vec { - let index_difference = index_difference as usize; - let mut mi = 0; - let mut mx = 0; - - for i in index_difference..nums.len() { - let j = i - index_difference; - - if nums[j] < nums[mi] { - mi = j; - } - - if nums[j] > nums[mx] { - mx = j; - } - - if nums[i] - nums[mi] >= value_difference { - return vec![mi as i32, i as i32]; - } - - if nums[mx] - nums[i] >= value_difference { - return vec![mx as i32, i as i32]; - } - } - - vec![-1, -1] - } -} \ No newline at end of file +impl Solution { + pub fn find_indices(nums: Vec, index_difference: i32, value_difference: i32) -> Vec { + let index_difference = index_difference as usize; + let mut mi = 0; + let mut mx = 0; + + for i in index_difference..nums.len() { + let j = i - index_difference; + + if nums[j] < nums[mi] { + mi = j; + } + + if nums[j] > nums[mx] { + mx = j; + } + + if nums[i] - nums[mi] >= value_difference { + return vec![mi as i32, i as i32]; + } + + if nums[mx] - nums[i] >= value_difference { + return vec![mx as i32, i as i32]; + } + } + + vec![-1, -1] + } +} diff --git a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md index 4a90e6e2fb4b0..eb4ad74d2a0d0 100644 --- a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md +++ b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md @@ -319,10 +319,12 @@ impl Solution { let mut ans = String::new(); for i in 0..n { - for j in (i + k as usize)..=n { + for j in i + (k as usize)..=n { let t = &s[i..j]; - if t.matches('1').count() as i32 == k && - (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && t < &ans)) { + if + (t.matches('1').count() as i32) == k && + (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && t < &ans)) + { ans = t.to_string(); } } @@ -356,7 +358,10 @@ impl Solution { j += 1; - if cnt == k && (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) { + if + cnt == k && + (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) + { ans = s_chars[i..j].iter().collect(); } } diff --git a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README_EN.md b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README_EN.md index 403b49ace51d2..79d625c320b43 100644 --- a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README_EN.md +++ b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README_EN.md @@ -309,10 +309,12 @@ impl Solution { let mut ans = String::new(); for i in 0..n { - for j in (i + k as usize)..=n { + for j in i + (k as usize)..=n { let t = &s[i..j]; - if t.matches('1').count() as i32 == k && - (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && t < &ans)) { + if + (t.matches('1').count() as i32) == k && + (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && t < &ans)) + { ans = t.to_string(); } } @@ -346,7 +348,10 @@ impl Solution { j += 1; - if cnt == k && (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) { + if + cnt == k && + (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) + { ans = s_chars[i..j].iter().collect(); } } diff --git a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.rs b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.rs index 66c81befce5c3..d69f0aad21db3 100644 --- a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.rs +++ b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.rs @@ -1,31 +1,34 @@ -impl Solution { - pub fn shortest_beautiful_substring(s: String, k: i32) -> String { - let s_chars: Vec = s.chars().collect(); - let mut i = 0; - let mut j = 0; - let mut cnt = 0; - let mut ans = String::new(); - let n = s.len(); - - while j < n { - if s_chars[j] == '1' { - cnt += 1; - } - - while cnt > k || (i < j && s_chars[i] == '0') { - if s_chars[i] == '1' { - cnt -= 1; - } - i += 1; - } - - j += 1; - - if cnt == k && (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) { - ans = s_chars[i..j].iter().collect(); - } - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn shortest_beautiful_substring(s: String, k: i32) -> String { + let s_chars: Vec = s.chars().collect(); + let mut i = 0; + let mut j = 0; + let mut cnt = 0; + let mut ans = String::new(); + let n = s.len(); + + while j < n { + if s_chars[j] == '1' { + cnt += 1; + } + + while cnt > k || (i < j && s_chars[i] == '0') { + if s_chars[i] == '1' { + cnt -= 1; + } + i += 1; + } + + j += 1; + + if + cnt == k && + (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) + { + ans = s_chars[i..j].iter().collect(); + } + } + + ans + } +} diff --git a/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.rs b/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.rs index 9eafb91c0a842..1a70423c9f98e 100644 --- a/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.rs +++ b/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.rs @@ -1,29 +1,29 @@ -impl Solution { - pub fn find_indices(nums: Vec, index_difference: i32, value_difference: i32) -> Vec { - let index_difference = index_difference as usize; - let mut mi = 0; - let mut mx = 0; - - for i in index_difference..nums.len() { - let j = i - index_difference; - - if nums[j] < nums[mi] { - mi = j; - } - - if nums[j] > nums[mx] { - mx = j; - } - - if nums[i] - nums[mi] >= value_difference { - return vec![mi as i32, i as i32]; - } - - if nums[mx] - nums[i] >= value_difference { - return vec![mx as i32, i as i32]; - } - } - - vec![-1, -1] - } -} \ No newline at end of file +impl Solution { + pub fn find_indices(nums: Vec, index_difference: i32, value_difference: i32) -> Vec { + let index_difference = index_difference as usize; + let mut mi = 0; + let mut mx = 0; + + for i in index_difference..nums.len() { + let j = i - index_difference; + + if nums[j] < nums[mi] { + mi = j; + } + + if nums[j] > nums[mx] { + mx = j; + } + + if nums[i] - nums[mi] >= value_difference { + return vec![mi as i32, i as i32]; + } + + if nums[mx] - nums[i] >= value_difference { + return vec![mx as i32, i as i32]; + } + } + + vec![-1, -1] + } +} diff --git a/solution/2900-2999/2906.Construct Product Matrix/README.md b/solution/2900-2999/2906.Construct Product Matrix/README.md index 9a094aeeb51b0..d9eaea6989a7f 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/README.md +++ b/solution/2900-2999/2906.Construct Product Matrix/README.md @@ -213,7 +213,7 @@ impl Solution { for i in (0..n).rev() { for j in (0..m).rev() { p[i][j] = suf; - suf = (suf as i64 * grid[i][j] as i64 % modulo as i64) as i32; + suf = (((suf as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; } } @@ -221,8 +221,8 @@ impl Solution { for i in 0..n { for j in 0..m { - p[i][j] = (p[i][j] as i64 * pre as i64 % modulo as i64) as i32; - pre = (pre as i64 * grid[i][j] as i64 % modulo as i64) as i32; + p[i][j] = (((p[i][j] as i64) * (pre as i64)) % (modulo as i64)) as i32; + pre = (((pre as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; } } diff --git a/solution/2900-2999/2906.Construct Product Matrix/README_EN.md b/solution/2900-2999/2906.Construct Product Matrix/README_EN.md index bed751960330f..2f9ce8e0924fd 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/README_EN.md +++ b/solution/2900-2999/2906.Construct Product Matrix/README_EN.md @@ -203,7 +203,7 @@ impl Solution { for i in (0..n).rev() { for j in (0..m).rev() { p[i][j] = suf; - suf = (suf as i64 * grid[i][j] as i64 % modulo as i64) as i32; + suf = (((suf as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; } } @@ -211,8 +211,8 @@ impl Solution { for i in 0..n { for j in 0..m { - p[i][j] = (p[i][j] as i64 * pre as i64 % modulo as i64) as i32; - pre = (pre as i64 * grid[i][j] as i64 % modulo as i64) as i32; + p[i][j] = (((p[i][j] as i64) * (pre as i64)) % (modulo as i64)) as i32; + pre = (((pre as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; } } diff --git a/solution/2900-2999/2906.Construct Product Matrix/Solution.rs b/solution/2900-2999/2906.Construct Product Matrix/Solution.rs index ae914a6474b2b..da5ae3150d7b2 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/Solution.rs +++ b/solution/2900-2999/2906.Construct Product Matrix/Solution.rs @@ -1,27 +1,27 @@ -impl Solution { - pub fn construct_product_matrix(grid: Vec>) -> Vec> { - let modulo: i32 = 12345; - let n = grid.len(); - let m = grid[0].len(); - let mut p: Vec> = vec![vec![0; m]; n]; - let mut suf = 1; - - for i in (0..n).rev() { - for j in (0..m).rev() { - p[i][j] = suf; - suf = (suf as i64 * grid[i][j] as i64 % modulo as i64) as i32; - } - } - - let mut pre = 1; - - for i in 0..n { - for j in 0..m { - p[i][j] = (p[i][j] as i64 * pre as i64 % modulo as i64) as i32; - pre = (pre as i64 * grid[i][j] as i64 % modulo as i64) as i32; - } - } - - p - } -} \ No newline at end of file +impl Solution { + pub fn construct_product_matrix(grid: Vec>) -> Vec> { + let modulo: i32 = 12345; + let n = grid.len(); + let m = grid[0].len(); + let mut p: Vec> = vec![vec![0; m]; n]; + let mut suf = 1; + + for i in (0..n).rev() { + for j in (0..m).rev() { + p[i][j] = suf; + suf = (((suf as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; + } + } + + let mut pre = 1; + + for i in 0..n { + for j in 0..m { + p[i][j] = (((p[i][j] as i64) * (pre as i64)) % (modulo as i64)) as i32; + pre = (((pre as i64) * (grid[i][j] as i64)) % (modulo as i64)) as i32; + } + } + + p + } +} diff --git a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README.md b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README.md index f92d84030fbd2..fcefc9df6a4ca 100644 --- a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README.md +++ b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README.md @@ -842,7 +842,7 @@ impl Solution { } } - for k in (j + 1)..n { + for k in j + 1..n { if prices[j] < prices[k] { right = right.max(profits[k]); } @@ -898,8 +898,8 @@ impl Solution { let mut right = vec![0; n]; let m = prices.iter().cloned().max().unwrap_or(0); - let mut tree1 = BinaryIndexedTree::new(m as usize + 1); - let mut tree2 = BinaryIndexedTree::new(m as usize + 1); + let mut tree1 = BinaryIndexedTree::new((m as usize) + 1); + let mut tree2 = BinaryIndexedTree::new((m as usize) + 1); for i in 0..n { let x = prices[i] as usize; diff --git a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README_EN.md b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README_EN.md index 4261d2f61cd73..36255abb891d0 100644 --- a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README_EN.md +++ b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/README_EN.md @@ -832,7 +832,7 @@ impl Solution { } } - for k in (j + 1)..n { + for k in j + 1..n { if prices[j] < prices[k] { right = right.max(profits[k]); } @@ -888,8 +888,8 @@ impl Solution { let mut right = vec![0; n]; let m = prices.iter().cloned().max().unwrap_or(0); - let mut tree1 = BinaryIndexedTree::new(m as usize + 1); - let mut tree2 = BinaryIndexedTree::new(m as usize + 1); + let mut tree1 = BinaryIndexedTree::new((m as usize) + 1); + let mut tree2 = BinaryIndexedTree::new((m as usize) + 1); for i in 0..n { let x = prices[i] as usize; diff --git a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.rs b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.rs index acc525d984cd9..e5604f8978e62 100644 --- a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.rs +++ b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.rs @@ -1,29 +1,29 @@ -impl Solution { - pub fn max_profit(prices: Vec, profits: Vec) -> i32 { - let n = prices.len(); - let mut ans = -1; - - for j in 0..n { - let mut left = 0; - let mut right = 0; - - for i in 0..j { - if prices[i] < prices[j] { - left = left.max(profits[i]); - } - } - - for k in (j + 1)..n { - if prices[j] < prices[k] { - right = right.max(profits[k]); - } - } - - if left > 0 && right > 0 { - ans = ans.max(left + profits[j] + right); - } - } - - ans - } -} \ No newline at end of file +impl Solution { + pub fn max_profit(prices: Vec, profits: Vec) -> i32 { + let n = prices.len(); + let mut ans = -1; + + for j in 0..n { + let mut left = 0; + let mut right = 0; + + for i in 0..j { + if prices[i] < prices[j] { + left = left.max(profits[i]); + } + } + + for k in j + 1..n { + if prices[j] < prices[k] { + right = right.max(profits[k]); + } + } + + if left > 0 && right > 0 { + ans = ans.max(left + profits[j] + right); + } + } + + ans + } +} diff --git a/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.rs b/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.rs index 9e7475531504d..735eb3bb5ac6b 100644 --- a/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.rs +++ b/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.rs @@ -1,37 +1,37 @@ -use std::collections::HashMap; - -impl Solution { - pub fn min_groups_for_valid_assignment(nums: Vec) -> i32 { - let mut cnt: HashMap = HashMap::new(); - - for x in nums.iter() { - let count = cnt.entry(*x).or_insert(0); - *count += 1; - } - - let mut k = i32::MAX; - - for &v in cnt.values() { - k = k.min(v); - } - - for k in (1..=k).rev() { - let mut ans = 0; - - for &v in cnt.values() { - if v / k < v % k { - ans = 0; - break; - } - - ans += (v + k) / (k + 1); - } - - if ans > 0 { - return ans; - } - } - - 0 - } -} \ No newline at end of file +use std::collections::HashMap; + +impl Solution { + pub fn min_groups_for_valid_assignment(nums: Vec) -> i32 { + let mut cnt: HashMap = HashMap::new(); + + for x in nums.iter() { + let count = cnt.entry(*x).or_insert(0); + *count += 1; + } + + let mut k = i32::MAX; + + for &v in cnt.values() { + k = k.min(v); + } + + for k in (1..=k).rev() { + let mut ans = 0; + + for &v in cnt.values() { + if v / k < v % k { + ans = 0; + break; + } + + ans += (v + k) / (k + 1); + } + + if ans > 0 { + return ans; + } + } + + 0 + } +} diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md index 0d3aec1c85b81..d4687ee2c48f5 100644 --- a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md @@ -405,8 +405,8 @@ impl Solution { let mut right = vec![0; n]; let m = prices.iter().cloned().max().unwrap_or(0); - let mut tree1 = BinaryIndexedTree::new(m as usize + 1); - let mut tree2 = BinaryIndexedTree::new(m as usize + 1); + let mut tree1 = BinaryIndexedTree::new((m as usize) + 1); + let mut tree2 = BinaryIndexedTree::new((m as usize) + 1); for i in 0..n { let x = prices[i] as usize; diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md index d09ff2d936e48..eeafbab8eb82f 100644 --- a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md @@ -397,8 +397,8 @@ impl Solution { let mut right = vec![0; n]; let m = prices.iter().cloned().max().unwrap_or(0); - let mut tree1 = BinaryIndexedTree::new(m as usize + 1); - let mut tree2 = BinaryIndexedTree::new(m as usize + 1); + let mut tree1 = BinaryIndexedTree::new((m as usize) + 1); + let mut tree2 = BinaryIndexedTree::new((m as usize) + 1); for i in 0..n { let x = prices[i] as usize; diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.rs b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.rs index d1244b86e8b21..cb6ca4d4170f3 100644 --- a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.rs +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.rs @@ -1,64 +1,64 @@ -struct BinaryIndexedTree { - n: usize, - c: Vec, -} - -impl BinaryIndexedTree { - fn new(n: usize) -> BinaryIndexedTree { - BinaryIndexedTree { - n, - c: vec![0; n + 1], - } - } - - fn update(&mut self, x: usize, v: i32) { - let mut x = x; - while x <= self.n { - self.c[x] = self.c[x].max(v); - x += x & x.wrapping_neg(); - } - } - - fn query(&self, x: usize) -> i32 { - let mut x = x; - let mut mx = 0; - while x > 0 { - mx = mx.max(self.c[x]); - x -= x & x.wrapping_neg(); - } - mx - } -} - -impl Solution { - pub fn max_profit(prices: Vec, profits: Vec) -> i32 { - let n = prices.len(); - let mut left = vec![0; n]; - let mut right = vec![0; n]; - let m = prices.iter().cloned().max().unwrap_or(0); - - let mut tree1 = BinaryIndexedTree::new(m as usize + 1); - let mut tree2 = BinaryIndexedTree::new(m as usize + 1); - - for i in 0..n { - let x = prices[i] as usize; - left[i] = tree1.query(x - 1); - tree1.update(x, profits[i]); - } - - for i in (0..n).rev() { - let x = (m + 1 - prices[i]) as usize; - right[i] = tree2.query(x - 1); - tree2.update(x, profits[i]); - } - - let mut ans = -1; - for i in 0..n { - if left[i] > 0 && right[i] > 0 { - ans = ans.max(left[i] + profits[i] + right[i]); - } - } - - ans - } -} \ No newline at end of file +struct BinaryIndexedTree { + n: usize, + c: Vec, +} + +impl BinaryIndexedTree { + fn new(n: usize) -> BinaryIndexedTree { + BinaryIndexedTree { + n, + c: vec![0; n + 1], + } + } + + fn update(&mut self, x: usize, v: i32) { + let mut x = x; + while x <= self.n { + self.c[x] = self.c[x].max(v); + x += x & x.wrapping_neg(); + } + } + + fn query(&self, x: usize) -> i32 { + let mut x = x; + let mut mx = 0; + while x > 0 { + mx = mx.max(self.c[x]); + x -= x & x.wrapping_neg(); + } + mx + } +} + +impl Solution { + pub fn max_profit(prices: Vec, profits: Vec) -> i32 { + let n = prices.len(); + let mut left = vec![0; n]; + let mut right = vec![0; n]; + let m = prices.iter().cloned().max().unwrap_or(0); + + let mut tree1 = BinaryIndexedTree::new((m as usize) + 1); + let mut tree2 = BinaryIndexedTree::new((m as usize) + 1); + + for i in 0..n { + let x = prices[i] as usize; + left[i] = tree1.query(x - 1); + tree1.update(x, profits[i]); + } + + for i in (0..n).rev() { + let x = (m + 1 - prices[i]) as usize; + right[i] = tree2.query(x - 1); + tree2.update(x, profits[i]); + } + + let mut ans = -1; + for i in 0..n { + if left[i] > 0 && right[i] > 0 { + ans = ans.max(left[i] + profits[i] + right[i]); + } + } + + ans + } +}