Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created September 30, 2020 08:08
Show Gist options
  • Save charlieInDen/51badf86f2fbd8b0b42dd32f5e5b235c to your computer and use it in GitHub Desktop.
Save charlieInDen/51badf86f2fbd8b0b42dd32f5e5b235c to your computer and use it in GitHub Desktop.
class BrowserHistory {
var history: [String]!
var current: Int!
var bound: Int!
init(_ homepage: String) {
history = [homepage]
current = 0
bound = 0
}
func visit(_ url: String) {
current = current + 1
if current == history.count {
history.append(url)
}else {
history[current] = url
}
bound = current
}
func back(_ steps: Int) -> String {
current = max(current - steps, 0)
return history[current]
}
func forward(_ steps: Int) -> String {
current = min(steps + current, bound)
return history[current]
}
}
/**
* Your BrowserHistory object will be instantiated and called as such:
* let obj = BrowserHistory(homepage)
* obj.visit(url)
* let ret_2: String = obj.back(steps)
* let ret_3: String = obj.forward(steps)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment