Design Browser History

ㅋㅋ·2023년 3월 18일
0

알고리즘-leetcode

목록 보기
128/135

인터넷 브라우저에서 앞서 방문한 사이트들을 앞뒤로 이동 할 수 있는 방문 기록을 구현하는 문제

  • BrowserHistory(string homepage)
  • void visit(string url)
  • string back(int steps)
  • string forward(int steps)

위 4가지 함수를 구현하면 된다.

class BrowserHistory {
public:
    BrowserHistory(string homepage) {
        
        _history.clear();
        _index = -1;
        _maxSize = 0;

        visit(homepage);
    }
    
    void visit(string url) {
        
        ++_index;
        if (_index == _history.size())
        {
            _history.emplace_back(url);
            ++_maxSize;
        }
        else
        {
            _history[_index] = url;
            _maxSize = _index + 1;
        }
    }
    
    string back(int steps) {
        
        _index = std::max(0, _index - steps);

        return _history[_index];
    }
    
    string forward(int steps) {
        
        _index = std::min(_maxSize - 1, _index + steps);

        return _history[_index];
    }

private:
    vector<string> _history;
    int _index;
    int _maxSize;
};

/**
 * Your BrowserHistory object will be instantiated and called as such:
 * BrowserHistory* obj = new BrowserHistory(homepage);
 * obj->visit(url);
 * string param_2 = obj->back(steps);
 * string param_3 = obj->forward(steps);
 */

0개의 댓글