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 ...
随机推荐
- 270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点
[抄题]: Given a non-empty binary search tree and a target value, find the value in the BST that is clo ...
- Integer和String "+""=="方法的不同
在上面的两个篇博客中,我们看到String和Integer不同的常量池的变现形式 我们再看一个例子: public static void main(String[] args) { // TODO ...
- [LintCode笔记了解一下]41.Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...
- Gym - 100971J ——DFS
Statements Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, ...
- ThinkJS 中的Logic层
第一个为什么需要Logic层: 当在 Action 里处理用户的请求时,经常要先获取用户提交过来的数据,然后对其校验,如果校验没问题后才能进行后续的操作:当参数校验完成后,有时候还要进行权限判断等,这 ...
- Git代码行统计命令集
统计某人的代码提交量,包括增加,删除: git log --author="$(git config --get user.name)" --pretty=tformat: --n ...
- Windows GDI绘图基础知识
一.Windows可以画直线.椭圆线(椭圆圆周上的曲线)和贝塞尔曲线.////////////7 个画线函式是:(1)画直线LineTo BOOL LineTo(HDC hdc,int nXEn ...
- Maven整理笔记のMaven使用
POM 就像Make的Makefile,Ant的build.xml一样,Maven项目的核心是pom.xml.POM(Project Object Model项目对象模型),定义了项目的基本信息,用于 ...
- delphi添加.ocx后缀的控件
在delphi ocx控件的安装步骤:1.第一步: 使用过Activex的人都知道,Activex不注册是不能够被系统识别和使用的,一般安装程序都会自动地把它所使用的Activex控件注册,但如果拿到 ...
- Delphi 调试连接 任意Android手机/平板/盒子(要安装Google USB Driver,并且还有USB的相关许多文章)
Delphi有时候无法连接调试一些手机,解决方案: 1.安装Google USB Driver 2.通过设备管理器查看手机或平板USB的VID,PID 3.修改你的电脑上的android_winusb ...