https://leetcode.com/problems/design-browser-history/description/
Design Browser History - LeetCode
Can you solve this real interview question? Design Browser History - 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. Implem
leetcode.com
문제 요약
인터넷 브라우저에서 방문기록과 동일한 작동을 하는 BrowserHistory 클래스를 구현하는 것이다.
구현할 브라우저는 hompage에서 시작하고, 이후에는 다른 url에 방문할 수 있다. 또, 뒤로가기와 앞으로가기가 작동하도록 구현해야한다.
BroswerHistroy(string homepage) - 브라우저는 hompage에서 시작이 된다.
void visit(string url) - 현재 page의 앞에 있는 페이지기록은 다 삭제가 되고 url로 방문한다.
string back(int steps) - steps 수 만큼 뒤로가기를 한다. 뒤로가기를 할 수 있는 페이지 개수가 x이고 step>x라면 x번 만큼만 뒤로가기를 한다. 뒤로가기가 완료되면 현재 url을 반환한다.
string forward(int steps) - steps 수 만큼 앞으로 가기를 한다. back과 동일한 원리로 동작한다.
구현
class BrowserHistory(object):
class Url:
def __init__(self, prev=None, value=0, next=None):
self.prev = prev
self.value = value
self.next = next
def __init__(self, homepage):
newpage = self.Url(value=homepage)
self.head = newpage #head 없어도됨
self.current = newpage
print(None)
def visit(self, url):
newurl = self.Url(value=url, prev=self.current)
self.current.next = newurl
self.current = newurl
print(None)
def back(self, steps):
while steps > 0 and self.current != self.head: #self.current.prev != None으로 대체 가능
self.current = self.current.prev
steps -= 1
print(self.current.value)
def forward(self, steps):
while steps > 0 and self.current.next != None:
self.current = self.current.next
steps -= 1
print(self.current.value)
b = BrowserHistory("leetcode.com")
b.visit("google.com")
b.visit("facebook.com")
b.visit("youtube.com")
b.back(1)
b.back(1)
b.forward(1)
b.visit("linkedin.com")
b.forward(2)
b.back(2)
b.back(7)
'언어 > Python' 카테고리의 다른 글
[프로그래머스 알고리즘 Kit] 스택/큐 - 다리를 지나는 트럭 (0) | 2023.11.05 |
---|---|
[프로그래머스 알고리즘 Kit] 스택/큐 - 주식가격 (0) | 2023.10.30 |
이중연결리스트(Doubled Linked-List) 구현 (0) | 2023.10.24 |
[김왼손의 왼손코딩] 파이썬 기초강의(2) (0) | 2023.10.18 |
[김왼손의 왼손코딩] 파이썬 기초 강의(1) (0) | 2023.10.16 |
댓글