File tree Expand file tree Collapse file tree 4 files changed +122
-0
lines changed Expand file tree Collapse file tree 4 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "math"
4+
5+ func main () {
6+
7+ }
8+
9+ type BrowserHistory struct {
10+ history []string
11+ current int
12+ }
13+
14+ func Constructor (homepage string ) BrowserHistory {
15+ return BrowserHistory {
16+ history : []string {homepage },
17+ current : 0 ,
18+ }
19+ }
20+
21+ func (this * BrowserHistory ) Visit (url string ) {
22+ this .history = this .history [0 : this .current + 1 ]
23+ this .history = append (this .history , url )
24+ this .current ++
25+ }
26+
27+ func (this * BrowserHistory ) Back (steps int ) string {
28+ this .current = int (math .Max (float64 (this .current ) - float64 (steps ), 0 ))
29+ return this .history [this .current ]
30+ }
31+
32+ func (this * BrowserHistory ) Forward (steps int ) string {
33+ this .current = int (math .Min (float64 (this .current ) + float64 (steps ), float64 (len (this .history ) - 1 )))
34+ return this .history [this .current ]
35+ }
36+
Original file line number Diff line number Diff line change 1+ class BrowserHistory {
2+ constructor ( homepage ) {
3+ this . history = [ homepage ] ;
4+ this . current = 0 ;
5+ }
6+
7+ /**
8+ * @param {string } url
9+ * @return {void }
10+ */
11+
12+ visit ( url ) {
13+ this . history [ ++ this . current ] = url ;
14+ this . history . length = this . current + 1 ;
15+ }
16+
17+ /**
18+ * @param {number } steps
19+ * @return {string }
20+ */
21+ back ( steps ) {
22+ this . current = Math . max ( this . current - steps , 0 ) ;
23+ return this . history [ this . current ] ;
24+ }
25+
26+ /**
27+ * @param {number } steps
28+ * @return {string }
29+ */
30+ forward ( steps ) {
31+ this . current = Math . min ( this . current + steps , this . history . length - 1 ) ;
32+ return this . history [ this . current ] ;
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ struct BrowserHistory {
2+ history : Vec < String > ,
3+ current : usize ,
4+ }
5+
6+ impl BrowserHistory {
7+ fn new ( homepage : String ) -> Self {
8+ Self {
9+ history : vec ! [ homepage] ,
10+ current : 0 ,
11+ }
12+ }
13+
14+ fn visit ( & mut self , url : String ) {
15+ self . current += 1 ;
16+ self . history . splice ( self . current .., std:: iter:: once ( url) ) ;
17+ }
18+
19+ fn back ( & mut self , steps : i32 ) -> String {
20+ self . current = self . current . saturating_sub ( steps as usize ) ;
21+ self . history [ self . current ] . clone ( )
22+ }
23+
24+ fn forward ( & mut self , steps : i32 ) -> String {
25+ self . current = ( self . current + steps as usize ) . min ( self . history . len ( ) - 1 ) ;
26+ self . history [ self . current ] . clone ( )
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ class BrowserHistory {
2+ history : string [ ] ;
3+ current : number ;
4+
5+ constructor ( homepage : string ) {
6+ this . history = [ homepage ] ;
7+ this . current = 0 ;
8+ }
9+
10+ visit ( url : string ) : void {
11+ this . history [ ++ this . current ] = url ;
12+ this . history . length = this . current + 1 ;
13+ }
14+
15+ back ( steps : number ) : string {
16+ this . current = Math . max ( this . current - steps , 0 ) ;
17+ return this . history [ this . current ] ;
18+ }
19+
20+ forward ( steps : number ) : string {
21+ this . current = Math . min ( this . current + steps , this . history . length - 1 ) ;
22+ return this . history [ this . current ] ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments