Skip to content

Commit 1d6bee7

Browse files
committed
subdomain-visit-count
1 parent 18a1a1a commit 1d6bee7

File tree

4 files changed

+198
-30
lines changed

4 files changed

+198
-30
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
git2 = "0.13.15"
11+
reqwest = { version = "0.10", features = ["blocking", "json"] }
12+
serde_json = "1.0"
13+
serde = { version = "1.0", features = ["derive"] }

README.md

+33-30
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,49 @@
22
通过rust刷leetcode题目。
33
通过刷leetcode题目学习rust。
44

5-
### 题目
5+
当前已刷:13
66

7+
### 题目
78
- 07:传递信息
8-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/chuan-di-xin-xi.rs)
9-
- [leetcode](https://leetcode-cn.com/problems/chuan-di-xin-xi/)
9+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/chuan-di-xin-xi.rs)
10+
- [leetcode](https://leetcode-cn.com/problems/chuan-di-xin-xi/)
1011
- 09:用两个栈实现队列
11-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.rs)
12-
- [leetcode](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
12+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/yong-liang-ge-zhan-shi-xian-dui-lie-lcof.rs)
13+
- [leetcode](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
1314
- 229:求众数 II
14-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/majority-element-ii.rs)
15-
- [leetcode](https://leetcode-cn.com/problems/majority-element-ii/)
15+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/majority-element-ii.rs)
16+
- [leetcode](https://leetcode-cn.com/problems/majority-element-ii/)
1617
- 367:有效的完全平方数
17-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/valid-perfect-square.rs)
18-
- [leetcode](https://leetcode-cn.com/problems/valid-perfect-square/)
18+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/valid-perfect-square.rs)
19+
- [leetcode](https://leetcode-cn.com/problems/valid-perfect-square/)
1920
- 374:猜数字大小
20-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/guess-number-higher-or-lower.rs)
21-
- [leetcode](https://leetcode-cn.com/problems/guess-number-higher-or-lower/)
21+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/guess-number-higher-or-lower.rs)
22+
- [leetcode](https://leetcode-cn.com/problems/guess-number-higher-or-lower/)
2223
- 387:字符串中的第一个唯一字符
23-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/first-unique-character-in-a-string.rs)
24-
- [leetcode](https://leetcode-cn.com/problems/first-unique-character-in-a-string/)
25-
- 746:检查整数及其两倍数是否存在
26-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/check-if-n-and-its-double-exist.rs)
27-
- [leetcode](https://leetcode-cn.com/problems/check-if-n-and-its-double-exist/)
24+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/first-unique-character-in-a-string.rs)
25+
- [leetcode](https://leetcode-cn.com/problems/first-unique-character-in-a-string/)
26+
- 746:检查整数及其两倍数是否存在
27+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/check-if-n-and-its-double-exist.rs)
28+
- [leetcode](https://leetcode-cn.com/problems/check-if-n-and-its-double-exist/)
29+
- 811:子域名访问计数
30+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/subdomain-visit-count.rs)
31+
- [leetcode](https://leetcode-cn.com/problems/subdomain-visit-count/)
2832
- 965:单值二叉树
29-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/univalued-binary-tree.rs)
30-
- [leetcode](https://leetcode-cn.com/problems/univalued-binary-tree/)
33+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/univalued-binary-tree.rs)
34+
- [leetcode](https://leetcode-cn.com/problems/univalued-binary-tree/)
3135
- 1008:前序遍历构造二叉搜索树
32-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/construct-binary-search-tree-from-preorder-traversal.rs)
33-
- [leetcode](https://leetcode-cn.com/problems/construct-binary-search-tree-from-preorder-traversal/)
36+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/construct-binary-search-tree-from-preorder-traversal.rs)
37+
- [leetcode](https://leetcode-cn.com/problems/construct-binary-search-tree-from-preorder-traversal/)
3438
- 1309:解码字母到整数映射
35-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/decrypt-string-from-alphabet-to-integer-mapping.rs)
36-
- [leetcode](https://leetcode-cn.com/problems/decrypt-string-from-alphabet-to-integer-mapping/)
39+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/decrypt-string-from-alphabet-to-integer-mapping.rs)
40+
- [leetcode](https://leetcode-cn.com/problems/decrypt-string-from-alphabet-to-integer-mapping/)
3741
- 1432:可获得的最大点数
38-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
39-
- [leetcode](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/)
42+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
43+
- [leetcode](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/)
4044
- 1480:一维数组的动态和
41-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
42-
- [leetcode](https://leetcode-cn.com/problems/running-sum-of-1d-array/)
43-
- 1678:设计 Goal 解析器
44-
45-
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/goal-parser-interpretation.rs)
46-
- [leetcode](https://leetcode-cn.com/problems/goal-parser-interpretation/)
45+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/maximum-points-you-can-obtain-from-cards.rs)
46+
- [leetcode](https://leetcode-cn.com/problems/running-sum-of-1d-array/)
47+
- 1678:设计 Goal 解析器
48+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/goal-parser-interpretation.rs)
49+
- [leetcode](https://leetcode-cn.com/problems/goal-parser-interpretation/)
4750

src/bin/subdomain-visit-count.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn main() {
2+
println!("{:?}", Solution::subdomain_visits(
3+
vec!["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"].
4+
into_iter().
5+
map(|x| x.to_string()).collect()
6+
));
7+
}
8+
9+
struct Solution;
10+
11+
impl Solution {
12+
pub fn subdomain_visits(cpdomains: Vec<String>) -> Vec<String> {
13+
let mut m = std::collections::HashMap::new();
14+
15+
for i in cpdomains.iter() {
16+
let split = i.split(" ").collect::<Vec<&str>>();
17+
let num = split[0].parse::<i32>().unwrap();
18+
let url = split[1];
19+
20+
m.entry(String::from(url)).and_modify(|x| *x += num).or_insert(num);
21+
22+
for (index, v) in url.as_bytes().iter().enumerate() {
23+
if *v == '.' as u8 {
24+
m.entry(url[index+1..].to_string()).and_modify(|x| *x += num).or_insert(num);
25+
}
26+
}
27+
}
28+
29+
m.iter().map(|(x, y)| format!("{} {}", y, x)).collect()
30+
}
31+
}

src/lib.rs

+130
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,137 @@
11
#[cfg(test)]
22
mod tests {
3+
use crate::{get_new_file_in_bin, get_question_msg, get_question_num};
4+
use super::*;
5+
36
#[test]
47
fn it_works() {
58
assert_eq!(2 + 2, 4);
69
}
10+
11+
#[test]
12+
fn test_get_new_file_in_bin() {
13+
println!("{:?}", get_new_file_in_bin());
14+
}
15+
16+
#[test]
17+
fn test_get_question_num() {
18+
assert_eq!(13usize, get_question_num());
19+
}
20+
21+
#[test]
22+
fn test_write_to_readme() {
23+
let resp = get_question_msg("maximum-points-you-can-obtain-from-cards");
24+
write_to_readme(resp);
25+
}
26+
}
27+
28+
extern crate reqwest;
29+
30+
use git2::{Repository, StatusOptions};
31+
use serde::Deserialize;
32+
use std::fs::{self, File, ReadDir};
33+
use std::ops::Deref;
34+
use std::io::Write;
35+
36+
37+
/// 获取bin目录下新加的文件
38+
pub fn get_new_file_in_bin() -> Vec<String> {
39+
let mut options = StatusOptions::new();
40+
options.include_untracked(true);
41+
let repo = Repository::open(".").unwrap();
42+
let statuses = repo.statuses(Some(&mut options)).unwrap();
43+
44+
statuses.iter().
45+
filter(|x| { x.path().unwrap().starts_with("src/bin/") }).
46+
map(|x| String::from(x.path().unwrap())).
47+
map(|x| {
48+
let x = x.trim_end_matches(".rs"); // 去掉路径
49+
let x = x.trim_start_matches("src/bin/"); // 去掉后缀
50+
x.to_string()
51+
}).
52+
collect()
53+
}
54+
55+
#[derive(Deserialize, Debug)]
56+
pub struct Ques {
57+
#[serde(rename = "questionId")]
58+
question_id: String,
59+
#[serde(rename = "titleSlug")]
60+
title_slug: String,
61+
#[serde(rename = "translatedTitle")]
62+
translated_title: String,
63+
}
64+
65+
#[derive(Deserialize, Debug)]
66+
pub struct Data {
67+
question: Ques,
68+
}
69+
70+
#[derive(Deserialize, Debug)]
71+
pub struct Resp {
72+
data: Data,
73+
}
74+
75+
/// 通过名字获取题目的ID
76+
pub fn get_question_msg(name: &str) -> Resp {
77+
let url = "https://leetcode-cn.com/graphql/";
78+
let data_fmt = r#"{"operationName":"questionData","variables":{"titleSlug":"{}"},"query":"query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n source\n shortDescription\n fullDescription\n bookImgUrl\n pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n __typename\n }\n}\n"}"#;
79+
let data = data_fmt.replace("{}", name);
80+
let res = reqwest::blocking::Client::new().
81+
post(url).
82+
header("content-type", "application/json").
83+
body(data).
84+
send().
85+
unwrap().
86+
json::<Resp>().unwrap();
87+
88+
res
89+
}
90+
91+
/// 把问题写到README.md中
92+
pub fn write_to_readme(question_info: Resp) {
93+
let readme = fs::read_to_string("README.md").unwrap();
94+
let mut write_string = String::new();
95+
write_string.push_str("# leetcode
96+
通过rust刷leetcode题目。
97+
通过刷leetcode题目学习rust。\n\n");
98+
write_string.push_str(format!("当前已刷:{}\n\n", get_question_num()).as_str());
99+
write_string.push_str("### 题目");
100+
101+
let mut f = File::create("1.md").unwrap();
102+
f.write(readme.as_bytes());
103+
104+
let mut index = 0usize;
105+
let split = readme.split("\n").into_iter().collect::<Vec<&str>>();
106+
let mut flag = false;
107+
108+
loop {
109+
if !flag {
110+
if split[index] == "### 题目" {
111+
flag = true;
112+
}
113+
index += 1;
114+
continue;
115+
}
116+
117+
118+
119+
120+
index += 3;
121+
}
122+
}
123+
124+
/// 获取题目数
125+
fn get_question_num() -> usize {
126+
let dir = fs::read_dir("src/bin/").unwrap();
127+
128+
dir.
129+
into_iter().
130+
filter(|x| {
131+
if let Ok(f) = x {
132+
f.file_name().to_str().unwrap().ends_with(".rs")
133+
} else {
134+
false
135+
}
136+
}).count()
7137
}

0 commit comments

Comments
 (0)