Given a string and an offset, rotate string by offset. (rotate from left to right)

Example

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的更多相关文章

  1. Lintcode: Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

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

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. 8. Rotate String

    8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...

  5. [Algorithm] 8. Rotate String

    Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...

  6. 【Leetcode_easy】796. Rotate String

    problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...

  7. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  8. [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 ...

  9. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

随机推荐

  1. Ibatis学习总结7--SqlMapClient 执行 SQL 语句

    SqlMapCient 类提供了执行所有 mapped statement 的 API.这些方法如下: public int insert(String statementName, Object p ...

  2. Xcode7企业版打包

    今天才发现Xcode7企业账号打包竟然和以前稍微不一样了,一时手残,先把公司服务器以前的ipa包删了,吓得我的小心脏呢 首先选中然后选archive然后点export然后然后选中include man ...

  3. Java设计模式-工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...

  4. POJ1947 Rebuilding Roads

    Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...

  5. SQLServer复制表

    把a的表结构复制到a1表,1=2不复制数据,如果要复制数据,就不要whereselect * into a1 from a where 1=2注意:这种方式不能复制主键.索引等信息如果要全部复制,只能 ...

  6. Linux/Unix System Level Attack、Privilege Escalation(undone)

    目录 . How To Start A System Level Attack . Remote Access Attack . Local Access Attack . After Get Roo ...

  7. OPENSSL编程入门学习

    相关学习资料 http://bbs.pediy.com/showthread.php?t=92649 https://www.openssl.org https://www.google.com.hk ...

  8. linux系统编程----统计一个目录下的普通文件个数

    主要是为了统计linux系统下一个指定目录下面的普通文件个数,运用目录操作的一些函数,配合递归调用来实现该功能. 首先介绍一下函数原型: 打开一个空目录                    DIR ...

  9. 轻量级应用开发之(04)UIScrollView-1

    本文是我在学习OC中的一些经验总结,在学习中总结了常用的Mac技巧,欢迎群友对本文提出意见,如有问题请联系我. 一 什么是UIScrollView 1)移动设备的屏幕大小是极其有限的,因此直接展示在用 ...

  10. android 事件处理概念簇

    1)事件传递函数 2)事件相关: 事件.事件源.事件监听器.MotionEvent 3)坐标系.定位.路由: 4)Window activity view viewGroup