[LeetCode 231,232][简单]2的幂/用栈实现队列

231.2的幂
题目链接

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n>0 && !(n&-n^n);
    }
};

232.用栈实现队列
题目链接

class MyQueue {
public:
    /** Initialize your data structure here. */
    deque<int>qin;
    deque<int>qout;
    int nw = 1,pre = 0;
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        qin.emplace_back(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int ret;
        if(qout.empty()){
            while(!qin.empty()){
                qout.emplace_back(qin.back());
                qin.pop_back();
            }
        }
        ret = qout.back();
        qout.pop_back();
        return ret;
    }
    
    /** Get the front element. */
    int peek() {
        if(qout.empty()){
            while(!qin.empty()){
                qout.emplace_back(qin.back());
                qin.pop_back();
            }
        }
        return qout.back();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return qin.empty()&&qout.empty();
    }
};
发布了104 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/IDrandom/article/details/104331515