数据结构练习
Matrix练习
week1
T1:输出螺旋数组
1 |
|
T2:压缩技术续集
1 |
|
T3:丑数
Matrix对应力扣题目:丑数2
洛谷有进阶版,自定义素数集合的丑数:丑数
week2
T4:奶牛转向
差分数组翻转区间,用一个差分数组记录翻转状态,读到对应位置就读取对应位置的翻转状态然后进行翻转。
此题希望快速对“牛的翻转次数”数组进行加减,故对此数组进行差分操作。
随后在遍历的过程中不断对差分数组累加,相当于计算第i头牛的翻转次数。
注意改变差分数组的值时,末尾元素的坐标是第一个不受影响的元素。
1 |
|
week5
T3:移除K位数字
402.移除k位数字1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution {
public:
string removeKdigits(string num, int k) {
//维护左侧单调递增性
vector<int>a;
a.push_back(0);
for(auto&c:num){
while(c-'0'<a.back()&&k){
k--;
a.pop_back();
}
a.push_back(c-'0');
}
while(k--){
a.pop_back();
}
string res;
int flag=0;
for(auto& n:a){
if(n!=0) flag=1;
if(flag){
res+=n+'0';
}
}
if(res == "")
return "0";
return res;
}
};
T4:合法的出栈序列
类似题目:洛谷【深基15.习9】验证栈序列1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using namespace std;
int ru_zhan[100005];
int chu_zhan[100005];
int main(){
int q;cin>>q;
while(q--){
int n;cin>>n;
for(int i=1;i<=n;i++)
cin>>ru_zhan[i];
for(int i=1;i<=n;i++)
cin>>chu_zhan[i];
vector<int> s;
for(int i=1,j=1;i<=n;i++){
while(s.size()==0||(s.back()!=chu_zhan[i]&&j<=n)){
s.push_back(ru_zhan[j]);
j++;
}
if(s.back()==chu_zhan[i]){
s.pop_back();
}
}
if(s.size()){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}
week6
T4:滑动窗口
1 |
|





