标签:ack code pre push 不可 span bool stack while
1 class MyQueue 2 { 3 stack<int> s1,s2; 4 public: 5 MyQueue() {} 6 7 void push(int x) 8 { 9 s1.push(x); 10 } 11 12 int pop() 13 { 14 while(!s1.empty()) 15 { 16 s2.push(s1.top()); 17 s1.pop(); 18 } 19 int temp = s2.top(); 20 s2.pop(); 21 22 while(!s2.empty())//这里不可以用swap(s1,s2) 23 { 24 s1.push(s2.top()); 25 s2.pop(); 26 } 27 return temp; 28 } 29 30 int peek() 31 { 32 while(!s1.empty()) 33 { 34 s2.push(s1.top()); 35 s1.pop(); 36 } 37 int temp = s2.top(); 38 39 while(!s2.empty()) 40 { 41 s1.push(s2.top()); 42 s2.pop(); 43 } 44 return temp; 45 } 46 47 bool empty() 48 { 49 return s1.empty(); 50 } 51 };
标签:ack code pre push 不可 span bool stack while
原文地址:https://www.cnblogs.com/yuhong1103/p/12682998.html