Skip to content

Commit 05e1d47

Browse files
authored
feat: add rust solution to lc problem: No.2590 (#1263)
1 parent e6fd148 commit 05e1d47

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed

solution/2500-2599/2590.Design a Todo List/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,105 @@ class TodoList {
201201
*/
202202
```
203203

204+
### **Rust**
205+
206+
```rust
207+
use std::collections::{HashMap, HashSet};
208+
209+
#[derive(Clone)]
210+
struct Task {
211+
task_id: i32,
212+
description: String,
213+
tags: HashSet<String>,
214+
due_date: i32,
215+
}
216+
217+
struct TodoList {
218+
/// The global task id
219+
id: i32,
220+
/// The mapping from `user_id` to `task`
221+
user_map: HashMap<i32, Vec<Task>>,
222+
}
223+
224+
impl TodoList {
225+
fn new() -> Self {
226+
Self {
227+
id: 1,
228+
user_map: HashMap::new(),
229+
}
230+
}
231+
232+
fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
233+
if self.user_map.contains_key(&user_id) {
234+
// Just add the task
235+
self.user_map.get_mut(&user_id).unwrap().push(Task {
236+
task_id: self.id,
237+
description: task_description,
238+
tags: tags.into_iter().collect::<HashSet<String>>(),
239+
due_date,
240+
});
241+
// Increase the global id
242+
self.id += 1;
243+
return self.id - 1;
244+
}
245+
// Otherwise, create a new user
246+
self.user_map.insert(
247+
user_id,
248+
vec![Task {
249+
task_id: self.id,
250+
description: task_description,
251+
tags: tags.into_iter().collect::<HashSet<String>>(),
252+
due_date,
253+
}],
254+
);
255+
self.id += 1;
256+
self.id - 1
257+
}
258+
259+
fn get_all_tasks(&self, user_id: i32) -> Vec<String> {
260+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
261+
return vec![];
262+
}
263+
// Get the task vector
264+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
265+
// Sort by due date
266+
ret_vec.sort_by(|lhs, rhs| {
267+
lhs.due_date.cmp(&rhs.due_date)
268+
});
269+
// Return the description vector
270+
ret_vec
271+
.into_iter()
272+
.map(|x| x.description)
273+
.collect()
274+
}
275+
276+
fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec<String> {
277+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
278+
return vec![];
279+
}
280+
// Get the task vector
281+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
282+
// Sort by due date
283+
ret_vec.sort_by(|lhs, rhs| {
284+
lhs.due_date.cmp(&rhs.due_date)
285+
});
286+
// Return the description vector
287+
ret_vec
288+
.into_iter()
289+
.filter(|x| x.tags.contains(&tag))
290+
.map(|x| x.description)
291+
.collect()
292+
}
293+
294+
fn complete_task(&mut self, user_id: i32, task_id: i32) {
295+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
296+
return;
297+
}
298+
self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id);
299+
}
300+
}
301+
```
302+
204303
### **C++**
205304

206305
```cpp

