题解 | 从单向链表中删除指定值的节点
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9?channelPut=w25springcamp
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double d; typedef long double ld; const int N=1e4+10; const int mod=1e9+7; int a[N]; void op(int x,int p,int k){//k为数组a的当前长度 for(int i=k;i>p+1;i--){ a[i]=a[i-1]; }//给x腾位置 a[p+1]=x; return; }//将数x插入位置p后面 int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n; cin>>n; cin>>a[0]; int k=1; for(int i=1;i<n;i++){ int aa,b; cin>>aa>>b; for(int j=0;j<k;j++){ if(b==a[j]){ op(aa,j,k);k++;//插入操作 break; } } } int kk; cin>>kk; for(int i=0;i<n;i++){ if(a[i]==kk){ continue;//直接跳过值为k的数,约等于删除了嘻嘻(๑¯◡¯๑) } else{ cout<<a[i]<<" "; } } return 0; }
本题作者没有熟练掌握链表而选择用数组投机取巧,很惊喜地过了,故发此贴*:ஐ٩(๑´ᵕ`)۶ஐ:*。
大概思路就是按照题目要求直接模拟,由于本题主要通过插入方式来构建数组,而链表在插入方式上时间复杂度比数组小,因此用链表来写时间上会优于数组。
#牛客春招刷题训练营#+牛客春招刷题训练营贴子链接
#牛客春招刷题训练营#