LeetCode题解之Rotate String

1、题目描述

2、问题分析

直接旋转字符串A,然后做比较即可。

3、代码

 1  bool rotateString(string A, string B) {
 2         if( A.size() != B.size() )
 3             return false;
 4         if( A.empty() && B.empty() )
 5             return true;
 6         int i = 0 ;
 7         while( i < A.size() ){
 8             char c = A[0] ;
 9             A += c;
10             A.erase( A.begin() );
11             if( A == B )
12                 return true;
13             ++i;
14         }
15         return false;
16
17     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/9310730.html

时间: 2024-10-13 08:30:45

LeetCode题解之Rotate String的相关文章

LeetCode算法题-Rotate String(Java实现)

这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是796).给定两个字符串A和B,在A上进行移位操作,规则是将A最左边的字符移动到最右边去.例如,如果A ='abcde',那么在A上移位一次后,它将是'bcdea'.当且仅当A在A上移位一定次数后可以变为B时返回True.

leetCode题解之寻找string中最后一个word的长度

1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 'a','akkk'. 2.问题分析 从后向前扫描,如果string是以空格'  '结尾的,就不用计数,现将空格最后的空格排除掉,直到找到最后一个出现的字符.从最后一个出现的字符开始计数,一直到下一次出现空格' '. 特殊情况是 输入的字符串是空的,先排除掉. 3.代码 1 1 int lengthOfLastWord(string s) { 2 3 if(s.size() == 0) // 排除字符

[LeetCode 题解]: Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 题意: 翻转链表. 思路: 首先需要读懂题意.题目的描述有问题,应该是将链表的最后k个元素移动到链表的头部. 这道题的本质就是寻找链表的

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode 题解: Length of Last Word

leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

&lt;LeetCode OJ&gt; 48. Rotate Image

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? Subscribe to see which companies asked this