leetcode-796-Rotate String
题目描述:
We are given two strings, A and B.
A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.
Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true Example 2:
Input: A = 'abcde', B = 'abced'
Output: false
Note:
AandBwill have length at most100.
要完成的函数:
bool rotateString(string A, string B)
说明:
1、给定两个字符串A和B,要求判断这两个字符串,可不可以通过不断地把A中的首字母搬到末尾去,最终把A变成B。比如A为abcde,B为cdeab,就是可以通过前述操作把A变成B的。
2、明白题意,笔者最开始觉得这种结构很熟悉,应该是队列的操作,可以定义一个队列,把A中的字母塞到队列中去,然后不断地取出首位,插入到末位,判断是不是能形成B字符串。
不过这样子还要定义一个队列,操作略显麻烦,直觉这样做并不是最简便的方法。
之后想到其实找到B首字母,比如上面给的例子,A是abcde,B是cdeab,B中首字母是c,那么找到c在A中的位置j,然后逐位比较A[j]和B[i](i从0开始)是否相等,这是A中后半部分的比较。
然后再比较A中前半部分是否与B中剩余部分相等。我们已知了j的位置,这些操作都是非常容易的。
这道题就可以做出来了。
不过,当碰到B中首字母在A中多次出现,而且首次出现还匹配不上,得第二次才匹配上的情况,比如A是abcdecdf,B是cdfabcde。
如果只是一次搜索,c在A中位置是第三位,这时匹配不成功。
但如果c在A中位置是倒数第三位那里,这时候的匹配就是成功的。
所以之前的做法就得在外面再加多个循环,一直迭代,直到搜索到能匹配的位置。
代码如下:(附详解)
bool rotateString(string A, string B)
{
int i=0,j=0,s1=A.size(),s2=B.size();
if(s1!=s2)//边界条件
return false;
if(s1==0)//边界条件,比如A和B都是空字符串
return true;
bool flag;
while(j<s1)//j用于表示B的首字母在A中的哪个位置
{
while(j<s1)//一直搜索,直到找到B中首字母在A中的位置
{
if(B[0]==A[j])
break;
j++;
}
flag=1;
i=0;
int t1=1,t2=j+1;//t1表示B中尝试匹配的起始位置,t2表示A中尝试匹配的起始位置
while(t2<s1)
{
if(B[t1]!=A[t2])
{
flag=0;
break;
}
t1++;
t2++;
}
if(flag==1)//如果上述匹配能成功,那么进行剩余部分的匹配
{
while(t1<s1)
{
if(B[t1]!=A[i])
{
flag=0;
break;
}
t1++;
i++;
}
}
if(flag==1)//如果两个部分都匹配成功了,那么返回true
return true;
j++;//如果没有匹配成功,那么j++,搜索B中首字母在A中的下一个出现位置
}
return false;//如果搜索到A的末尾,每一次都不能匹配成功,那么返回false
}
上述代码实测3ms,beats 97.87% of cpp submissions。
leetcode-796-Rotate String的更多相关文章
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
- 【LeetCode】796. Rotate String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 796. Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- 796. Rotate String旋转字符串
[抄题]: We are given two strings, A and B. A shift on A consists of taking string A and moving the lef ...
- 796. Rotate String
class Solution { public: bool rotateString(string A, string B) { if(A.length()==B.length()&& ...
- [LeetCode] 796. Rotate String_Easy **KMP
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LC] 796. Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
随机推荐
- Anaconda3 ubuntu18.04
luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/M ...
- php static 变量声明
<?phpfunction test($key){ static $array = array(); /* 静态变量是只存在于函数作用域中的变量,注释:执行后这种变量不会丢失(下次调用这个函数 ...
- linux下安装或升级GCC4.8.2,以支持C++11标准[转]
在编译kenlm的时候需要安装gcc, 然后还需要安装g++. g++安装命令:sudo apt-get install g++ ----------------------以下为网上转载内容,加上自 ...
- RAW编程接口
LWIP移植好之后,就要使用它提供的API接口来编写程序.
- NArrange 配置与使用
1. 在VS中设置一下就可以用了 2.It is also recommended to setup a restore command using the same parameters, repl ...
- 使用VM克隆CentOS后,更改网络配置
在使用VM克隆之后,遇到一件非常郁闷的事,就算我使用‘setup’命令,修改了我的网络配置,依然无法查询到我配置的网卡,也无法开启网卡. 经过百度等一系列手段,总结如下套路--PS:朋友称之为“破釜沉 ...
- AC620教程 第十五节 8位7段数码管驱动设计与验证
本章导读 电子系统中常用的显示设备有数码管.LCD液晶以及VGA显示器等.其中数码管又可分为段式显示(7段.米字型等)以及点阵显示(8*8.16*16等),LCD液晶的应用可以分为字符式液晶(1602 ...
- 14、Semantic-UI之菜单样式
14.1 基础菜单样式 在Semantic-UI中使用class="ui menu". 示例:定义基础菜单样式 <div class="ui menu" ...
- Dubbo RPC源码解读
https://yq.aliyun.com/articles/272405#27 本文代码摘录的时候,将一些与本流程无关的内容去掉了,如有需要请看源码. 一.闲言碎语 使用rpc框架已经多年了,虽然之 ...
- [示例] Drag And Drop for FireMonkey (Win & macOS)
源码下载: https://github.com/OneChen/DragAndDrop 效果: