Skip to content

Commit 5743507

Browse files
committed
add new question rust file
1 parent 564afa5 commit 5743507

File tree

5 files changed

+62
-81
lines changed

5 files changed

+62
-81
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ git2 = "0.13.15"
1111
reqwest = { version = "0.10", features = ["blocking", "json"] }
1212
serde_json = "1.0"
1313
serde = { version = "1.0", features = ["derive"] }
14+
15+
[[bin]]
16+
name ="leetcode"
17+
path ="src/main.rs"

README.md

-79
Original file line numberDiff line numberDiff line change
@@ -1,79 +0,0 @@
1-
# leetcode
2-
通过rust刷leetcode题目。
3-
通过刷leetcode题目学习rust。
4-
5-
当前已刷:23
6-
7-
### 题目
8-
- 6:Z 字形变换
9-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/zigzag-conversion.rs)
10-
- [leetcode](https://leetcode-cn.com/problems/zigzag-conversion/)
11-
- 07:传递信息
12-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/chuan-di-xin-xi.rs)
13-
- [leetcode](https://leetcode-cn.com/problems/chuan-di-xin-xi/)
14-
- 09:用两个栈实现队列
15-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.rs)
16-
- [leetcode](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
17-
- 43:字符串相乘
18-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/multiply-strings.rs)
19-
- [leetcode](https://leetcode-cn.com/problems/multiply-strings/)
20-
- 136:只出现一次的数字
21-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/single-number.rs)
22-
- [leetcode](https://leetcode-cn.com/problems/single-number/)
23-
- 137:只出现一次的数字 II
24-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/single-number-ii.rs)
25-
- [leetcode](https://leetcode-cn.com/problems/single-number-ii/)
26-
- 229:求众数 II
27-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/majority-element-ii.rs)
28-
- [leetcode](https://leetcode-cn.com/problems/majority-element-ii/)
29-
- 367:有效的完全平方数
30-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/valid-perfect-square.rs)
31-
- [leetcode](https://leetcode-cn.com/problems/valid-perfect-square/)
32-
- 371:两整数之和
33-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/sum-of-two-integers.rs)
34-
- [leetcode](https://leetcode-cn.com/problems/sum-of-two-integers/)
35-
- 374:猜数字大小
36-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/guess-number-higher-or-lower.rs)
37-
- [leetcode](https://leetcode-cn.com/problems/guess-number-higher-or-lower/)
38-
- 387:字符串中的第一个唯一字符
39-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/first-unique-character-in-a-string.rs)
40-
- [leetcode](https://leetcode-cn.com/problems/first-unique-character-in-a-string/)
41-
- 674:最长连续递增序列
42-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/longest-continuous-increasing-subsequence.rs)
43-
- [leetcode](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/)
44-
- 746:检查整数及其两倍数是否存在
45-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/check-if-n-and-its-double-exist.rs)
46-
- [leetcode](https://leetcode-cn.com/problems/check-if-n-and-its-double-exist/)
47-
- 811:子域名访问计数
48-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/subdomain-visit-count.rs)
49-
- [leetcode](https://leetcode-cn.com/problems/subdomain-visit-count/)
50-
- 857:较大分组的位置
51-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/positions-of-large-groups.rs)
52-
- [leetcode](https://leetcode-cn.com/problems/positions-of-large-groups/)
53-
- 965:单值二叉树
54-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/univalued-binary-tree.rs)
55-
- [leetcode](https://leetcode-cn.com/problems/univalued-binary-tree/)
56-
- 1008:前序遍历构造二叉搜索树
57-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/construct-binary-search-tree-from-preorder-traversal.rs)
58-
- [leetcode](https://leetcode-cn.com/problems/construct-binary-search-tree-from-preorder-traversal/)
59-
- 1309:解码字母到整数映射
60-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/decrypt-string-from-alphabet-to-integer-mapping.rs)
61-
- [leetcode](https://leetcode-cn.com/problems/decrypt-string-from-alphabet-to-integer-mapping/)
62-
- 1432:可获得的最大点数
63-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
64-
- [leetcode](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/)
65-
- 1480:一维数组的动态和
66-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
67-
- [leetcode](https://leetcode-cn.com/problems/running-sum-of-1d-array/)
68-
- 1538:可获得的最大点数
69-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
70-
- [leetcode](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/)
71-
- 1552:用栈操作构建数组
72-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/build-an-array-with-stack-operations.rs)
73-
- [leetcode](https://leetcode-cn.com/problems/build-an-array-with-stack-operations/)
74-
- 1566:检查单词是否为句中其他单词的前缀
75-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.rs)
76-
- [leetcode](https://leetcode-cn.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)
77-
- 1678:设计 Goal 解析器
78-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/goal-parser-interpretation.rs)
79-
- [leetcode](https://leetcode-cn.com/problems/goal-parser-interpretation/)

src/bin/maximum-points-you-can-obtain-from-cards.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Solution {
1010
/// 如果从头开始的话,区间的和为card_points[..k]的和sum1,区间向后移动一位,区间sum1 = sum1+card_points[k]-card_points[0]
1111
pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
1212
// 剩余数组的长度
13-
let other = (card_points.len() - k as usize);
13+
let other = card_points.len() - k as usize;
1414
// 假设剩余数组从0下标开始,和为sum1
1515
let mut sum1: i32 = card_points[0..other].iter().sum();
1616
let mut sum = sum1;

src/lib.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ mod tests {
2929
assert_eq!(374, get_question_no("- 374:猜数字大小"));
3030
assert_eq!(367, get_question_no("- 367:有效的完全平方数"));
3131
}
32+
33+
#[test]
34+
fn test_get_question_msg() {
35+
println!("{:?}", get_question_msg("maximum-points-you-can-obtain-from-cards"));
36+
}
37+
38+
#[test]
39+
fn test_make_new_file() {
40+
make_new_file(get_question_msg("shu-de-zi-jie-gou-lcof"));
41+
}
3242
}
3343

3444
extern crate reqwest;
@@ -66,6 +76,20 @@ pub struct Ques {
6676
title_slug: String,
6777
#[serde(rename = "translatedTitle")]
6878
translated_title: String,
79+
#[serde(rename = "codeSnippets")]
80+
code_snippets: Vec<CodeSnippets>,
81+
}
82+
83+
#[derive(Deserialize, Debug)]
84+
pub struct CodeSnippets {
85+
#[serde(rename = "code")]
86+
code: String,
87+
#[serde(rename = "lang")]
88+
lang: String,
89+
#[serde(rename = "langSlug")]
90+
lang_slug: String,
91+
#[serde(rename = "__typename")]
92+
typename: String,
6993
}
7094

7195
#[derive(Deserialize, Debug)]
@@ -190,4 +214,20 @@ fn add_and_commit(file: &str) {
190214

191215
fn push_to_origin() {
192216
Command::new("git").arg("push").output().unwrap();
193-
}
217+
}
218+
219+
/// 通过题目名获取rust答题模板
220+
pub fn make_new_file(resp: Resp) {
221+
let mut f = File::create(format!("src/bin/{}.rs", resp.data.question.title_slug)).unwrap();
222+
let mut s = String::new();
223+
s.push_str("fn main() {}\n\n");
224+
225+
for i in resp.data.question.code_snippets {
226+
if i.lang == "Rust" {
227+
s.push_str(i.code.replace("↵", "\n").as_str())
228+
}
229+
}
230+
s.push_str("\n");
231+
232+
f.write(s.as_bytes()).unwrap();
233+
}

src/main.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
use leetcode;
33

44
fn main() {
5+
let args = std::env::args().collect::<Vec<String>>();
6+
if args.len() == 1 {
7+
git();
8+
} else if args.len() == 2 {
9+
new(args[1].as_str());
10+
} else {
11+
panic!("只能有一个参数");
12+
}
13+
}
14+
15+
fn git() {
516
// 获取新加的文件
617
let files = leetcode::get_new_file_in_bin();
718

@@ -12,3 +23,8 @@ fn main() {
1223

1324
leetcode::git_add_commit_files(files);
1425
}
26+
27+
fn new(filename: &str) {
28+
let resp = leetcode::get_question_msg(filename);
29+
leetcode::make_new_file(resp);
30+
}

0 commit comments

Comments
 (0)