Skip to content

Commit ed07425

Browse files
committed
Fix the inconsistency between src/lib.rs and new problem-solving files, and rustfmt everything
1 parent 341dc91 commit ed07425

File tree

227 files changed

+5041
-3950
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+5041
-3950
lines changed

src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ mod n0215_kth_largest_element_in_an_array;
186186
mod n0216_combination_sum_iii;
187187
mod n0217_contains_duplicate;
188188
mod n0218_the_skyline_problem;
189-
mod n0220_contains_duplicate_iii;
190189
mod n0219_contains_duplicate_ii;
190+
mod n0220_contains_duplicate_iii;
191191
mod n0221_maximal_square;
192-
mod n0223_rectangle_area;
193192
mod n0222_count_complete_tree_nodes;
193+
mod n0223_rectangle_area;
194194
mod n0224_basic_calculator;
195195
mod n0225_implement_stack_using_queues;
196196
mod n0226_invert_binary_tree;
@@ -227,12 +227,14 @@ mod n0300_longest_increasing_subsequence;
227227
mod n0301_remove_invalid_parentheses;
228228
mod n0303_range_sum_query_immutable;
229229
mod n0304_range_sum_query_2d_immutable;
230-
mod n1009_pancake_sorting;
231230
mod n0306_additive_number;
232231
mod n0307_range_sum_query_mutable;
233232
mod n0309_best_time_to_buy_and_sell_stock_with_cooldown;
234233
mod n0310_minimum_height_trees;
235234
mod n0312_burst_balloons;
236235
mod n0313_super_ugly_number;
237-
mod n1013_fibonacci_number;
238236
mod n0792_binary_search;
237+
mod n1009_pancake_sorting;
238+
mod n1013_fibonacci_number;
239+
mod n1071_binary_prefix_divisible_by_5;
240+
mod n1127_last_stone_weight;

src/main.rs

+36-21
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ mod problem;
77

88
use std::env;
99
use std::fs;
10-
use std::path::{Path};
11-
use std::io::Write;
1210
use std::io;
11+
use std::io::Write;
12+
use std::path::Path;
1313

1414
/// main() helps to generate the submission template .rs
1515
fn main() {
1616
println!("Welcome to leetcode-rust system.");
1717
let mut solved_ids = get_solved_ids();
1818
loop {
19-
println!("Please enter a problem id, or enter \"random\" to generate a random problem.");
19+
println!("Please enter a frontend problem id, or \"random\" to generate a random one.");
2020
let mut is_random = false;
21-
let mut id :u32 = 0;
21+
let mut id: u32 = 0;
2222
let mut id_arg = String::new();
23-
io::stdin().read_line(&mut id_arg)
23+
io::stdin()
24+
.read_line(&mut id_arg)
2425
.expect("Failed to read line");
2526
let id_arg = id_arg.trim();
2627
match id_arg {
@@ -29,23 +30,30 @@ fn main() {
2930
id = generate_random_id(&solved_ids);
3031
is_random = true;
3132
println!("Generate random problem: {}", &id);
32-
},
33+
}
3334
_ => {
34-
id = id_arg.parse::<u32>().expect(&format!("not a number: {}", id_arg));
35+
id = id_arg
36+
.parse::<u32>()
37+
.expect(&format!("not a number: {}", id_arg));
3538
if solved_ids.contains(&id) {
36-
println!("The problem you chose is invalid (the problem may have been solved \
37-
or may have no rust version).");
39+
println!(
40+
"The problem you chose is invalid (the problem may have been solved \
41+
or may have no rust version)."
42+
);
3843
continue;
3944
}
4045
}
4146
}
4247

