forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create: 1472-design-browser-history.rs / .ts / .js / .go
- Loading branch information
1 parent
ee7a3bc
commit 3e71fc9
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import "math" | ||
|
||
func main() { | ||
|
||
} | ||
|
||
type BrowserHistory struct { | ||
history []string | ||
current int | ||
} | ||
|
||
func Constructor(homepage string) BrowserHistory { | ||
return BrowserHistory{ | ||
history: []string{homepage}, | ||
current: 0, | ||
} | ||
} | ||
|
||
func (this *BrowserHistory) Visit(url string) { | ||
this.history = this.history[0: this.current + 1] | ||
this.history = append(this.history, url) | ||
this.current++ | ||
} | ||
|
||
func (this *BrowserHistory) Back(steps int) string { | ||
this.current = int(math.Max(float64(this.current) - float64(steps), 0)) | ||
return this.history[this.current] | ||
} | ||
|
||
func (this *BrowserHistory) Forward(steps int) string { | ||
this.current = int(math.Min(float64(this.current) +float64(steps), float64(len(this.history) - 1))) | ||
return this.history[this.current] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class BrowserHistory { | ||
constructor(homepage) { | ||
this.history = [homepage]; | ||
this.current = 0; | ||
} | ||
|
||
/** | ||
* @param {string} url | ||
* @return {void} | ||
*/ | ||
|
||
visit(url) { | ||
this.history[++this.current] = url; | ||
this.history.length = this.current + 1; | ||
} | ||
|
||
/** | ||
* @param {number} steps | ||
* @return {string} | ||
*/ | ||
back(steps) { | ||
this.current = Math.max(this.current - steps, 0); | ||
return this.history[this.current]; | ||
} | ||
|
||
/** | ||
* @param {number} steps | ||
* @return {string} | ||
*/ | ||
forward(steps) { | ||
this.current = Math.min(this.current + steps, this.history.length - 1); | ||
return this.history[this.current]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
struct BrowserHistory { | ||
history: Vec<String>, | ||
current: usize, | ||
} | ||
|
||
impl BrowserHistory { | ||
fn new(homepage: String) -> Self { | ||
Self { | ||
history: vec![homepage], | ||
current: 0, | ||
} | ||
} | ||
|
||
fn visit(&mut self, url: String) { | ||
self.current += 1; | ||
self.history.splice(self.current.., std::iter::once(url)); | ||
} | ||
|
||
fn back(&mut self, steps: i32) -> String { | ||
self.current = self.current.saturating_sub(steps as usize); | ||
self.history[self.current].clone() | ||
} | ||
|
||
fn forward(&mut self, steps: i32) -> String { | ||
self.current = (self.current + steps as usize).min(self.history.len() - 1); | ||
self.history[self.current].clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class BrowserHistory { | ||
history: string[]; | ||
current: number; | ||
|
||
constructor(homepage: string) { | ||
this.history = [homepage]; | ||
this.current = 0; | ||
} | ||
|
||
visit(url: string): void { | ||
this.history[++this.current] = url; | ||
this.history.length = this.current + 1; | ||
} | ||
|
||
back(steps: number): string { | ||
this.current = Math.max(this.current - steps, 0); | ||
return this.history[this.current]; | ||
} | ||
|
||
forward(steps: number): string { | ||
this.current = Math.min(this.current + steps, this.history.length - 1); | ||
return this.history[this.current]; | ||
} | ||
} |