lintcode练习-224. 用一个数组实现三个栈

描述

用一个数组实现三个栈。你可以假设这三个栈都一样大并且足够大。你不需要担心如果一个栈满了之后怎么办。

您在真实的面试中是否遇到过这个题?  是

样例

ThreeStacks(5)  // create 3 stacks with size 5 in single array. stack index from 0 to 2
push(0, 10) // push 10 in the 1st stack.
push(0, 11)
push(1, 20) // push 20 in the 2nd stack.
push(1, 21)
pop(0) // return 11
pop(1) // return 21
peek(1) // return 20
push(2, 30)  // push 30 in the 3rd stack.
pop(2) // return 30
isEmpty(2) // return true
isEmpty(0) // return false

我的解法:

比较复杂,将一个数组分成了三份,需要考虑很多判断

class ThreeStacks:
    """
    @param: size: An integer
    """
    def __init__(self, size):
        # do intialization if necessary
        self.num = [None] * (3*size)
        self.one = self.num[0:size]
        self.two = self.num[size:2*size]
        self.three = self.num[2*size:3*size]

    """
    @param: stackNum: An integer
    @param: value: An integer
    @return: nothing
    """
    def push(self, stackNum, value):
        # Push value into stackNum stack
        if stackNum == 0:
            if self.one[0] is None:
                self.one.pop(0)
            self.one.append(value)
        elif stackNum == 1:
            if self.two[0] is None:
                self.two.pop(0)
            self.two.append(value)
        elif stackNum == 2:
            if self.three[0] is None:
                self.three.pop(0)
            self.three.append(value)

    """
    @param: stackNum: An integer
    @return: the top element
    """
    def pop(self, stackNum):
        # Pop and return the top element from stackNum stack
        if stackNum == 0:
            if not self.one:
                return [] 
            return self.one.pop()
        elif stackNum == 1:
            if not self.two:
                return []
            return self.two.pop()
        elif stackNum == 2:
            if not self.three:
                return []
            return self.three.pop()

    """
    @param: stackNum: An integer
    @return: the top element
    """
    def peek(self, stackNum):
        # Return the top element
        if stackNum == 0:
            if not self.one:
                return []
            return self.one[-1]
        elif stackNum == 1:
            if not self.two:
                return []
            return self.two[-1]
        elif stackNum == 2:
            if not self.three:
                return []
            return self.three[-1]

    """
    @param: stackNum: An integer
    @return: true if the stack is empty else false
    """
    def isEmpty(self, stackNum):
        # write your code here
        if stackNum == 0:
            return self.peek(stackNum) is None
        elif stackNum == 1:
            return self.peek(stackNum) is None
        elif stackNum == 2:
            return self.peek(stackNum) is None

排名第一的算法,比较简洁,在数组内又定义了三个数组

class ThreeStacks:
    """
    @param: size: An integer
    """
    def __init__(self, size):
        # do intialization if necessary
        self.size = size
        self.stack = [[], [], []]
    """
    @param: stackNum: An integer
    @param: value: An integer
    @return: nothing
    """
    def push(self, stackNum, value):
        # Push value into stackNum stack
        self.stack[stackNum].append(value)
    """
    @param: stackNum: An integer
    @return: the top element
    """
    def pop(self, stackNum):
        # Pop and return the top element from stackNum stack
        return self.stack[stackNum].pop()
    """
    @param: stackNum: An integer
    @return: the top element
    """
    def peek(self, stackNum):
        # Return the top element
        return self.stack[stackNum][-1]
    """
    @param: stackNum: An integer
    @return: true if the stack is empty else false
    """
    def isEmpty(self, stackNum):
        # write your code here
        return len(self.stack[stackNum]) == 0

猜你喜欢

转载自blog.csdn.net/qq_36387683/article/details/81194233
今日推荐