-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1472-design-browser-history.rs
56 lines (50 loc) · 1.45 KB
/
1472-design-browser-history.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
struct BrowserHistory {
his: Vec::<String>,
idx: usize,
}
impl BrowserHistory {
fn new(homepage: String) -> Self {
BrowserHistory {
his: vec![homepage.clone()],
idx: 0
}
}
fn visit(&mut self, url: String) {
self.his.truncate(self.idx + 1);
self.his.push(url.clone());
self.idx += 1;
}
fn back(&mut self, steps: i32) -> String {
let steps = steps as usize;
if self.idx < steps {
self.idx = 0;
} else {
self.idx -= steps;
}
// println!("{:?} {:?}", self.his, self.idx);
self.his[self.idx].clone()
}
fn forward(&mut self, steps: i32) -> String {
let steps = steps as usize;
if self.idx + steps < self.his.len() {
self.idx += steps;
} else {
self.idx = self.his.len() - 1;
}
// println!("{:?} {:?}", self.his, self.idx);
self.his[self.idx].clone()
}
}
fn main() {
let mut obj = BrowserHistory::new("leetcode.com".to_string());
obj.visit("google.com".to_string());
obj.visit("facebook.com".to_string());
obj.visit("youtube.com".to_string());
println!("{:?}", obj.back(1));
println!("{:?}", obj.back(1));
println!("{:?}", obj.forward(1));
obj.visit("linkedin.com".to_string());
println!("{:?}", obj.forward(2)); // linkedin
println!("{:?}", obj.back(2));
println!("{:?}", obj.back(7));
}