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. writing concurrent programs

    Computer Systems A Programmer's Perspective Second Edition To this point in our study of computer sy ...

  2. rgb转灰度 RGB To Gray php Adobe RGB (1998) [gamma=2.20]

    <?php /** * Date: 2016/10/24 * Time: 0:52 */ // Gray = (R^2.2 * 0.2973 + G^2.2 * 0.6274 + B^2.2 * ...

  3. data-"mit.edu-Thinking In C++"

    Volume 2 ctrl+s http://web.mit.edu/merolish/ticpp/TicV2.html http://web.mit.edu/merolish/ticpp/TicV2 ...

  4. Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)

    http://blog.csdn.net/yanzi1225627/article/details/21294553 原文

  5. JBPM3.2 TABLE

    http://m.blog.csdn.net/blog/longjie_happy/9343349

  6. ubuntu下安装与卸载qt的方法

    http://blog.csdn.net/huyisu/article/details/24014407 ubuntu下安装与卸载qt的方法 分类: linux 2014-04-18 14:20 18 ...

  7. Bootstrap 进度条媒体对象和 Well 组件

    一.Well 组件 这个组件可以实现简单的嵌入效果. //嵌入效果 <div class="well"> Bootstrap </div> //有 lg 和 ...

  8. library not found for -lPods 的解决办法

    在老项目工程中使用cocoapods,可能会报这个错误:library not found for -lPods . 导致这个错误可能有两个原因,这两个原因在编译过程中都是有蛛丝马迹可循的. 原因1: ...

  9. Android笔记:C memory copy

    socket通讯问题之一: 在c中按字节发送数据  比如设备1状态(1字节)值(1字节)设备2状态(1字节)值(1字节)....这种格式拆分的问题 在c中可以利用struct的 memory copy ...

  10. UITableViewCell 设置圆角

    #import <QuartzCore/QuartzCore.h> QuartzCore.framework [self.commentsCell.layer setMasksToBoun ...