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 ...
随机推荐
- Openssl verify命令
一.简介 verify命令对证书的有效性进行验证,verify 指令会沿着证书链一直向上验证,直到一个自签名的CA 二.语法 openssl verify [-CApath directory] [- ...
- myeclipse10.X以上的破解方法
破解补丁下载地址:http://pan.baidu.com/s/1dDzVP3z 本文使用的破解补丁对MyEclipse Standard/ Professional/ Blue/ Spring的10 ...
- [C++] Lvalue and Rvalue Reference
Lvalue and Rvalue Reference int a = 10;// a is in stack int& ra = a; // 左值引用 int* && pa ...
- zigbee广播通信原理
广播:可以理解成,发送模块发出数据,这个网络里的所有节点模块都可以拿到这个数据. 实验:终端模块以广播的形式发送出去,让协调器和路由器模块作为接收器收到数据并显示出来! 协调器模块作为接收模块: 和单 ...
- SQL日期跟时间值序列
与数据操作相关的场景要生成日期和时间序列,序列的范围是从输入值@start到@end,且具有一定的时间间隔.这样的场景包括填充数据仓库中的时间维度.应用程序的运行时间安排以及其他.可以借助http:/ ...
- explain分析sql效率
Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...
- post上传文件限制--另一种解决途径
问题:项目之前的上传功能是没有问题的,但是今天同样的代码上传一个压缩包的时候出现了问题,报的是struts.xml的错,说是找不到返回的映射, 问题截图: 很奇怪的问题,之前都没问题的,仔细对比后发现 ...
- GridView中合并单元格
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- Linq to SQL 中将数字转换为字符串
使用LINQ to Entities中的SqlFunctions调用数据库中的函数 添加引用System.Data.Entity 引用命名空间 using System.Data.Objects.Sq ...
- C#串口数据互通小程序
主要功能: 所编写的程序需将串口1.串口2数据互通,即:串口1接收到数据的同时将数据通过串口2发出,串口2接收到数据的同时将数据通过串口1发出. 并根据需要由指定串口发送或获取数据. 代码如下: us ...