返回介绍

lcci / 03.01.Three in One / README

发布于 2024-06-17 01:04:43 字数 4531 浏览 0 评论 0 收藏 0

面试题 03.01. 三合一

English Version

题目描述

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

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

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

示例1:

 输入:
["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
 输出:
[null, null, null, 1, -1, -1, true]
说明:当栈为空时`pop, peek`返回-1,当栈满时`push`不压入元素。

示例2:

 输入:
["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
 输出:
[null, null, null, null, 2, 1, -1, -1]

解法

方法一

class TripleInOne:
  def __init__(self, stackSize: int):
    self._capacity = stackSize
    self._s = [[], [], []]

  def push(self, stackNum: int, value: int) -> None:
    if len(self._s[stackNum]) < self._capacity:
      self._s[stackNum].append(value)

  def pop(self, stackNum: int) -> int:
    return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop()

  def peek(self, stackNum: int) -> int:
    return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1]

  def isEmpty(self, stackNum: int) -> bool:
    return len(self._s[stackNum]) == 0


# Your TripleInOne object will be instantiated and called as such:
# obj = TripleInOne(stackSize)
# obj.push(stackNum,value)
# param_2 = obj.pop(stackNum)
# param_3 = obj.peek(stackNum)
# param_4 = obj.isEmpty(stackNum)
class TripleInOne {
  private int[] s;
  private int capacity;

  public TripleInOne(int stackSize) {
    s = new int[stackSize * 3 + 3];
    capacity = stackSize;
  }

  public void push(int stackNum, int value) {
    if (s[stackNum + 3 * capacity] < capacity) {
      s[s[stackNum + 3 * capacity] * 3 + stackNum] = value;
      ++s[stackNum + 3 * capacity];
    }
  }

  public int pop(int stackNum) {
    if (isEmpty(stackNum)) {
      return -1;
    }
    --s[stackNum + 3 * capacity];
    return s[s[stackNum + 3 * capacity] * 3 + stackNum];
  }

  public int peek(int stackNum) {
    return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum];
  }

  public boolean isEmpty(int stackNum) {
    return s[stackNum + 3 * capacity] == 0;
  }
}

/**
 * Your TripleInOne object will be instantiated and called as such:
 * TripleInOne obj = new TripleInOne(stackSize);
 * obj.push(stackNum,value);
 * int param_2 = obj.pop(stackNum);
 * int param_3 = obj.peek(stackNum);
 * boolean param_4 = obj.isEmpty(stackNum);
 */
type TripleInOne struct {
  data    []int
  offset  [3]int
  stackSize int
}

func Constructor(stackSize int) TripleInOne {
  total := stackSize * 3
  data := make([]int, total)
  offset := [3]int{}
  for i := 0; i < 3; i++ {
    offset[i] = i * stackSize
  }
  return TripleInOne{
    data:    data,
    offset:  offset,
    stackSize: stackSize,
  }
}

func (this *TripleInOne) Push(stackNum int, value int) {
  i := this.offset[stackNum]
  if i < (stackNum+1)*this.stackSize {
    this.data[i] = value
    this.offset[stackNum]++
  }
}

func (this *TripleInOne) Pop(stackNum int) int {
  i := this.offset[stackNum]
  if i == stackNum*this.stackSize {
    return -1
  }
  this.offset[stackNum]--
  return this.data[i-1]
}

func (this *TripleInOne) Peek(stackNum int) int {
  i := this.offset[stackNum]
  if i == stackNum*this.stackSize {
    return -1
  }
  return this.data[i-1]
}

func (this *TripleInOne) IsEmpty(stackNum int) bool {
  return this.offset[stackNum] == stackNum*this.stackSize
}

/**
 * Your TripleInOne object will be instantiated and called as such:
 * obj := Constructor(stackSize);
 * obj.Push(stackNum,value);
 * param_2 := obj.Pop(stackNum);
 * param_3 := obj.Peek(stackNum);
 * param_4 := obj.IsEmpty(stackNum);
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文