一:调整数组顺序使奇数位于偶数前面
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.使用数字反转
这里注意: