1.二分查找
2.动态规划
3.栈
class Solution {
public:
int search(vector<int>& nums, int target) {
int left=0,right=nums.size() -1;
while(left<=right)
{
int mid = (right - left )/2 + left;
int num=nums[mid];
if(num==target)
{
return mid;
}
else if(num>target)
{
right=mid-1;
}
else
left=mid+1;
}
return -1;
}
};
class Solution {
public:
int fib(int n) {
int f[31];
f[0]=0,f[1]=1;
for(int i=2;i<=n;i++)
{
f[i]=f[i-1]+f[i-2];
}
return f[n];
}
};
class Solution {
public:
int tribonacci(int n) {
int T[38];
T[0]=0;T[1]=1;T[2]=1;
for(int i=3;i<=n;i++)
{
T[i]=T[i-3]+T[i-2]+T[i-1];
}
return T[n];
}
};
class CQueue {
private:
stack<int>inStack,outStack;
void in2out(){
while (!inStack.empty()){
outStack.push(inStack.top());
inStack.pop();
}
}
public:
CQueue() {}
void appendTail(int value) {
inStack.push(value);
}
int deleteHead() {
if(outStack.empty()){
if(inStack.empty()){
return -1;
}
in2out();
}
int value = outStack.top();
outStack.pop();
return value;
}
};