Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7cd46a7

Browse files
authored
Merge pull request #19 from songyzh/return-type
增加问题的默认返回值, 方便书写代码
2 parents 3373054 + 8a06a59 commit 7cd46a7

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

‎Cargo.lock

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 1 addition & 0 deletions
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

‎src/main.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate serde_json;
55

66
mod problem;
77

8+
use regex::Regex;
89
use std::env;
910
use std::fs;
1011
use std::io;
@@ -74,7 +75,10 @@ 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(
79+
"__PROBLEM_DEFAULT_CODE__",
80+
&insert_return_in_code(&problem.return_type, &code.default_code),
81+
)
7882
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
7983
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
8084

@@ -147,6 +151,48 @@ fn parse_extra_use(code: &str) -> String {
147151
extra_use_line
148152
}
149153

154+
fn insert_return_in_code(return_type: &str, code: &str) -> String {
155+
let re = Regex::new(r"\{[ \n]+}").unwrap();
156+
match return_type {
157+
"ListNode" => re
158+
.replace(&code, "{\n Some(Box::new(ListNode::new(0)))\n }")
159+
.to_string(),
160+
"ListNode[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
161+
"TreeNode" => re
162+
.replace(
163+
&code,
164+
"{\n Some(Rc::new(RefCell::new(TreeNode::new(0))))\n }",
165+
)
166+
.to_string(),
167+
"boolean" => re.replace(&code, "{\n false\n }").to_string(),
168+
"character" => re.replace(&code, "{\n '0'\n }").to_string(),
169+
"character[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
170+
"double" => re.replace(&code, "{\n 0f64\n }").to_string(),
171+
"double[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
172+
"int[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
173+
"integer" => re.replace(&code, "{\n 0\n }").to_string(),
174+
"integer[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
175+
"integer[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
176+
"list<String>" => re.replace(&code, "{\n vec![]\n }").to_string(),
177+
"list<TreeNode>" => re.replace(&code, "{\n vec![]\n }").to_string(),
178+
"list<boolean>" => re.replace(&code, "{\n vec![]\n }").to_string(),
179+
"list<double>" => re.replace(&code, "{\n vec![]\n }").to_string(),
180+
"list<integer>" => re.replace(&code, "{\n vec![]\n }").to_string(),
181+
"list<list<integer>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
182+
"list<list<string>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
183+
"list<string>" => re.replace(&code, "{\n vec![]\n }").to_string(),
184+
"null" => code.to_string(),
185+
"string" => re
186+
.replace(&code, "{\n String::new()\n }")
187+
.to_string(),
188+
"string[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
189+
"void" => code.to_string(),
190+
"NestedInteger" => code.to_string(),
191+
"Node" => code.to_string(),
192+
_ => code.to_string(),
193+
}
194+
}
195+
150196
fn build_desc(content: &str) -> String {
151197
// TODO: fix this shit
152198
content

‎src/problem.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate reqwest;
22
extern crate serde_json;
33

4+
use serde_json::Value;
45
use std::fmt::{Display, Error, Formatter};
56

67
const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/";
@@ -43,6 +44,10 @@ pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
4344
sample_test_case: resp.data.question.sample_test_case,
4445
difficulty: problem.difficulty.to_string(),
4546
question_id: problem.stat.frontend_question_id,
47+
return_type: {
48+
let v: Value = serde_json::from_str(&resp.data.question.meta_data).unwrap();
49+
v["return"]["type"].to_string().replace("\"", "")
50+
},
4651
});
4752
}
4853
}
@@ -64,6 +69,7 @@ pub struct Problem {
6469
pub sample_test_case: String,
6570
pub difficulty: String,
6671
pub question_id: u32,
72+
pub return_type: String,
6773
}
6874

6975
#[derive(Serialize, Deserialize)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /