【数据结构与算法】程序员面试金典 3.1-3.6

面试题 03.01. 三合一

class TripleInOne {
    vector<int> s; // 用来存储数据
    int stackSize;
    int spointer[3];

public:
    TripleInOne(int stackSize) {
        s = vector<int>(stackSize * 3, 0);// vector s,初始化为长度是3个stackSize,默认3个栈长度相同
        this->stackSize = stackSize;
        spointer[0] = 0;
        spointer[1] = stackSize;
        spointer[2] = stackSize * 2;
    }
    
    void push(int stackNum, int value) {// stackNum是栈的序号,就是0,1,2共3个栈
        // 如果没有溢出
        if(spointer[stackNum] < (stackNum + 1) * stackSize){// 2号栈是第3个栈,计算长度范围时应+1
            s[spointer[stackNum]++] = value;// 第stackNum个栈,此时的下标是spointer[stackNum],先保存到s数组的这个指定的下标处,之后将这个下标自加1,这样保证索引指向新的空位置,便于下一次push
        }
    }
    
    int pop(int stackNum) {
        int res = -1;
        if(spointer[stackNum] > (stackNum)*stackSize){// 此处不必+1,看数组左端,就是栈底,是否还有元素,如果有元素:
            res = s[--spointer[stackNum]];// 应该先自减,退掉空值区,来到有值区,返回值
        }
        return res;
    }
    
    int peek(int stackNum) {
        int res = -1;
        if(spointer[stackNum] > stackNum*stackSize){
            res = s[spointer[stackNum] - 1];// 同pop,只是无需将索引退回1位
        }
        return res;
    }
    
    bool isEmpty(int stackNum) {
        return spointer[stackNum] == stackNum * stackSize;// 看看指针是否在其原来的位置上即可
    }
};


/**
 * 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);
 * bool param_4 = obj->isEmpty(stackNum);
 */

面试题 03.02. 栈的最小值

class MinStack {
    
vector<int> vi,vmin;
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        vi.push_back(x);
        if(vmin.size() != 0){
            if(x < vmin[vmin.size()-1]){
                vmin.push_back(x);
            }else{
                vmin.push_back(vmin[vmin.size()-1]);
            }
        }else{
            vmin.push_back(x);
        }
    }
    
    void pop() {
        if(vi.size() != 0 && vmin.size() != 0){
            vi.pop_back();
            vmin.pop_back();
        }
    }
    
    int top() {
        if(vi.size() == 0){
            return -1;
        }
        return vi[vi.size()-1];
    }
    
    int getMin() {
        if(vmin.size() == 0){
            return -1;
        }
        return vmin[vmin.size()-1];
    }
};

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

面试题 03.03. 堆盘子

“StackOfPlates” [2] 表示每堆盘子最高2个,再次push就要另添加一个栈来放盘子

“push” [1] 堆盘子1(第一堆有一个盘子1)

“push” [2] 堆盘子2(第一堆有两个盘子1,2)

“push” [3] 堆盘子3(第一堆已经有两个盘子了,达到_capcity,另起一堆,第二堆有一个盘子3)

“popAt” [0] 弹出指定堆(第一堆)的顶层盘子2

“popAt” [0] 弹出指定堆(第一堆)的顶层盘子1(此时第一堆没有盘子了,记得删除这一堆,原来的第二堆变成现在的第一堆)

“popAt” [0] 弹出指定堆(第一堆)的顶层盘子3

注意cap=0的时候。

class StackOfPlates {

    vector<stack<int>> vs;
    int stack_max_size;

public:
    StackOfPlates(int cap) {
        vs.clear();
        stack_max_size = cap;
    }
    
    void push(int val) {
        int vs_size = vs.size();
        if(vs_size == 0 || vs.back().size() == stack_max_size){
            stack<int> s;
            s.push(val);
            vs.push_back(s);
        }else{
            vs.back().push(val);
        }
    }
    
    int pop() {
        int vs_size = vs.size();
        if(vs_size == 0 || stack_max_size == 0){
            return -1;
        }
        int res = vs.back().top();
        vs.back().pop();
        if(vs.back().size() == 0){
            vs.pop_back();
        }
        return res;
    }
    
    int popAt(int index) {
        int vs_size = vs.size();
        if(index >= vs_size || stack_max_size == 0){
            return -1;
        }
        int res = vs[index].top();
        vs[index].pop();
        if(vs[index].size() == 0){
            vs.erase(vs.begin() + index);
        }
        return res;
    }
};