43-
let problem = problem::get_problem(id)
44-
.expect(&format!("Error: failed to get problem #{} \
45-
(The problem may be paid-only or may not be exist).",
46-
id));
47-
let code = problem.code_definition.iter()
48-
.filter(|&d| { d.value == "rust" })
48+
let problem = problem::get_problem(id).expect(&format!(
49+
"Error: failed to get problem #{} \
50+
(The problem may be paid-only or may not be exist).",
51+
id
52+
));
53+
let code = problem
54+
.code_definition
55+
.iter()
56+
.filter(|&d| d.value == "rust")
4957
.next();
5058
if code.is_none() {
5159
println!("Problem {} has no rust version.", &id);
@@ -54,7 +62,11 @@ fn main() {
5462
}
5563
let code = code.unwrap();
5664

57-
let file_name = format!("n{:04}_{}", problem.question_id, problem.title_slug.replace("-", "_"));
65+
let file_name = format!(
66+
"n{:04}_{}",
67+
problem.question_id,
68+
problem.title_slug.replace("-", "_")
69+
);
5870
let file_path = Path::new("./src").join(format!("{}.rs", file_name));
5971
if file_path.exists() {
6072
panic!("problem already initialized");
@@ -88,17 +100,20 @@ fn main() {
88100
}
89101
}
90102

91-
fn generate_random_id(except_ids : &Vec<u32>) -> u32 {
92-
use std::fs;
103+
fn generate_random_id(except_ids: &Vec<u32>) -> u32 {
93104
use rand::Rng;
105+
use std::fs;
94106
let mut rng = rand::thread_rng();
95107
loop {
96-
let res :u32 = rng.gen_range(1, 1106);
108+
let res: u32 = rng.gen_range(1, 1106);
97109
if !except_ids.contains(&res) {
98110
return res;
99111
}
100-
println!("Generate a random num ({}), but it is invalid (the problem may have been solved \
101-
or may have no rust version). Regenerate..", res);
112+
println!(
113+
"Generate a random num ({}), but it is invalid (the problem may have been solved \
114+
or may have no rust version). Regenerate..",
115+
res
116+
);
102117
}
103118
}
104119

src/n0001_two_sum.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ impl Solution {
2626
let mut map = HashMap::with_capacity(nums.len());
2727
for (index, num) in nums.iter().enumerate() {
2828
match map.get(&(target - num)) {
29-
None => { map.insert(num, index); },
30-
Some(sub_index) => { return vec![*sub_index as i32, index as i32] },
29+
None => {
30+
map.insert(num, index);
31+
}
32+
Some(sub_index) => return vec![*sub_index as i32, index as i32],
3133
}
3234
}
3335
vec![]

src/n0002_add_two_numbers.rs

+43-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* You are given two non-empty linked lists representing two non-negative
55
* integers. The digits are stored in reverse order and each of their nodes
66
* contain a single digit. Add the two numbers and return it as a linked list.
7-
*
7+
*
88
* You may assume the two numbers do not contain any leading zero, except the
99
* number 0 itself.
10-
*
10+
*
1111
* Example:
1212
*
1313
*
@@ -17,31 +17,52 @@
1717
*
1818
*/
1919
pub struct Solution {}
20-
use super::util::linked_list::{ListNode, to_list};
20+
use super::util::linked_list::{to_list, ListNode};
2121

2222
// submission codes start here
2323

2424
impl Solution {
25-
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
25+
pub fn add_two_numbers(
26+
l1: Option<Box<ListNode>>,
27+
l2: Option<Box<ListNode>>,
28+
) -> Option<Box<ListNode>> {
2629
let (mut l1, mut l2) = (l1, l2);
2730
let mut dummy_head = Some(Box::new(ListNode::new(0)));
2831
let mut tail = &mut dummy_head;
2932
let (mut l1_end, mut l2_end, mut overflow) = (false, false, false);
3033
loop {
3134
let lhs = match l1 {
32-
Some(node) => { l1 = node.next; node.val },
33-
None => { l1_end = true; 0 },
35+
Some(node) => {
36+
l1 = node.next;
37+
node.val
38+
}
39+
None => {
40+
l1_end = true;
41+
0
42+
}
3443
};
3544
let rhs = match l2 {
36-
Some(node) => { l2 = node.next; node.val },
37-
None => { l2_end = true; 0 }
45+
Some(node) => {
46+
l2 = node.next;
47+
node.val
48+
}
49+
None => {
50+
l2_end = true;
51+
0
52+
}
3853
};
3954
// if l1, l2 end and there is not overflow from previous operation, return the result
4055
if l1_end && l2_end && !overflow {
41-
break dummy_head.unwrap().next
56+
break dummy_head.unwrap().next;
4257
}
4358
let sum = lhs + rhs + if overflow { 1 } else { 0 };
44-
let sum = if sum >= 10 { overflow = true; sum - 10 } else { overflow = false; sum };
59+
let sum = if sum >= 10 {
60+
overflow = true;
61+
sum - 10
62+
} else {
63+
overflow = false;
64+
sum
65+
};
4566
tail.as_mut().unwrap().next = Some(Box::new(ListNode::new(sum)));
4667
tail = &mut tail.as_mut().unwrap().next
4768
}
@@ -50,17 +71,25 @@ impl Solution {
5071

5172
// submission codes end
5273

53-
5474
#[cfg(test)]
5575
mod tests {
5676
use super::*;
5777

5878
#[test]
5979
fn test_2() {
60-
assert_eq!(Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])), to_list(vec![7, 0, 8]));
80+
assert_eq!(
81+
Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])),
82+
to_list(vec![7, 0, 8])
83+
);
6184

62-
assert_eq!(Solution::add_two_numbers(to_list(vec![9, 9, 9, 9]), to_list(vec![9, 9, 9, 9, 9, 9])), to_list(vec![8, 9, 9, 9, 0, 0, 1]));
85+
assert_eq!(
86+
Solution::add_two_numbers(to_list(vec![9, 9, 9, 9]), to_list(vec![9, 9, 9, 9, 9, 9])),
87+
to_list(vec![8, 9, 9, 9, 0, 0, 1])
88+
);
6389

64-
assert_eq!(Solution::add_two_numbers(to_list(vec![0]), to_list(vec![0])), to_list(vec![0]))
90+
assert_eq!(
91+
Solution::add_two_numbers(to_list(vec![0]), to_list(vec![0])),
92+
to_list(vec![0])
93+
)
6594
}
6695
}

