-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDesignBrowserHistory.java
45 lines (37 loc) · 1.1 KB
/
DesignBrowserHistory.java
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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/design-browser-history/
public class DesignBrowserHistory {
private final List<String> history = new ArrayList<>();
private int current = 0;
public DesignBrowserHistory(String homepage) {
history.add(homepage);
}
public void visit(String url) {
if (current < history.size() - 1) {
int length = history.size() - 1;
for (int i = current; i < length; i++) {
history.remove(history.size() - 1);
}
}
history.add(url);
current++;
}
public String back(int steps) {
if (current < steps) {
current = 0;
} else {
current = current - steps;
}
return history.get(current);
}
public String forward(int steps) {
if (current + steps >= history.size()) {
current = history.size() - 1;
} else {
current = current + steps;
}
return history.get(current);
}
}