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

Example
Given "abcdefg" for offset=0, return "abcdefg" for offset=1, return "gabcdef" for offset=2, return "fgabcde" for offset=3, return "efgabcd"

需要注意的是:if (A.length == 0) return new char[0]; 空数组

 public class Solution {
/*
* param A: A string
* param offset: Rotate string with offset.
* return: Rotated string.
*/
public char[] rotateString(char[] A, int offset) {
// wirte your code here
if (A==null || A.length == 0) return new char[0];
String str = new String(A);
offset %= str.length();
if (offset == 0) return str.toCharArray();
String first = str.substring(0, str.length()-offset);
String second = str.substring(str.length()-offset);
String res = second + first;
return res.toCharArray();
}
};

法二(推荐):

 public class Solution {
/**
* @param str: an array of char
* @param offset: an integer
* @return: nothing
*/
public void rotateString(char[] str, int offset) {
// write your code here
if (str==null || str.length==0) return;
offset %= str.length;
if (offset == 0) return;
reverse(str, 0, str.length-1);
reverse(str, 0, offset-1);
reverse(str, offset, str.length-1);
} public void reverse(char[] str, int l, int r) { while (l < r) {
char temp = str[l];
str[l] = str[r];
str[r] = temp;
l++;
r--;
}
}
}

Lintcode: Rotate String的更多相关文章

  1. 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. LintCode Interleaving String

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  9. [LintCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

随机推荐

  1. Delphi Application.MessageBox详解

    引数:1. Text:要显示的讯息2. Caption:讯息视窗的标题列文字3. Flags:讯息旗标     3.1. 可指定讯息视窗上的图示     3.2. 可指定讯息视窗出现的按钮     3 ...

  2. (转)Linux下MatlabCompilerRuntime的安装和使用

    1MCR简介 MCR之前是 Matlab Component Runtime的缩写,后更名为Matlab Compiler Runtime.MCR实际上是一组独立的共享库,也即是常说的动态连接库,所起 ...

  3. 地图API使用文档-以腾讯地图为例

    目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...

  4. C# where(泛型类型约束)

    /*在泛型类型定义中,where 子句用于指定对下列类型的约束:这些类型可用作泛型声明中定义的类型参数的实参. 例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 ...

  5. Android源码剖析之Framework层基础版(窗口、linux、token、Binder)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于Framework,就是应用层底下的控制层,离应用层最近,总想找个机会,写写WindowMang ...

  6. 在VC6.0中编译头文件时产生moc文件

    1.在FileView视图中 右键点击需要产生moc文件的头文件(就是类中包含Q_OBJECT宏,如果没有这个宏就不需要产生moc文件) 2.在右键菜单中选择Setting... 3.选择Custom ...

  7. net use

    net use * \\ipaddr\share "password" /user:"username" /persistent:yes 在运行中输入“\\”+ ...

  8. css模仿表格 居中

    <div class="service_box"><div class="service_list"> <span class=& ...

  9. HighCharts -教程+例子

    Highchart简介:   Highcharts是一款免费开源的纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,  Highcharts目前支持直线图 ...

  10. jQuery实现隔行变色

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...