acwing刷题(简单篇综合)

一:调整数组顺序使奇数位于偶数前面

class Solution {
public:
    void reOrderArray(vector<int> &array) {
        int i =0,j =array.size()-1;
        while(i<j)
        {
            while(array[i]%2==1) i++;
            while(array[j]%2==0) j--;
            if(i<j) 
            {
                int temp = array[j];
                array[j] = array[i];
                array[i] = temp;
            }
        }
    }
};

二:使用两个栈实现队列的功能(纪念acwing第一个自己一步到位的题)

class MyQueue {
public:
    /** Initialize your data structure here. */
    stack <int> a,b;
    MyQueue() {
        
    };
    
    /** Push element x to the back of queue. */
    void push(int x) {
        a.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int t;//储存首元素
        if(b.empty())
        {
            while(!a.empty())
            {
                int temp = a.top();
                b.push(temp);
                a.pop();
            }
            t = b.top();  
            b.pop();
        }
        else 
        {
            t = b.top();
            b.pop();
        }
        return t;
    }
    
    /** Get the front element. */
    int peek() {
        int t;//储存首元素
        if(b.empty())
        {
            while(!a.empty())
            {
                int temp = a.top();
                b.push(temp);
                a.pop();
            }
            t = b.top();  
        }
        else 
        {
            t = b.top();
        }
        return t;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        if(a.empty()&&b.empty()) return true;
        else return false;
    }
};

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

三:实现最小栈

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        
    };
    stack<int> s;
    stack<int> m;
    void push(int x) {
        if(s.empty())
        {
             s.push(x);
             m.push(x);
        }
        else
        {
            if(x<=m.top()) m.push(x);
            s.push(x);
        }
    }
    
    void pop() {
        if(s.top() == m.top())
        {
            m.pop();
        }
        s.pop();
    }
    
    int top() {
            return s.top();   
    }
    
    int getMin() {
        return m.top();
    }
};

/**
 * 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();
 */

遇到问题:

1.最开始没有想到使用一个最小栈,而是直接定义一个min来储存,导致当这个min出栈后min的值不会更新

2.判断进入最小栈的条件是    x<=m.top();而不是x<m.top();

四:回文数

1.把数字转换成字符串(比较简单)

2.使用数字反转

这里注意:

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小布丁729

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值