当前位置:网站首页>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;;
}
边栏推荐
- Sqldeveloper tools quick start
- Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
- 外文文献查找技巧方法有哪些
- DICOM learning materials collection
- 装备制造业的变革时代,SCM供应链管理系统如何赋能装备制造企业转型升级
- If food manufacturing enterprises want to realize intelligent and collaborative supplier management, it is enough to choose SRM supplier system
- Xiaobai, which securities firm is the best and safest to open an account
- 领导抢功劳,我改个变量名让他下岗了
- 双屏协作效率翻倍 灵耀X双屏Pro引领双屏科技新潮流
- DevSecOps,让速度和安全兼顾
猜你喜欢

Vs add settings for author information and time information

OSPF and mGRE experiments

数商云:引领化工业态数字升级,看摩贝如何快速打通全场景互融互通

Jintuo shares listed on the Shanghai Stock Exchange: the market value of 2.6 billion Zhang Dong family business has a strong color

Where is the foreign literature needed to write the graduation thesis?

How do college students apply for utility model patents?

【五分钟Paper】基于参数化动作空间的强化学习

2. Add two numbers

Write a summary, want to use a reliable software to sort out documents, is there any recommendation?
![[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial](/img/fe/3baec8f6def4e569842a683fcb9fd1.png)
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
随机推荐
Abbkine EliKine人甲胎蛋白(AFP)ELISA定量试剂盒操作方法
C# 给Word每一页设置不同文字水印
什么是传输层协议TCP/UDP???
晶品特装递交注册:第一季营收降80% 陈波控制68.5%股权
MySQL builds master-slave replication
Practical purchasing skills, purchasing methods of five bottleneck materials
R语言ggplot2可视化:使用ggpubr包的ggballoonplot函数可视化气球图(可视化由两个分类变量组成的列联表)、配置guides函数中的size参数指定不显示数据点大小的图例
Parallel d-Pipeline: A Cuckoo Hashing Implementation for Increased Throughput论文总结
The R language uses the histogram function in the lattice package to visualize the histogram (histogram plot), the col parameter to customize the fill color, and the type parameter to customize the hi
广州地铁十三号线二期全线土建已完成53%,预计明年开通
How to find the translation of foreign literature for undergraduate graduation thesis?
R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(设置min.segment.length参数为0为每个数据点的标签添加线段)
Solve the problem that typora pictures cannot be displayed
Google tries to introduce password strength indicator for chromeos to improve online security
持续集成(二)Jenkins基本使用介绍
如何查找国内各大学本科学位论文?
Remote desktop on Jetson nano
Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
Write a summary, want to use a reliable software to sort out documents, is there any recommendation?
【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程