Skip to content

Commit d660892

Browse files
authored
Merge pull request #287 from ben1009/tool-chain
just have fun, no reason
2 parents 703d214 + 0235815 commit d660892

File tree

60 files changed

+448
-422
lines changed

Some content is hidden

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

60 files changed

+448
-422
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ concurrency:
1212

1313
env:
1414
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
15-
RUST_TOOLCHAIN: nightly-2025-02-01
15+
RUST_TOOLCHAIN: nightly-2025-07-01
1616

1717
name: Check
1818
jobs:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ concurrency:
1212

1313
env:
1414
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
15-
RUST_TOOLCHAIN: nightly-2025-02-01
15+
RUST_TOOLCHAIN: nightly-2025-07-01
1616

1717
name: Test
1818
jobs:

Makefile.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ min_version = "0.36.10"
1313
[tasks.install-nextest]
1414
category = "Misc"
1515
description = "install cargo-nextest"
16-
install_crate = { crate_name = "cargo-nextest", version = "0.9.87", binary = "cargo", test_arg = [
16+
install_crate = { crate_name = "cargo-nextest", version = "0.9.96", binary = "cargo", test_arg = [
1717
"nextest",
1818
"--help",
1919
] }
@@ -50,7 +50,7 @@ args = ["fmt", "--all"]
5050
[tasks.check-typos]
5151
category = "Dev - check"
5252
description = "Run cargo typos-cli check"
53-
install_crate = { version = "1.28.4", crate_name = "typos-cli", binary = "typos", test_arg = [
53+
install_crate = { version = "1.32.0", crate_name = "typos-cli", binary = "typos", test_arg = [
5454
"--help",
5555
] }
5656
script = """
@@ -80,7 +80,7 @@ cargo hakari verify > /dev/null
8080
[tasks.check-machete]
8181
category = "Dev - check"
8282
description = "Run cargo machete check"
83-
install_crate = { version = "0.7.0", crate_name = "cargo-machete", binary = "cargo", test_arg = [
83+
install_crate = { version = "0.8.0", crate_name = "cargo-machete", binary = "cargo", test_arg = [
8484
"machete",
8585
"--help",
8686
] }

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "nightly-2025-02-01"
2+
channel = "nightly-2025-07-01"

src/main.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn main() {
120120
} else {
121121
_id = id_arg
122122
.parse::<u32>()
123-
.unwrap_or_else(|_| panic!("not a number: {}", id_arg));
123+
.unwrap_or_else(|_| panic!("not a number: {id_arg}"));
124124
if initialized_ids.contains(&_id) {
125125
println!("The problem you chose has been initialized in problem/");
126126
continue;
@@ -129,9 +129,8 @@ fn main() {
129129

130130
let problem = fetcher::get_problem(_id).unwrap_or_else(|| {
131131
panic!(
132-
"Error: failed to get problem #{} \
133-
(The problem may be paid-only or may not be exist).",
134-
_id
132+
"Error: failed to get problem #{_id} \
133+
(The problem may be paid-only or may not be exist)."
135134
)
136135
});
137136
let code = problem.code_definition.iter().find(|&d| d.value == *"rust");
@@ -157,9 +156,8 @@ fn generate_random_id(except_ids: &[u32]) -> u32 {
157156
return res;
158157
}
159158
println!(
160-
"Generate a random num ({}), but it is invalid (the problem may have been solved \
161-
or may have no rust version). Regenerate..",
162-
res
159+
"Generate a random num ({res}), but it is invalid (the problem may have been solved \
160+
or may have no rust version). Regenerate.."
163161
);
164162
}
165163
}
@@ -291,7 +289,7 @@ fn deal_solving(id: &u32) {
291289
problem.question_id,
292290
problem.title_slug.replace('-', "_")
293291
);
294-
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
292+
let file_path = Path::new("./src/problem").join(format!("{file_name}.rs"));
295293
// check problem/ existence
296294
if !file_path.exists() {
297295
panic!("problem does not exist");
@@ -302,15 +300,15 @@ fn deal_solving(id: &u32) {
302300
problem.question_id,
303301
problem.title_slug.replace('-', "_")
304302
);
305-
let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name));
303+
let solution_path = Path::new("./src/solution").join(format!("{solution_name}.rs"));
306304
if solution_path.exists() {
307305
panic!("solution exists");
308306
}
309307
// rename/move file
310308
fs::rename(file_path, solution_path).unwrap();
311309
// remove from problem/mod.rs
312310
let mod_file = "./src/problem/mod.rs";
313-
let target_line = format!("mod {};", file_name);
311+
let target_line = format!("mod {file_name};");
314312
let lines: Vec<String> = io::BufReader::new(File::open(mod_file).unwrap())
315313
.lines()
316314
.map(|x| x.unwrap())
@@ -322,7 +320,7 @@ fn deal_solving(id: &u32) {
322320
.append(true)
323321
.open("./src/solution/mod.rs")
324322
.unwrap();
325-
let _ = writeln!(lib_file, "mod {};", solution_name);
323+
let _ = writeln!(lib_file, "mod {solution_name};");
326324
}
327325

328326
fn deal_problem(problem: &Problem, code: &CodeDefinition, write_mod_file: bool) {
@@ -331,8 +329,7 @@ fn deal_problem(problem: &Problem, code: &CodeDefinition, write_mod_file: bool)
331329
problem.question_id,
332330
problem.title_slug.replace('-', "_")
333331
);
334-
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
335-
332+
let file_path = Path::new("./src/problem").join(format!("{file_name}.rs"));
336333
let template = fs::read_to_string("./template.rs").unwrap();
337334
let source = template
338335
.replace("__PROBLEM_TITLE__", &problem.title)
@@ -361,7 +358,7 @@ fn deal_problem(problem: &Problem, code: &CodeDefinition, write_mod_file: bool)
361358
.append(true)
362359
.open("./src/problem/mod.rs")
363360
.unwrap();
364-
let _ = writeln!(lib_file, "pub mod {};", file_name);
361+
let _ = writeln!(lib_file, "pub mod {file_name};");
365362
}
366363
}
367364

@@ -378,9 +375,9 @@ mod test {
378375
// pub mod p0001_two_sum;
379376
pub mod p0002_add_two_numbers;
380377
pub mod p0003_longest_substring_without_repeating_characters;"#;
381-
writeln!(file, "{}", content).unwrap();
378+
writeln!(file, "{content}").unwrap();
382379
let ids = get_initialized_ids(path.to_str().unwrap());
383-
println!("{:?}", ids);
380+
println!("{ids:?}");
384381
assert!(ids.len() == 2);
385382
assert!(ids[0] == 2);
386383
assert!(ids[1] == 3);

src/problem/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub mod p1016_binary_string_with_substrings_representing_1_to_n;
6262
pub mod p1054_distant_barcodes;
6363
pub mod p1186_maximum_subarray_sum_with_one_deletion;
6464
pub mod p1376_time_needed_to_inform_all_employees;
65+
pub mod p1608_special_array;
6566
pub mod p2437_number_of_valid_clock_times;
6667
pub mod p2671_frequency_tracker;
6768
pub mod p2708_maximum_strength_of_a_group;

src/problem/p0014_longest_common_prefix.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::cmp;
2-
31
/// [14] Longest Common Prefix
42
///
53
/// Write a function to find the longest common prefix string amongst an array of strings.
@@ -32,23 +30,22 @@ pub struct Solution {}
3230
impl Solution {
3331
pub fn longest_common_prefix(strs: Vec<String>) -> String {
3432
if strs.len() == 1 {
35-
return strs[0].clone();
33+
return strs[0].to_string();
3634
}
3735

38-
let mut ret = strs[0].as_bytes();
36+
let mut ret = strs[0].to_string();
3937
for str in strs.iter().skip(1) {
40-
let t = str.as_bytes();
4138
let mut j = 0;
42-
while j < cmp::min(ret.len(), t.len()) {
43-
if ret[j] != t[j] {
39+
while j < std::cmp::min(ret.len(), str.len()) {
40+
if ret.as_bytes()[j] != str.as_bytes()[j] {
4441
break;
4542
}
4643
j += 1;
4744
}
48-
ret = &ret[0..j];
45+
ret = ret[..j].to_string();
4946
}
5047

51-
String::from_utf8_lossy(ret).to_string()
48+
ret
5249
}
5350
}
5451

src/problem/p0017_letter_combinations_of_a_phone_number.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Solution {}
3636
impl Solution {
3737
pub fn letter_combinations(digits: String) -> Vec<String> {
3838
if digits.is_empty() {
39-
return Vec::new();
39+
return vec![];
4040
}
4141

4242
let dic = HashMap::from([
@@ -51,26 +51,28 @@ impl Solution {
5151
]);
5252

5353
let mut ret = vec![];
54-
Self::dfs(digits.as_bytes(), 0, &dic, &mut vec![], &mut ret);
54+
Self::dfs(digits.as_bytes(), &dic, 0, &mut vec![], &mut ret);
5555

5656
ret
5757
}
5858

5959
fn dfs(
6060
digits: &[u8],
61-
idx: usize,
6261
dic: &HashMap<u8, &str>,
62+
idx: usize,
6363
tmp: &mut Vec<u8>,
6464
ret: &mut Vec<String>,
6565
) {
6666
if tmp.len() == digits.len() {
67-
ret.push(String::from_utf8(tmp.clone()).unwrap());
67+
ret.push(String::from_utf8_lossy(tmp).to_string());
68+
6869
return;
6970
}
7071

71-
for c in dic[&digits[idx]].as_bytes() {
72-
tmp.push(*c);
73-
Self::dfs(digits, idx + 1, dic, tmp, ret);
72+
let s = dic[&digits[idx]].as_bytes();
73+
for item in s {
74+
tmp.push(*item);
75+
Solution::dfs(digits, dic, idx + 1, tmp, ret);
7476
tmp.pop();
7577
}
7678
}

src/problem/p0019_remove_nth_node_from_end_of_list.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ use crate::util::linked_list::ListNode;
5454
// }
5555
impl Solution {
5656
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
57-
let mut root_pointer = ListNode { val: 0, next: head };
57+
let mut root_pointer = ListNode::new(0);
58+
root_pointer.next = head.clone();
5859
let mut pre = &mut root_pointer;
59-
let mut current = &pre.clone();
60-
for _i in 0..n {
61-
current = current.next.as_ref().unwrap();
62-
}
60+
let mut head = head.as_ref().unwrap();
6361

64-
while let Some(ref n) = current.next {
65-
current = n;
62+
for _ in 1..n {
63+
head = head.next.as_ref().unwrap();
64+
}
65+
while let Some(n) = head.next.as_ref() {
6666
pre = pre.next.as_mut().unwrap();
67+
head = n;
6768
}
6869
pre.next = pre.next.as_mut().unwrap().next.take();
6970

src/problem/p0020_valid_parentheses.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,16 @@ impl Solution {
4343
return false;
4444
}
4545

46+
let dic = HashMap::from([(')', '('), (']', '['), ('}', '{')]);
4647
let mut stack = vec![];
47-
let s = s.as_bytes();
48-
let map = HashMap::from([(b'}', b'{'), (b')', b'('), (b']', b'[')]);
49-
for v in s {
50-
if let Some(t) = stack.last() {
51-
if let Some(p) = map.get(v) {
52-
if p == t {
53-
stack.pop();
54-
continue;
55-
}
48+
for c in s.chars() {
49+
if let t @ Some(_) = stack.last() {
50+
if t == dic.get(&c) {
51+
stack.pop();
5652
}
53+
} else {
54+
stack.push(c);
5755
}
58-
59-
stack.push(*v);
6056
}
6157

6258
stack.is_empty()

0 commit comments

Comments
 (0)