Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right)
Given "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
分析:
利用 (A^TB^T)^T = BA
public class Solution {
/**
* @param str: an array of char
* @param offset: an integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
if (str == null || str.length <= ) return;
offset = offset % str.length;
if (offset == ) return;
int p = str.length - - offset;
swapFromIToJ(str, , p);
swapFromIToJ(str, p + , str.length - );
swapFromIToJ(str, , str.length - );
}
public void swapFromIToJ(char[] str, int i, int j) {
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
}
Rotate String的更多相关文章
- Lintcode: Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- 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) ...
- 8. Rotate String
8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...
- [Algorithm] 8. Rotate String
Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- [Swift]LeetCode796. 旋转字符串 | 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 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
随机推荐
- HashMap的一些用法
1.HashMap的遍历 //这个是通过 迭代器iterator 来实现 HashMap的遍历 Iterator iterator=hashMap.keySet().iterator(); while ...
- DELL R720系统内存指南
该文章摘自于:http://www.dell.com/support/article/cn/zh/cndhs1/SLN153646/zh#issue3,仅供个人作为笔记使用 PowerEdge R72 ...
- poj3107 树形dp
好久没更了.前段时间去ec-final,实力水一波,混了个铜,虽然很弱,但是可以算是对之前一段时间的回报吧. 现在每天忙着复习,逃课太多,啥都不会...不想挂科啊!!Orz... 题意(简化):警察想 ...
- Ajax 的缺点
1.ajax干掉了back按钮,即对浏览器后退机制的破坏.后退按钮是一个标准的web站点的重要功能,但是它没法和js进行很好的合作.这是ajax所带来的一个比较严重的问题,因为用户往往是希望能够通过后 ...
- 【Gym 100015A】Another Rock-Paper-Scissors Problem
题 题意 Sonny出石头剪刀布的猜拳策略是 先出R,然后每连续两段都是打败前一段的出拳, 现在问你第n回合打败他要出什么. 分析 他的策略 R P S PSR SRP PSRSRPRPS SRPR ...
- 自动打包iOS项目
基于Lexrus的博文iOS-makefile,本文对自动打包涉及到的操作步骤以及理论基础进行了适当的补充. 请在阅读本文前先阅读<iOS makefile>.文章地址:http: ...
- POJ1703Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37722 Accepted: ...
- view视图文件中的input等输入框必须含有name属性,不然控制器里的动作formCollection是没有值的
view视图文件中的input等输入框必须含有name属性,不然控制器里的动作formCollection是没有值的,就是没有name属性,后台获取不到值
- C# 参考之方法参数关键字:params、ref及out
如果在为方法声明参数时未使用 ref 或 out,则该参数可以具有关联的值.可以在方法中更改该值,但当控制传递回调用过程时,不会保留更改的值.通过使用方法参数关键字,可以更改这种行为. params ...
- js校验表单后提交表单的三种方法总结
第一种: 复制代码 代码如下: <script type="text/javascript"> function check(form) { if(fo ...