头像

昔日少年




离线:3天前


最近来访(3)
用户头像
acwing9876
用户头像
Anohgy


C++ 代码

class MyQueue {
public:
    stack<int> s1;
    stack<int> s2;
    /** 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() {
        if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.top());
                s1.pop();
            }
        }
        int temp = s2.top();
        s2.pop();
        return temp;

    }

    /** Get the front element. */
    int peek() {
        if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.top());
                s1.pop();
            }
        }
        return s2.top();
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        if (s1.empty() && s2.empty()) {
            return true;
        }
        else return false;
    }
};



遍历字符串,遇到空格就替换

C++ 代码

class Solution {
public:
    string replaceSpaces(string &str) {

        for (int i = 0; i < str.size(); i++) {
            if (str[i] == ' ') {
                str.replace(i,1,"%20");
            }
        }
        return str;
    }
};