src/n0003_longest_substring.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ impl Solution {
2424
for idx in start..end {
2525
if seq[end] == seq[idx] {
2626
start = idx + 1;
27-
break
27+
break;
2828
}
2929
}
3030
let curr = end - start + 1;
31-
if curr > max { max = curr }
31+
if curr > max {
32+
max = curr
33+
}
3234
end += 1
3335
}
3436
max as i32
@@ -43,8 +45,14 @@ mod tests {
4345

4446
#[test]
4547
fn test_3() {
46-
assert_eq!(Solution::length_of_longest_substring("abcabcbb".to_string()), 3);
48+
assert_eq!(
49+
Solution::length_of_longest_substring("abcabcbb".to_string()),
50+
3
51+
);
4752
assert_eq!(Solution::length_of_longest_substring("bbbb".to_string()), 1);
48-
assert_eq!(Solution::length_of_longest_substring("pwwkew".to_string()), 3);
53+
assert_eq!(
54+
Solution::length_of_longest_substring("pwwkew".to_string()),
55+
3
56+
);
4957
}
5058
}

src/n0004_median_of_two_sorted_arrays.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
* [4] Median of Two Sorted Arrays
33
*
44
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
5-
*
5+
*
66
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7-
*
7+
*
88
* You may assume nums1 and nums2 cannot be both empty.
9-
*
9+
*
1010
* Example 1:
11-
*
11+
*
1212
*
1313
* nums1 = [1, 3]
1414
* nums2 = [2]
15-
*
15+
*
1616
* The median is 2.0
17-
*
18-
*
17+
*
18+
*
1919
* Example 2:
20-
*
21-
*
20+
*
21+
*
2222
* nums1 = [1, 2]
2323
* nums2 = [3, 4]
24-
*
24+
*
2525
* The median is (2 + 3)/2 = 2.5
26-
*
27-
*
26+
*
27+
*
2828
*/
2929
pub struct Solution {}
3030

@@ -47,7 +47,13 @@ mod tests {
4747
#[test]
4848
#[ignore]
4949
fn test_4() {
50-
assert_eq!(Solution::find_median_sorted_arrays(vec![1, 3], vec![2]), 2.0);
51-
assert_eq!(Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]), 2.5);
50+
assert_eq!(
51+
Solution::find_median_sorted_arrays(vec![1, 3], vec![2]),
52+
2.0
53+
);
54+
assert_eq!(
55+
Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]),
56+
2.5
57+
);
5258
}
5359
}

0 commit comments

Comments
 (0)