标签:二维数组 实现 alt empty 用两个栈 递增 else 插入 size
class Solution { public: bool Find(int target, vector<vector<int> > array) { int m = array.size();//行数 int n = array[0].size();//列数 int x =m-1,y=0; while(x >= 0 && y < n ){ if(array[x][y] == target){ return true; } else if(array[x][y] > target) { x--; continue; } else if(array[x][y] < target) { y++; continue; } } return false; } };
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int>dev; if(head!=NULL) //头不为空 { dev.insert(dev.begin(),head->val); //在vector首插入节点 while(head->next!=NULL) //next不为空 { dev.insert(dev.begin(),head->next->val); //next插入vector节点 head = head->next; //移动节点 } } return dev; } };
class Solution { public: void push(int node) { stack1.push(node);//新进的元素,压入栈1 } int pop() { if(stack2.empty()&&stack1.empty()){ //cout<<“queue is empty” } if(stack2.empty()){ //如果栈2为空 while(!stack1.empty()){ //如果栈1不为空 stack2.push(stack1.top()); //把栈1的全部数据出栈,压入放入栈2 stack1.pop(); } } int n = stack2.top(); stack2.pop(); return n;//栈2出栈 } private: stack<int> stack1; stack<int> stack2; };
n<=39
class Solution { public: int Fibonacci(int n) { int num; int num1 = 1; int num2 = 1; if(n == 0) { return 0 ; } if(n == 1 || n==2) { return 1; } for(int i = 2; i < n; ++i){ num = num1+num2; num1=num2; num2=num; } return num; } };
标签:二维数组 实现 alt empty 用两个栈 递增 else 插入 size
原文地址:http://www.cnblogs.com/yuhanghzsd/p/6808608.html