用两个栈实现队列
class Solution
{
public:
void push(int node) {
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
stack1.push(node);
while(!stack2.empty()){
stack1.push(stack2.top());
stack2.pop();
}
}
int pop() {
if(stack1.empty()){
return -1;
}
int node = stack1.top();
stack1.pop();
return node;
}
private:
stack<int> stack1;
stack<int> stack2;
};
旋转数组的最小数字
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0){
return 0;
}
int cur = rotateArray[0];
for(unsigned long i=1;i<rotateArray.size();i++){
if(rotateArray[i]>=cur)
cur = rotateArray[i];
else{
cur = rotateArray[i];
break;
}
}
return cur;
}
};
菲波那切数列
class Solution {
public:
int Fibonacci(int n) {
int a = 0,b = 1;
int cur = 0;
if(n==0) return a;
else if(n==1) return b;
else{
for (int i=2;i<=n;i++){
cur = a + b;
a = b;
b = cur;
}
}
return cur;
}
};
跳台阶
class Solution {
public:
int jumpFloor(int number) {
int a = 1,b = 2;
int cur = 0;
if (number==0)
return 0;
if (number==1)
return a;
if (number==2)
return b;
for(int i=3;i<=number;i++){
cur = a+b;
a = b;
b = cur;
}
return cur;
}
};