solution/2500-2599/2590.Design a Todo List/README_EN.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,105 @@ class TodoList {
191191
*/
192192
```
193193

194+
### **Rust**
195+
196+
```rust
197+
use std::collections::{HashMap, HashSet};
198+
199+
#[derive(Clone)]
200+
struct Task {
201+
task_id: i32,
202+
description: String,
203+
tags: HashSet<String>,
204+
due_date: i32,
205+
}
206+
207+
struct TodoList {
208+
/// The global task id
209+
id: i32,
210+
/// The mapping from `user_id` to `task`
211+
user_map: HashMap<i32, Vec<Task>>,
212+
}
213+
214+
impl TodoList {
215+
fn new() -> Self {
216+
Self {
217+
id: 1,
218+
user_map: HashMap::new(),
219+
}
220+
}
221+
222+
fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
223+
if self.user_map.contains_key(&user_id) {
224+
// Just add the task
225+
self.user_map.get_mut(&user_id).unwrap().push(Task {
226+
task_id: self.id,
227+
description: task_description,
228+
tags: tags.into_iter().collect::<HashSet<String>>(),
229+
due_date,
230+
});
231+
// Increase the global id
232+
self.id += 1;
233+
return self.id - 1;
234+
}
235+
// Otherwise, create a new user
236+
self.user_map.insert(
237+
user_id,
238+
vec![Task {
239+
task_id: self.id,
240+
description: task_description,
241+
tags: tags.into_iter().collect::<HashSet<String>>(),
242+
due_date,
243+
}],
244+
);
245+
self.id += 1;
246+
self.id - 1
247+
}
248+
249+
fn get_all_tasks(&self, user_id: i32) -> Vec<String> {
250+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
251+
return vec![];
252+
}
253+
// Get the task vector
254+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
255+
// Sort by due date
256+
ret_vec.sort_by(|lhs, rhs| {
257+
lhs.due_date.cmp(&rhs.due_date)
258+
});
259+
// Return the description vector
260+
ret_vec
261+
.into_iter()
262+
.map(|x| x.description)
263+
.collect()
264+
}
265+
266+
fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec<String> {
267+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
268+
return vec![];
269+
}
270+
// Get the task vector
271+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
272+
// Sort by due date
273+
ret_vec.sort_by(|lhs, rhs| {
274+
lhs.due_date.cmp(&rhs.due_date)
275+
});
276+
// Return the description vector
277+
ret_vec
278+
.into_iter()
279+
.filter(|x| x.tags.contains(&tag))
280+
.map(|x| x.description)
281+
.collect()
282+
}
283+
284+
fn complete_task(&mut self, user_id: i32, task_id: i32) {
285+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
286+
return;
287+
}
288+
self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id);
289+
}
290+
}
291+
```
292+
194293
### **C++**
195294

196295
```cpp
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
#[derive(Clone)]
4+
struct Task {
5+
task_id: i32,
6+
description: String,
7+
tags: HashSet<String>,
8+
due_date: i32,
9+
}
10+
11+
struct TodoList {
12+
/// The global task id
13+
id: i32,
14+
/// The mapping from `user_id` to `task`
15+
user_map: HashMap<i32, Vec<Task>>,
16+
}
17+
18+
impl TodoList {
19+
fn new() -> Self {
20+
Self {
21+
id: 1,
22+
user_map: HashMap::new(),
23+
}
24+
}
25+
26+
fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec<String>) -> i32 {
27+
if self.user_map.contains_key(&user_id) {
28+
// Just add the task
29+
self.user_map.get_mut(&user_id).unwrap().push(Task {
30+
task_id: self.id,
31+
description: task_description,
32+
tags: tags.into_iter().collect::<HashSet<String>>(),
33+
due_date,
34+
});
35+
// Increase the global id
36+
self.id += 1;
37+
return self.id - 1;
38+
}
39+
// Otherwise, create a new user
40+
self.user_map.insert(
41+
user_id,
42+
vec![Task {
43+
task_id: self.id,
44+
description: task_description,
45+
tags: tags.into_iter().collect::<HashSet<String>>(),
46+
due_date,
47+
}],
48+
);
49+
self.id += 1;
50+
self.id - 1
51+
}
52+
53+
fn get_all_tasks(&self, user_id: i32) -> Vec<String> {
54+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
55+
return vec![];
56+
}
57+
// Get the task vector
58+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
59+
// Sort by due date
60+
ret_vec.sort_by(|lhs, rhs| {
61+
lhs.due_date.cmp(&rhs.due_date)
62+
});
63+
// Return the description vector
64+
ret_vec
65+
.into_iter()
66+
.map(|x| x.description)
67+
.collect()
68+
}
69+
70+
fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec<String> {
71+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
72+
return vec![];
73+
}
74+
// Get the task vector
75+
let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone();
76+
// Sort by due date
77+
ret_vec.sort_by(|lhs, rhs| {
78+
lhs.due_date.cmp(&rhs.due_date)
79+
});
80+
// Return the description vector
81+
ret_vec
82+
.into_iter()
83+
.filter(|x| x.tags.contains(&tag))
84+
.map(|x| x.description)
85+
.collect()
86+
}
87+
88+
fn complete_task(&mut self, user_id: i32, task_id: i32) {
89+
if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() {
90+
return;
91+
}
92+
self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id);
93+
}
94+
}

0 commit comments

Comments
 (0)