day1
作者:互联网
1.剑指 Offer 09. 用两个栈实现队列
1 class CQueue { 2 public: 3 stack<int> s1,s2; 4 CQueue() { 5 6 } 7 8 void copy(stack<int>& a,stack<int>& b){ 9 while(a.size()){ 10 int tmp = a.top(); 11 a.pop(); 12 b.push(tmp); 13 } 14 } 15 16 void appendTail(int value) { 17 s1.push(value); 18 } 19 20 int deleteHead() { 21 int result; 22 if(s2.size()){ 23 result = s2.top(); 24 s2.pop(); 25 } 26 else{ 27 if(s1.size()){ 28 copy(s1,s2); 29 result = s2.top(); 30 s2.pop(); 31 } 32 else 33 result = -1; 34 } 35 return result; 36 } 37 }; 38 39 /** 40 * Your CQueue object will be instantiated and called as such: 41 * CQueue* obj = new CQueue(); 42 * obj->appendTail(value); 43 * int param_2 = obj->deleteHead(); 44 */
2.剑指 Offer 30. 包含 min 函数的栈
1 class MinStack { 2 public: 3 /** initialize your data structure here. */ 4 stack<int> stk1,stk2; 5 MinStack() { 6 7 } 8 9 void push(int x) { 10 stk1.push(x); 11 if(stk2.empty() || stk2.top() >= x) 12 stk2.push(x); 13 } 14 15 void pop() { 16 if(stk1.top() == stk2.top()) 17 stk2.pop(); 18 stk1.pop(); 19 } 20 21 int top() { 22 return stk1.top(); 23 } 24 25 int min() { 26 return stk2.top(); 27 } 28 }; 29 30 /** 31 * Your MinStack object will be instantiated and called as such: 32 * MinStack* obj = new MinStack(); 33 * obj->push(x); 34 * obj->pop(); 35 * int param_3 = obj->top(); 36 * int param_4 = obj->min(); 37 */
标签:obj,int,s2,top,pop,day1,stk2 来源: https://www.cnblogs.com/balabalabubalabala/p/16411923.html