Chinaunix首页 | 论坛 | 博客
  • 博客访问: 154274
  • 博文数量: 45
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 273
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-28 10:30
文章分类
文章存档

2017年(6)

2016年(3)

2015年(8)

2014年(28)

我的朋友

分类: C/C++

2015-04-02 22:07:28

题目:分布式系统中的RPC请求经常出现乱序情况。
写一个算法讲一个乱序的序列保序输出。例如,假设起始序号是1,对于(1,2,5,8,10,4,3,6,9,7),则输出是
1
2
3 4 5
6
7 8 9 10
上述例子中,3到来的时候,发现4,5已经存在了,因此将(3,4,5)输出。
  1. #include
  2. #include
  3. #include
  4. #include
  5. using namespace std;
  6. class Solution{
  7. public:
  8.     static void printInOrder(vector<int> & num)
  9.     {
  10.         unordered_multiset<int> tmp ;
  11.         int needNum = 1;
  12.         for (auto iter = num.begin(); iter != num.end(); iter++)
  13.         {
  14.             if (*iter != needNum)
  15.             {
  16.                 
  17.                 tmp.insert(*iter);
  18.                 continue;
  19.             }
  20.             tmp.insert(*iter);
  21.             while (true)
  22.             {
  23.                     auto tmpiter = tmp.find(needNum);
  24.                     if (tmpiter == tmp.end())
  25.                         break;
  26.                     else
  27.                     {
  28.                         cout << *tmpiter<<" ";
  29.                         needNum = *tmpiter+1;
  30.                         tmp.erase(tmpiter);
  31.                     }
  32.             }
  33.             cout << endl;
  34.         }
  35.     }
  36. };

  37. int
  38. main()
  39. {
  40.     vector<int> num = {11,10,9,8,7,6,5,4,3,2,1 };
  41.     //vector<int> num;
  42.     Solution::printInOrder(num);
  43.     system("pause");
  44.     return 0;
  45. }


阅读(1150) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~