NGE

来源:互联网 发布:幼儿园早教软件 编辑:程序博客网 时间:2024/06/08 02:37
#include <iostream>#include <stack>#include <vector>using namespace std;int findNGE(vector<int> &arr, int length){ stack<int> s; s.push(arr[0]);  for (int i = 1; i < length; ++i) { int next = arr[i]; int top = s.top(); while (s.size() > 0 && top < next) { cout << top << "->" << next << endl; s.pop(); if (s.size() > 0) { top = s.top(); } } s.push(next); } while (s.size() > 0) { int top = s.top(); cout << top << "->" << -1 << endl; s.pop(); }return 0;}int main(){vector<int> arr = { 11, 13, 10, 5, 12, 21, 3 };findNGE(arr, arr.size());return 0;}

0 0
原创粉丝点击