Acwing交换相邻元素
思路
通过01序列将序列分成多段,每段元素最大值必须为下标,只要不符合返回NO
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n;
int w[N];
char s[N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
scanf("%s", s + 1);
int t = w[1];
bool flag = true;
for (int i = 1; i < n; i ++ )
{
t = max(t, w[i]);
if (s[i] == '0' && t != i)
flag = false;
}
if (flag) puts("YES");
else puts("NO");
return 0;
}
Leetcode每日一题:表序列号
出模取余法就可以了
class Solution {
public:
int titleToNumber(string str) {
int res = 0;
for (auto &c : str)
{
auto t = c - 'A' + 1;
res = res * 26 + t;
}
return res;
}
};
本文介绍了一种通过01序列检查序列是否可通过交换相邻元素变得有序的方法,并提供了一个LeetCode题目中字母序列转换为数字的解决方案。
1618

被折叠的 条评论
为什么被折叠?



