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 ...
随机推荐
- markdown语法模板
(GitHub-Flavored) Markdown Editor Basic useful feature list: Ctrl+S / Cmd+S to save the file Ctrl+Sh ...
- 查看json数据更新情况
#! python3 # -*- coding:utf8 -*- #主要为读取excel中接口地址,打开网页爬取url页面中数据,解析json,检查是否符合逻辑(正常) import requests ...
- 四. Python基础(4)--语法
四. Python基础(4)--语法 1 ● 比较几种实现循环的代码 i = 1 sum = 0 while i <= 10: # 循环10-1+1=10次 sum += i i ...
- aspnet core 2.0 发布之后没有 views文件夹
在项目文件里面 增加这个节点: MvcRazorCompileOnPublish 设置为false 是会发布views <PropertyGroup> <PackageTargetF ...
- 理解AXI Quad Serial Peripheral Interface(SPI) IP核
reference : PG153-AXI Quad SPI v3.2 LogiCORE IP Product Guide.pdf 在使用MicroBlaze过程中,调用了此IP,所以有必须仔细学 ...
- angular2架构详解
参考 http://codin.im/2016/09/18/angular2-architecture-intro/ http://www.tuicool.com/articles/EvEZjmZ ...
- [转] ajax方法
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
- webview定位 & native和webview切换
前言:现在的app大都是混合式的native+webview,对于native可以直接用uiautomator定位然后操作元素,但是web就定位不到了 一.先看看使用native定位的 二.定位web ...
- 20165326 java第三周学习笔记
纸质学习笔记 代码托管
- L295 how to turn down a job but keep a good relationship with the hiring manager
Let’s say you’re on the hunt for a new job. Three interviews in, you realize it’s not the place for ...