careercup3.6
2012-05-04 15:53
176 查看
把全局的排序做成每个子问题的排序,每次出来的一个数都会排好序。
/*Implement a MyQueue class which implements a queue using two stacks */ #include <iostream> using namespace std; class Node{ public: int data; Node* next; Node(){this->next = 0;} Node(int a, Node* n=0):data(a),next(n){} }; class Stack{ public: Node* top; Stack():top(0){ } Stack(int ar[], int l){ top = 0; for(int i = 0; i<l; i++) this->push(ar[i]); } ~Stack(); int pop(); void push(int); void print()const{ Node* p = top; while(p) { cout<<p->data<<" "; p = p->next; } cout<<endl; } }; Stack::~Stack(){ while(top){ this->pop(); } } int Stack::pop(){ if(!top) return 0; Node* p = top; top = top->next; int k = p->data; delete p; return k; } void Stack::push(int k){ Node* p = top; top = new Node(k,p); } Stack* sort(Stack s){ Stack* temp = new Stack(); while(s.top){ int k = s.pop(); while(temp->top && temp->top->data >k) s.push(temp->pop()); temp->push(k); } return temp; } int main(){ int ar[]={16,423,5,67,8,9,45,78,777,33,12,11,76,4,43,7}; Stack ll(ar,16); sort(ll)->print(); }
相关文章推荐
- CareerCup Longest Common Substring Longest Common Subsequence
- CareerCup After the BIOS performs a successful power-on-self-test, describe everything
- CareerCup the Lexicographically Smallest Permutation
- Careercup - Facebook面试题 - 5733320654585856
- CareerCup Facebook Number of sstrings
- CareerCup 4.1 & 4.3
- Recursion 在数组中寻找神奇下标 @CareerCup
- Sorting&Searching 旋转数组二分法查找 @CareerCup
- Moderate 查单词 @CareerCup
- Hard 单词变型成另一个单词 @CareerCup
- CareerCup Sort an array in a special way
- CareerCup Fork Problem
- Arrays_Strings 把字符串中所有的空格替换为%20 @CareerCup
- Stack_Queue 栈实现min函数 @CareerCup
- Tree_Graph 判断是否平衡二叉树 @CareerCup
- CareerCup How would you implement a secondary sorting
- CareerCup chapter 3 Stacks and Queues
- CareerCup Facebook Total number of substring palindrome
- careercup-C和C++ 13.4
- Careercup面试题