当前位置:网站首页>777. Exchange adjacent characters in LR string
777. Exchange adjacent characters in LR string
2022-07-26 15:19:00 【Mr Gao】
777. stay LR Exchange adjacent characters in a string
In a ‘L’ , ‘R’ and ‘X’ A string of three characters ( for example "RXXLRXRXL") In the mobile operation . One move at a time refers to the use of one "LX" Replacing a "XL", Or use a "XR" Replacing a "RX". Now let's give the starting string start And the ending string end, Please write the code , If and only if there is a series of move operations to make start It can be converted into end when , return True.
Example :
Input : start = “RXXLRXRXL”, end = “XRLXXRRLX”
Output : True
explain :
We can use the following steps to start convert to end:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX
The solution code is as follows :
void swap(char *s,int i1,int i2){
char p=s[i1];
s[i1]=s[i2];
s[i2]=p;
}
int find_R(char *s){
int i=0;
while(s[i]!='\0'){
if(s[i]=='X'){
i++;
}
if(s[i]!='X'){
if(s[i]=='R'){
return i;
}
return -1;
}
}
return -1;
}
int find_X(char *s){
int i=0;
while(s[i]!='\0'){
if(s[i]=='L'){
i++;
}
if(s[i]!='L'){
if(s[i]=='X'){
return i;
}
return -1;
}
}
return -1;
}
bool canTransform(char * start, char * end){
int s=0,e=0,i=0;
int sx=0;
while(start[s]!='\0'){
// printf("--%s %s ",start,end);
if(start[s]=='R'){
if(end[e]=='R'){
s++;
e++;
}
else{
if(end[e]!='X'){
return false;
}
int i=find_R(end+e+1);
if(i==-1)
return false;
swap(end,e,e+i+1);
}
}
if(start[s]=='X'){
if(end[s]=='X'){
s++;
e++;
}
else{
if(end[e]!='L'){
return false;
}
int i=find_X(end+e+1);
if(i==-1)
return false;
swap(end,e,e+i+1);
}
}
if(start[s]=='L'){
if(end[e]=='L'){
e++;
s++;
}
else{
return false;
}
}
}
printf("%s %s",start,end);
return true;;
}
边栏推荐
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用fitted函数计算出模型的拟合的y值(响应值)向量
- Sexy prime number (summer vacation daily question 1)
- 【LeetCode】33、 搜索旋转排序数组
- Cve-2022-33891 vulnerability recurrence
- R语言使用lm函数构建带交互项的多元回归模型、使用step函数构建逐步回归模型筛选预测变量的最佳子集(step regression)
- 大学论文格式怎么写?
- Driver development environment
- 【留点代码】将transformer运用到目标检测上来,通过debug了解模型的模型运算流程
- 李宏毅《机器学习》丨3. Gradient Descent(梯度下降)
- DevSecOps,让速度和安全兼顾
猜你喜欢

【LeetCode每日一题】——121.买卖股票的最佳时机

81.(cesium之家)cesium修改灰色背景(默认蓝色)

【基础】动态链接库/静态链接库的区别

Parallel d-Pipeline: A Cuckoo Hashing Implementation for Increased Throughput论文总结

OSPF and mGRE experiments

【LeetCode】33、 搜索旋转排序数组

driver开发环境

哪里有写毕业论文需要的外文文献?

QCF for deep packet inspection paper summary

Deep Packet Inspection Using Cuckoo Filter论文总结
随机推荐
R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
Use of oscilloscope
MYSQL 命令大全
Parallel d-Pipeline: A Cuckoo Hashing Implementation for Increased Throughput论文总结
R语言ggplot2可视化:可视化折线图、使用aes函数中的group参数为不同分组可视化折线图
OSS deletes all files two days before the current time
Li Hongyi, machine learning 3. Gradient descent
How to find undergraduate dissertations of domestic universities?
QCF for deep packet inspection paper summary
What are the skills and methods of searching foreign literature
Which software must be used by scientific researchers to read literature?
7. In JS [] = =! [] Why is it true?
Devsecops, speed and security
MySQL builds master-slave replication
R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(设置min.segment.length参数为0为每个数据点的标签添加线段)
益方生物上市首日跌16%:公司市值88亿 高瓴与礼来是股东
The most detailed patent application tutorial, teaching you how to apply for a patent
外文文献查找技巧方法有哪些
Driver development environment
DICOM learning materials collection