LeetCode - 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: A and B will have length at most 100.
class Solution {
public boolean rotateString(String A, String B) {
if(A == null || B == null || A.length() != B.length()){
return false;
}
if(A.length() == 0 && B.length() == 0){
return true;
} int n = A.length();
//轮数
for(int i = 0; i< n; i++){
boolean find = true;
//每一个元素
for(int j = 0; j < n; j++){
int a = A.charAt(j);
int newIndex = (n - i + j) % n;
int b = B.charAt(newIndex);
if(a != b){
find = false;
break;
}
}
if(find){
return true;
}
}
return false;
}
}
LeetCode - Rotate String的更多相关文章
- [LeetCode] 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 ...
- 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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- 8. Rotate String
8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
随机推荐
- Unity中物体碰撞后去掉相互之间的反弹力
最近自制了一个的角色控制器(没有重力的角色)时发现,角色碰撞到墙壁之后会有一个小小的反弹力导致角色有一个微弱的反弹位移,这样给人一种不好的感觉.研究了一下,除了限制坐标轴( Rigidbody---C ...
- OOP⑺
1.多态和instanceof 都是去买东西,但是根据我们给别人金额的不同,得到不同的结果!!!! 生活中的多态! 操作是否一致? 一致! 都是买东西! 什么不一样?? 01.消费金额不一样 02.因 ...
- 【阅读笔记】《C程序员 从校园到职场》第四章 变量和函数
参考: Contents: 一.数据类型(对基本数据类型进行重定义——规范化) 二.变量和函数 (命名规则,注意事项) 三.静态变量及其使用 一.数据类型(对基本数据类型进行重定义——规范化) 1. ...
- 前端之Bootstrap框架
一.Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局, ...
- python 定义class时的内置方法
__contains__():对类实例使用in ,not in操作时调用 class A(object): def __init__(self,num): self.num=num def __con ...
- vector容器的注意事项
1.容器是指对象的集合,每一个元素都是一个对象,并且对象的类型相同.可以使用索引去访问容器中的对象. 2.由于容器中存放的是对象,所以引用无法成为vector的成员. 3.容器的初始化,与string ...
- 十八. Python基础(18)常用模块
十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...
- java基础入门系列1
学java之前先了解下window系统的的一些常识: #常用windows快捷键 ctrl+x:剪切 ctrl+z:撤销 ctrl+A:全选 ctrl+V:黏贴 windows+d:回到桌面 Wind ...
- Android开发---如何操作资源目录中的资源文件
效果图: 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...
- springMvc 简单搭建
1.pom.xml 依赖引入 2.配置web.xml 3.配置 springMvc.xml 4.配置 logback.xml 5.编写 controller 测试 1.pom.xml 依赖引入 < ...