일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- hashset
- 부분배열
- ArrayList
- programmers
- HashMap
- 재귀함수
- 우선순위 큐
- binary tree
- 브루트포스
- PCCP
- string
- DP
- dfs
- Algorithm
- Array
- leetcode
- greedy
- coding
- 깊이우선탐색
- recursion
- two pointers
- priority queue
- Java
- 리트코드
- 알고리즘
- Today
- Total
지식창고
[Java] LeetCode 1472. Design Browser History 본문
[Java] LeetCode 1472. Design Browser History
문 제 :
웹사이트의 뒤로가기, 앞으로가기 기능을 구현하여라.
구현할 메소드는 생성자를 포함한 4개이다.
생성자 : Browserhistory(String homepage)
방문 : void visit(String url)
뒤로가기 : String back(int steps)
앞으로 가기 : String forward(int steps)
Constraint
{ 1 <= homepage.length <= 20 }
{ 1 <= url.length <= 20 }
{ 1 <= steps <= 100 }
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.
Implement the BrowserHistory class:
BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
void visit(string url) Visits url from the current page. It clears up all the forward history.
string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.
Example )
Input:
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
Explanation:
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
접 근 :
Stack을 이용해 방문한 웹 페이지를 관리
해 결 :
Stack을 2개 생성하여, 하나는 현재 방문 중인 페이지를 저장하고, 하나는 뒤로가기를 했을 때, 내가 방문 했었던 페이지를 저장해주는 스택으로 구성한다.
뒤로가기를 할 때, 뒤로가기 횟수만큼 미래에 방문했었던 페이지를 저장하는 스택 (Stack2)에 저장해준다.
그리고나서 앞으로 가기를 하게되면, Stack2에서 메인스택(Stack1)으로 빼 오는 것이다.
새로운 페이지를 방문하게 되면 Stack2는 초기화해주고, 항상 Stack1의 맨 위에 있는 페이지가 사용자가 보게되는 페이지가 되게 만들어준다.
결 과 :
class BrowserHistory {
Stack<String> stack1;
Stack<String> stack2;
public BrowserHistory(String homepage) {
stack1 = new Stack<>();
visit(homepage);
}
public void visit(String url) {
stack2 = new Stack<>();
stack1.push(url);
}
public String back(int steps) {
while(stack1.size()>1 && steps-- > 0){
stack2.push(stack1.peek());
stack1.pop();
}
return stack1.peek();
}
public String forward(int steps) {
while(!stack2.isEmpty() && steps-- > 0){
stack1.push(stack2.peek());
stack2.pop();
}
return stack1.peek();
}
}
'Algorithm > Leetcode' 카테고리의 다른 글
[Java] LeetCode 2316. Count Unreachable Pairs of Nodes in an Undirected Graph (0) | 2023.03.25 |
---|---|
[Java] LeetCode 1319. Number of Operations to Make Network Connected (0) | 2023.03.23 |
[Java] LeetCode 208. Implement Trie (Prefix Tree) (0) | 2023.03.17 |
[Java] LeetCode 142. Linked List Cycle II (0) | 2023.03.09 |
[Java] LeetCode 1539. Kth Missing Positive Number (0) | 2023.03.06 |