LeetCode 1472. Historial del navegador de diseño (Edición de Javascript) | Por Anisha Jain | enero, 2023

Input:
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
  • BrowserHistory(string homepage): Este método es el constructor de la clase e inicializa el objeto con la página de inicio del navegador. Establece la URL actual en la página de inicio e inicializa dos listas, una para el historial de las URL visitadas (historial anterior) y otra para las URL desde las que se ha navegado (historial anterior).
  • void visit(string url): Este método se llama cuando el usuario visita una nueva URL. Borra el historial de avance y agrega la nueva URL al historial de retroceso. La URL actual se establece en la nueva URL.
  • string back(int steps): Este método se llama cuando el usuario quiere retroceder en el historial. El número de pasos para retroceder se toma como argumento. Si el usuario desea retroceder más pasos de los que están disponibles en el historial, el método retrocederá el máximo número de pasos posible. La URL actual se actualiza con la URL obtenida después de volver al historial.
  • string forward(int steps): Este método se llama cuando el usuario quiere avanzar en la fecha. Se toma como argumento el número de pasos a seguir. Si el usuario desea avanzar más pasos de los disponibles en la fecha, el método avanzará el máximo número de pasos posibles. La URL actual se actualiza con la URL obtenida después de avanzar en el historial.

Constructor

/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {
this.history = [homepage]
this.forwardHistory = []
};

Visita la URL

/** 
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {
this.history.push(url)
if (this.forwardHistory.length > 0) {
this.forwardHistory = []
}
};

Retrocede unos pasos

/** 
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
// Initializing this variable is important because we are altering
// the length of the array in the loop and it will affect
// our i < value check at each iteration
const l = this.history.length - 1
// Taking the min of steps or l will handle our edge case
// for steps > items in history
for (let i = 0; i < Math.min(steps, l); i++) {
let b = this.history.pop()
this.forwardHistory.push(b)
}
// Be sure not to use the above initialized variable "l" here
// because the length is now different
return this.history[this.history.length - 1]
};

Da unos pasos hacia adelante

/** 
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
const l = this.forwardHistory.length
if (l > 0) {
for (let i = 0; i < Math.min(steps, l); i++) {
let b = this.forwardHistory.pop()
this.history.push(b)
}
}
return this.history[this.history.length - 1]
};
/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {
this.history = [homepage]
this.forwardHistory = []
};

/**
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {
// console.log(`visit "${url}"`)
this.history.push(url)
if (this.forwardHistory.length > 0) {
this.forwardHistory = []
}
// console.log(`history: "${this.history}"`)
// console.log(`forward: "${this.forwardHistory}"`)
};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
// console.log(`back ${steps} steps`)
// console.log(this.history.length - 1, Math.min(steps, this.history.length - 1))
const l = this.history.length - 1
for (let i = 0; i < Math.min(steps, l); i++) {
let b = this.history.pop()
// console.log(`Popped ${b} from history`)
this.forwardHistory.push(b)
}
// console.log(`history: "${this.history}"`)
// console.log(`forward: "${this.forwardHistory}"`)
// console.log(`return: "${this.history[this.history.length - 1]}"`)
return this.history[this.history.length - 1]
};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
// console.log(`forward ${steps} steps`)
const l = this.forwardHistory.length
if (l > 0) {
for (let i = 0; i < Math.min(steps, l); i++) {
let b = this.forwardHistory.pop()
this.history.push(b)
}
}
// console.log(`history: "${this.history}"`)
// console.log(`forward: "${this.forwardHistory}"`)
// console.log(`return: "${this.history[this.history.length - 1]}"`)
return this.history[this.history.length - 1]
};

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

Leave a Reply

Your email address will not be published. Required fields are marked *