题目链接:
https://www.lintcode.com/problem/implement-three-stacks-by-single-array/description
用一个数组实现三个栈。你可以假设这三个栈都一样大并且足够大。你不需要担心如果一个栈满了之后怎么办。
Example
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
思路:假设栈的大小为size,
1、定义一个数组用来保存栈的值,数组长度为3*size;
2、定义一个长度为3的size数组,用来保存三个栈的长度