|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +--* 1472. Design Browser History *-- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +Implement the BrowserHistory class: |
| 11 | +
|
| 12 | +BrowserHistory(string homepage) Initializes the object with the homepage of the browser. |
| 13 | +void visit(string url) Visits url from the current page. It clears up all the forward history. |
| 14 | +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. |
| 15 | +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. |
| 16 | + |
| 17 | +
|
| 18 | +Example: |
| 19 | +
|
| 20 | +Input: |
| 21 | +["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] |
| 22 | +[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] |
| 23 | +Output: |
| 24 | +[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] |
| 25 | +
|
| 26 | +Explanation: |
| 27 | +BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); |
| 28 | +browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" |
| 29 | +browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" |
| 30 | +browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" |
| 31 | +browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" |
| 32 | +browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" |
| 33 | +browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" |
| 34 | +browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" |
| 35 | +browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. |
| 36 | +browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" |
| 37 | +browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com" |
| 38 | + |
| 39 | +
|
| 40 | +Constraints: |
| 41 | +
|
| 42 | +1 <= homepage.length <= 20 |
| 43 | +1 <= url.length <= 20 |
| 44 | +1 <= steps <= 100 |
| 45 | +homepage and url consist of '.' or lower case English letters. |
| 46 | +At most 5000 calls will be made to visit, back, and forward. |
| 47 | +
|
| 48 | +*/ |
| 49 | + |
| 50 | +/* |
| 51 | +
|
| 52 | +
|
| 53 | +class BrowserHistory { |
| 54 | +
|
| 55 | + BrowserHistory(String homepage) { |
| 56 | +
|
| 57 | + } |
| 58 | + |
| 59 | + void visit(String url) { |
| 60 | +
|
| 61 | + } |
| 62 | + |
| 63 | + String back(int steps) { |
| 64 | +
|
| 65 | + } |
| 66 | + |
| 67 | + String forward(int steps) { |
| 68 | +
|
| 69 | + } |
| 70 | +} |
| 71 | +
|
| 72 | +/** |
| 73 | + * Your BrowserHistory object will be instantiated and called as such: |
| 74 | + * BrowserHistory obj = BrowserHistory(homepage); |
| 75 | + * obj.visit(url); |
| 76 | + * String param2 = obj.back(steps); |
| 77 | + * String param3 = obj.forward(steps); |
| 78 | + */ |
| 79 | +
|
| 80 | +*/ |
| 81 | + |
| 82 | +// import 'dart:math'; |
| 83 | + |
| 84 | +// class BrowserHistory { |
| 85 | +// late List<String> list; |
| 86 | +// int total = 0; |
| 87 | +// int current = 0; |
| 88 | +// BrowserHistory(String homepage) { |
| 89 | +// list = <String>[]; |
| 90 | +// list.add(homepage); |
| 91 | +// total++; |
| 92 | +// current++; |
| 93 | +// } |
| 94 | + |
| 95 | +// void visit(String url) { |
| 96 | +// if (list.length > current) { |
| 97 | +// list[current] = url; |
| 98 | +// } else { |
| 99 | +// list.add(url); |
| 100 | +// } |
| 101 | +// current++; |
| 102 | +// total = current; |
| 103 | +// } |
| 104 | + |
| 105 | +// String back(int steps) { |
| 106 | +// current = max(1, current - steps); |
| 107 | +// return list.elementAt(current - 1); |
| 108 | +// } |
| 109 | + |
| 110 | +// String forward(int steps) { |
| 111 | +// current = min(total, current + steps); |
| 112 | +// return list.elementAt(current - 1); |
| 113 | +// } |
| 114 | +// } |
| 115 | + |
| 116 | +// /** |
| 117 | +// * Your BrowserHistory object will be instantiated and called as such: |
| 118 | +// * BrowserHistory obj = BrowserHistory(homepage); |
| 119 | +// * obj.visit(url); |
| 120 | +// * String param2 = obj.back(steps); |
| 121 | +// * String param3 = obj.forward(steps); |
| 122 | +// */ |
| 123 | + |
| 124 | +// Doubly Linked List |
| 125 | + |
| 126 | +class Node { |
| 127 | + String? value; |
| 128 | + Node? next; |
| 129 | + Node? previous; |
| 130 | + Node(String source) { |
| 131 | + value = source; |
| 132 | + previous = next = null; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +class BrowserHistory { |
| 137 | + late Node root; |
| 138 | + BrowserHistory(String homepage) { |
| 139 | + root = Node(homepage); |
| 140 | + } |
| 141 | + |
| 142 | + void visit(String url) { |
| 143 | + Node node = Node(url); |
| 144 | + root.next = node; |
| 145 | + node.previous = root; |
| 146 | + root = node; |
| 147 | + } |
| 148 | + |
| 149 | + String back(int steps) { |
| 150 | + Node current = root; |
| 151 | + while (steps > 0 && current.previous != null) { |
| 152 | + current = current.previous!; |
| 153 | + steps--; |
| 154 | + } |
| 155 | + root = current; |
| 156 | + return current.value!; |
| 157 | + } |
| 158 | + |
| 159 | + String forward(int steps) { |
| 160 | + Node cur = root; |
| 161 | + while (steps > 0 && cur.next != null) { |
| 162 | + cur = cur.next!; |
| 163 | + steps--; |
| 164 | + } |
| 165 | + root = cur; |
| 166 | + return cur.value!; |
| 167 | + } |
| 168 | +} |
0 commit comments