/**
 * Your StackOfPlates object will be instantiated and called as such:
 * StackOfPlates* obj = new StackOfPlates(cap);
 * obj->push(val);
 * int param_2 = obj->pop();
 * int param_3 = obj->popAt(index);
 */

面试题 03.04. 化栈为队

class MyQueue {
    stack<int> s1;
    stack<int> s2;
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);
        if(s2.empty()){// s2为空的时候,把s1中的内容全部给到s2
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        
        if(s2.empty()){ 
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
            if(s2.empty()){
                return -1;
            }
        }
        int res = s2.top();
        s2.pop();
        return res;
    }
    
    /** Get the front element. */
    int peek() {
        if(s2.empty()){ 
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
            if(s2.empty()){
                return -1;
            }
        }
        return s2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        if(s2.empty()){ 
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
            if(s2.empty()){
                return true;
            }
        }
        return false;
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

上面是我自己写的,有些冗余,参考了一位大佬代码,立刻双100%了。pop的时候,利用了一下peek,处理的很精彩:

class MyQueue {
    stack<int> s1;
    stack<int> s2;
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int res = peek();
        s2.pop();
        return res;
    }
    
    /** Get the front element. */
    int peek() {
        if(s2.empty()){ 
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
            if(s2.empty()){
                return -1;
            }
        }
        return s2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty() && s2.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

面试题 03.05. 栈排序

class SortedStack {
    stack<int> s1;
    stack<int> s2;
public:
    SortedStack() {

    }
    
    void push(int val) {
        if(s1.empty() || val <= s1.top()){
            s1.push(val);
        }
        if(val > s1.top()){
            while(!s1.empty() && val > s1.top()){
                s2.push(s1.top());
                s1.pop();
            }
            s1.push(val);
            while(!s2.empty()){
                s1.push(s2.top());
                s2.pop();
            }
        }
    }
    
    void pop() {
        if(!s1.empty()){
            s1.pop();
        }
    }
    
    int peek() {
        if(s1.empty()){
            return -1;
        }
        return s1.top();
    }
    
    bool isEmpty() {
        return s1.empty();
    }
};

/**
 * Your SortedStack object will be instantiated and called as such:
 * SortedStack* obj = new SortedStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->isEmpty();
 */

面试题 03.06. 动物收容所

class AnimalShelf {

    queue<vector<int>> m_queue_cat;
    queue<vector<int>> m_queue_dog;

public:
    AnimalShelf() {

    }
    
    void enqueue(vector<int> animal) {
        if(animal.at(1) == 0){
            m_queue_cat.push(animal);
        }else{
            m_queue_dog.push(animal);
        }
    }
    
    vector<int> dequeueAny() {
        vector<int> animal;
        if(m_queue_cat.empty() && m_queue_dog.empty()){
            return {-1,-1};
        }else if(!m_queue_cat.empty() && m_queue_dog.empty()){
            animal = m_queue_cat.front();
            m_queue_cat.pop();
        }else if(m_queue_cat.empty() && !m_queue_dog.empty()){
            animal = m_queue_dog.front();
            m_queue_dog.pop();
        }else{
            if(m_queue_cat.front().at(0) < m_queue_dog.front().at(0)){
                animal = m_queue_cat.front();
                m_queue_cat.pop();
            }else{
                animal = m_queue_dog.front();
                m_queue_dog.pop();
            }
        }
        return animal;
    }
    
    vector<int> dequeueDog() {
        if(m_queue_dog.empty()){
            return vector<int>{-1,-1};
        }
        vector<int> dog = m_queue_dog.front();
        m_queue_dog.pop();
        return dog;
    }
    
    vector<int> dequeueCat() {
        if(m_queue_cat.empty()){
            return vector<int>{-1,-1};
        }
        vector<int> cat = m_queue_cat.front();
        m_queue_cat.pop();
        return cat;
    }
};

/**
 * Your AnimalShelf object will be instantiated and called as such:
 * AnimalShelf* obj = new AnimalShelf();
 * obj->enqueue(animal);
 * vector<int> param_2 = obj->dequeueAny();
 * vector<int> param_3 = obj->dequeueDog();
 * vector<int> param_4 = obj->dequeueCat();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值