Ai
1 Star 0 Fork 0

ashley/Algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
solo_stack.cpp 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
ashley 提交于 2021-09-21 15:14 +08:00 . DST_2
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N = 100010;
int a[N];
int stk[N] , hh = -1;
int n;
void baoli_solo(int a[],int n){
cout<<"-1 ";
for(int i = 1 ; i < n ; ++i){
int j = i - 1;
while( j >= 0){
if(a[j] < a[i]){
printf("%d ", a[j]);
break;
}
--j;
}
///////注意必须是严格小于0,才代表没找到比a[i]小的数
if(j < 0) printf("-1 ");
}
}
void res_solo(int a[] , int x){
// stk[++hh] = -1;
// cout<<stk[hh--]<<" ";
// cout<<-1<<endl;
vector<int> res;
for(int i = 0 ; i < n ;++i){
while(hh >= 0 && stk[hh] >= a[i]) {
cout<<"出栈元素: "<<stk[hh]<<endl;
--hh;
}
if(hh >= 0) {
res.push_back(stk[hh]);
cout<<a[i]<<"左边第一个小于的数:"<<stk[hh]<<endl;;
}
else {
res.push_back(-1);
// cout<<"-1 ";
}
// int j = i - 1;
// if(hh < 0 || a[i] > a[i - 1]) {////栈空则入栈
// if(hh < 0 || a[i] > stk[hh]) {
stk[++hh] = a[i];
cout<<"进栈元素: "<<stk[hh]<<endl;
// }
/////////如果 出现重复元素
// if(hh >= 0) cout<<stk[hh]<<" ";
// else cout<<"-1 ";
}
for(auto i : res){
cout<<i<<" ";
}
printf("\n");
}
void yxc(){
///将stk按照从hh = 0 表示没有元素
for(int i = 0 ; i < n ;++i){
int xi;
cin>>xi;
////出栈条件
while(hh && stk[hh] >= xi) --hh;
if(hh) cout<<stk[hh]<<" ";
else cout<<"-1 ";
stk[++hh] = xi;/////必入栈,栈顶不比xi小,就是空
}
}
// 5
// 3 4 2 7 5
// 6
// 3 3 4 2 7 5
int main(){
scanf("%d" , &n);
for(int i = 0 ; i < n ;++i) scanf("%d" , &a[i]);
// baoli_solo(a , n);
// cout<<endl;
res_solo(a , n);
printf("C 艹 mother fucker\n");
system("pause");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ashleyZZ/algorithm.git
git@gitee.com:ashleyZZ/algorithm.git
ashleyZZ
algorithm
Algorithm
master

搜索帮助