• 《Cracking the Coding Interview》——第3章:栈和队列——题目1


    2014-03-18 03:19

    题目:用一个数组实现3个栈。

    解法:

      首先我想过让三个栈动态决定长度。要么左右各一个向中间靠拢,要么三个穿插着,后来都觉得实现起来太复杂,而且思路总有各种功能缺陷,会导致额外的时间或空间复杂度。所以,还是三等分成固定大小吧。好写又好用。

    代码:

    // 3.1 Use an array to implement three stacks.
    // three fixed-length stacks
    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    
    template <class T>
    class ThreeStack {
    public:
        ThreeStack(size_t _total_capacity = 1): total_capacity(_total_capacity) {
            msize[0] = 0;
            msize[1] = 0;
            msize[2] = 0;
            
            mcapacity[0] = mcapacity[1] = mcapacity[2] = total_capacity / 3;
            mcapacity[0] += (total_capacity % 3 != 0 ? 1 : 0);
            mcapacity[1] += (total_capacity % 3 == 2 ? 1 : 0);
            
            offset[0] = 0;
            offset[1] = offset[0] + mcapacity[0];
            offset[2] = offset[1] + mcapacity[1];
            
            mdata.resize(total_capacity);
        }
        
        ~ThreeStack() {
            mdata.clear();
        }
        
        void push(int idx, T val) {
            if (msize[idx] == mcapacity[idx]) {
                // this stack is full
                return;
            }
            
            mdata[offset[idx] + msize[idx]] = val;
            ++msize[idx];
        }
        
        void pop(int idx) {
            if (msize[idx] == 0) {
                return;
            }
            
            --msize[idx];
        }
        
        T top(int idx) {
            if (msize[idx] == 0) {
                return mdata[-1];
            }
            
            return mdata[offset[idx] + msize[idx] - 1];
        }
        
        size_t size(int idx) {
            return msize[idx];
        }
    private:
        // total capacity of all stack
        size_t total_capacity;
        // starting offset for each stack
        size_t offset[3];
        // capacities of the three stacks
        size_t mcapacity[3];
        // sizes of the three stacks
        size_t msize[3];
        // the data in the stacks
        vector<T> mdata;
    };
    
    int main()
    {
        int n;
        size_t idx;
        int val;
        char str[100];
        
        scanf("%d", &n);
        ThreeStack<int> ts(n);
        while (scanf("%s", str) == 1) {
            if (strcmp(str, "end") == 0) {
                break;
            } else if (strcmp(str, "push") == 0) {
                scanf("%u%d", &idx, &val);
                ts.push(idx, val);
            } else if (strcmp(str, "pop") == 0) {
                scanf("%u", &idx);
                ts.pop(idx);
            } else if (strcmp(str, "top") == 0) {
                scanf("%u", &idx);
                printf("top[%u] = %d
    ", idx, ts.top(idx));
            } else if (strcmp(str, "size") == 0) {
                scanf("%u", &idx);
                printf("size[%u] = %u
    ", idx, ts.size(idx));
            }
        }
        
        return 0;
    }
  • 相关阅读:
    初学Java Web(2)——搭建Java Web开发环境 (转)
    我们为什么要配置CATALINA_HOME环境变量(转)
    【510】Attention-based Model
    【509】NLP实战系列(九)—— 结合 CNN 和 RNN 来处理长序列
    【510】NLP实战系列(八)—— 使用一维CNN处理 IMDB 情感分析
    【510】NLP实战系列(七)—— 进阶版(dropout/stacking/BiLSTM)
    【509】NLP实战系列(六)—— 通过 LSTM 来做分类
    apt-mark
    ROS程序开机自启动
    个人开发者可以申请微信支付了
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3606705.html
Copyright © 2020-2023  润新知