Skip to content

Commit 70467d4

Browse files
committed
对于返回类型为ListNode的问题, 插入默认答案
1 parent cbfeffb commit 70467d4

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

Diff for: Cargo.lock

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ serde = "1.0"
1010
serde_json = "1.0"
1111
serde_derive = "1.0"
1212
rand = "0.6.5"
13+
regex = "1.3.4"
1314

1415
[lib]
1516
doctest = false

Diff for: src/main.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::fs;
1010
use std::io;
1111
use std::io::Write;
1212
use std::path::Path;
13+
use regex::Regex;
1314

1415
/// main() helps to generate the submission template .rs
1516
fn main() {
@@ -74,7 +75,7 @@ fn main() {
7475
let source = template
7576
.replace("__PROBLEM_TITLE__", &problem.title)
7677
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
77-
.replace("__PROBLEM_DEFAULT_CODE__", &code.default_code)
78+
.replace("__PROBLEM_DEFAULT_CODE__", &insert_return_in_code(&problem.return_type, &code.default_code))
7879
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
7980
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
8081

@@ -147,6 +148,16 @@ fn parse_extra_use(code: &str) -> String {
147148
extra_use_line
148149
}
149150

151+
fn insert_return_in_code(return_type: &str, code: &str) -> String {
152+
match return_type {
153+
"ListNode" => {
154+
let re = Regex::new(r"\{\n +\n +}").unwrap();
155+
re.replace(code, format!("{{\n{:8}Some(Box::new(ListNode::new(0)))\n{:4}}}")).to_string()
156+
},
157+
_ => code
158+
}
159+
}
160+
150161
fn build_desc(content: &str) -> String {
151162
// TODO: fix this shit
152163
content

0 commit comments

Comments
 (0)