js解leetcode(61)-简单

1.三合一

题目:

三合一。描述如何只用一个数组来实现三个栈。

你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。stackNum表示栈下标,value表示压入的值。

构造函数会传入一个stackSize参数,代表每个栈的大小。

思路:二维数组,记录每个下表对应的数据

/**
 * @param {number} stackSize
 */
var TripleInOne = function(stackSize) {
  this.stack = []
  this.size = stackSize
};

/** 
 * @param {number} stackNum 
 * @param {number} value
 * @return {void}
 */
TripleInOne.prototype.push = function(stackNum, value) {
  if(!this.stack[stackNum]){
    this.stack[stackNum] = []
  }
  if(this.stack[stackNum].length < this.size){
    this.stack[stackNum].push(value)
  }

};

/** 
 * @param {number} stackNum
 * @return {number}
 */
TripleInOne.prototype.pop = function(stackNum) {
  if(this.stack[stackNum] && this.stack[stackNum].length){
    return this.stack[stackNum].pop()
  }   
  return -1

};

/** 
 * @param {number} stackNum
 * @return {number}
 */
TripleInOne.prototype.peek = function(stackNum) {
  if(this.stack[stackNum] && this.stack[stackNum].length){
    return this.stack[stackNum][this.stack[stackNum].length - 1]
  }   
  return -1
};

/** 
 * @param {number} stackNum
 * @return {boolean}
 */
TripleInOne.prototype.isEmpty = function(stackNum) {
  return !this.stack[stackNum] || !this.stack[stackNum].length
};

/**
 * Your TripleInOne object will be instantiated and called as such:
 * var obj = new TripleInOne(stackSize)
 * obj.push(stackNum,value)
 * var param_2 = obj.pop(stackNum)
 * var param_3 = obj.peek(stackNum)
 * var param_4 = obj.isEmpty(stackNum)
 */

2.栈的最小值

题目:

请设计一个栈,除了常规栈支持的pop与push函数以外,还支持min函数,该函数返回栈元素中的最小值。执行push、pop和min操作的时间复杂度必须为O(1)。

思路:用链表记录节点,用数组升序记录节点值

/**
 * initialize your data structure here.
 */
var MinStack = function() {
  this.node = null;
  this.stack = [];
};

/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function(x) {
  this.node = {
    val: x,
    next: this.node,
  };
  if (!this.stack.length) {
    this.stack.push(x);
    return;
  }
  let i = 0;
  while (i<this.stack.length) {
    if (this.stack[i] >= x) {
      break;
    }
    i++;
  }
  if (!i) {
    this.stack.unshift(x);
  } else {
    this.stack.splice(i, 0, x);
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
  if (!this.node) return;
    const x = this.node.val;
  this.node = this.node.next;
  let i = 0;
  while (true) {
    if (this.stack[i] == x) {
      break;
    }
    i++;
  }
  if (!i) {
    this.stack.shift();
  } else {
    this.stack.splice(i, 1);
  }
};

/**
 * @return {number}
 */
MinStack.prototype.top = function() {
  if (!this.node) return null;
  return this.node.val;
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
  if (!this.node) return null;
  return this.stack[0];
};

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(x)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */

3.化栈为队

题目:

实现一个MyQueue类,该类用两个栈来实现一个队列。

思路:两个栈,一个栈用于push,一个栈用于pop。在push时,让所有元素都推入栈1,.pop时所有元素都推入栈2

/**
 * Initialize your data structure here.
 */
var MyQueue = function () {
  this.stack1 = null;
  this.stack2 = null;
};

/**
 * Push element x to the back of queue.
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function (x) {
  while (this.stack2) {
    const next = this.stack2.next;
    this.stack2.next = this.stack1;
    this.stack1 = this.stack2;
    this.stack2 = next;
  }
  const node = {
    val: x,
    next: this.stack1,
  };
  this.stack1 = node;
};

/**
 * Removes the element from in front of queue and returns that element.
 * @return {number}
 */
MyQueue.prototype.pop = function () {
  while (this.stack1) {
    const next = this.stack1.next;
    this.stack1.next = this.stack2;
    this.stack2 = this.stack1;
    this.stack1 = next;
  }
  const node = this.stack2;
  this.stack2 = this.stack2.next;
  return node.val;
};

/**
 * Get the front element.
 * @return {number}
 */
MyQueue.prototype.peek = function () {
  while (this.stack1) {
    const next = this.stack1.next;
    this.stack1.next = this.stack2;
    this.stack2 = this.stack1;
    this.stack1 = next;
  }
  return this.stack2.val;
};

/**
 * Returns whether the queue is empty.
 * @return {boolean}
 */
MyQueue.prototype.empty = function () {
  return !this.stack1 && !this.stack2;
};


/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

4.动物收容所

题目:

动物收容所。有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,实现各种操作方法,比如enqueue、dequeueAny、dequeueDog和dequeueCat。允许使用Java内置的LinkedList数据结构。

enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。

dequeue*方法返回一个列表[动物编号, 动物种类],若没有可以收养的动物,则返回[-1,-1]。

思路:数组记录

var AnimalShelf = function () {
  this.data = [];
};

/**
 * @param {number[]} animal
 * @return {void}
 */
AnimalShelf.prototype.enqueue = function (animal) {
  this.data.push(animal);
};

/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueAny = function () {
  if (!this.data.length) return [-1, -1];
  return this.data.shift();
};

/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueDog = function () {
  const index = this.data.findIndex((item) => item[1] == 1);
  if (index < 0) return [-1, -1];
  return this.data.splice(index, 1)[0];
};

/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueCat = function () {
  const index = this.data.findIndex((item) => item[1] == 0);
  if (index < 0) return [-1, -1];
  return this.data.splice(index, 1)[0];
};

/**
 * Your AnimalShelf object will be instantiated and called as such:
 * var obj = new AnimalShelf()
 * obj.enqueue(animal)
 * var param_2 = obj.dequeueAny()
 * var param_3 = obj.dequeueDog()
 * var param_4 = obj.dequeueCat()
 */

5.最小高度树

题目:给定一个有序整数数组,元素各不相同且按升序排列,编写一个算法,创建一棵高度最小的二叉搜索树。

思路:高度最小,所以是自平衡数,用分治法,比如一个数组,左半数组构建左子树,右半数组构建右子树。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function(nums) {
  const l = nums.length;
  if (!l) return null;
  if (l == 1) return new TreeNode(nums[0]);
  const mid = ~~(l / 2);
  const left = nums.slice(0, mid);
  const right = nums.slice(mid + 1);
  const root = new TreeNode(nums[mid]);
  root.left = sortedArrayToBST(left);
  root.right = sortedArrayToBST(right);
  return root;
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值