Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e67e2f1

Browse files
add 1472
1 parent 532fba0 commit e67e2f1

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ _If you like this project, please leave me a star._ ★
276276
|1476|[Subrectangle Queries](https://leetcode.com/problems/subrectangle-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1476.java) | |Medium|Array|
277277
|1475|[Final Prices With a Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1475.java) | |Easy|Array|
278278
|1474|[Delete N Nodes After M Nodes of a Linked List](https://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1474.java) | |Easy|LinkedList|
279+
|1472|[Design Browser History](https://leetcode.com/problems/design-browser-history/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1472.java) | |Medium|Array, Design|
279280
|1471|[The k Strongest Values in an Array](https://leetcode.com/problems/the-k-strongest-values-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1471.java) | |Medium|Array, Sort|
280281
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1470.java), [C++](../master/cpp/_1470.cpp) | |Easy|Array|
281282
|1469|[Find All The Lonely Nodes](https://leetcode.com/problems/find-all-the-lonely-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1469.java) | |Easy|Tree, DFS|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1472 {
7+
public static class Solution1 {
8+
public static class BrowserHistory {
9+
10+
int curr;
11+
List<String> history;
12+
13+
public BrowserHistory(String homepage) {
14+
history = new ArrayList<>();
15+
history.add(homepage);
16+
curr = 0;
17+
}
18+
19+
public void visit(String url) {
20+
curr++;
21+
history = history.subList(0, curr);
22+
history.add(url);
23+
}
24+
25+
public String back(int steps) {
26+
curr -= steps;
27+
if (curr < 0) {
28+
curr = 0;
29+
}
30+
return history.get(curr);
31+
}
32+
33+
public String forward(int steps) {
34+
curr += steps;
35+
if (history.size() <= curr) {
36+
curr = history.size() - 1;
37+
}
38+
return history.get(curr);
39+
}
40+
}
41+
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1472;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _1472Test {
9+
private static _1472.Solution1.BrowserHistory browserHistory;
10+
11+
@Test
12+
public void test1() {
13+
browserHistory = new _1472.Solution1.BrowserHistory("leetcode.com");
14+
browserHistory.visit("google.com");
15+
browserHistory.visit("facebook.com");
16+
browserHistory.visit("youtube.com");
17+
assertEquals("facebook.com", browserHistory.back(1));
18+
assertEquals("google.com", browserHistory.back(1));
19+
assertEquals("facebook.com", browserHistory.forward(1));
20+
browserHistory.visit("linkedin.com");
21+
assertEquals("linkedin.com", browserHistory.forward(2));
22+
assertEquals("google.com", browserHistory.back(2));
23+
assertEquals("leetcode.com", browserHistory.back(7));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)