파비의 매일매일 공부기록

2023.03.18 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.18 Today's Challenge

fabichoi 2023. 3. 18. 23:45

https://leetcode.com/problems/design-browser-history/

 

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

링크드리스트를 이용한 풀이

class Node:
    def __init__(self, url: str):
        self.prev = None
        self.next = None
        self.url = url        

class BrowserHistory:
    def __init__(self, homepage: str):
        self.curr = Node(homepage)        

    def visit(self, url: str) -> None:
        self.curr.next = Node(url)
        self.curr.next.prev = self.curr
        self.curr = self.curr.next

    def back(self, steps: int) -> str:        
        while self.curr.prev and steps > 0:
            self.curr = self.curr.prev
            steps -= 1
        
        return self.curr.url

    def forward(self, steps: int) -> str:
        while self.curr.next and steps > 0:
            self.curr = self.curr.next
            steps -= 1
        
        return self.curr.url
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.20 Today's Challenge  (0) 2023.03.20
2023.03.19 Today's Challenge  (0) 2023.03.19
2023.03.17 Today's Challenge  (0) 2023.03.17
2023.03.16 Today's Challenge  (0) 2023.03.16
2023.03.15 Today's Challenge  (0) 2023.03.15
Comments