1. Given a string and an offset, rotate string by offset. (rotate from left to right)
  2.  
  3. Example
  4. Given "abcdefg"
  5.  
  6. for offset=0, return "abcdefg"
  7.  
  8. for offset=1, return "gabcdef"
  9.  
  10. for offset=2, return "fgabcde"
  11.  
  12. for offset=3, return "efgabcd"

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

  1. public class Solution {
  2. /*
  3. * param A: A string
  4. * param offset: Rotate string with offset.
  5. * return: Rotated string.
  6. */
  7. public char[] rotateString(char[] A, int offset) {
  8. // wirte your code here
  9. if (A==null || A.length == 0) return new char[0];
  10. String str = new String(A);
  11. offset %= str.length();
  12. if (offset == 0) return str.toCharArray();
  13. String first = str.substring(0, str.length()-offset);
  14. String second = str.substring(str.length()-offset);
  15. String res = second + first;
  16. return res.toCharArray();
  17. }
  18. };

法二(推荐):

  1. public class Solution {
  2. /**
  3. * @param str: an array of char
  4. * @param offset: an integer
  5. * @return: nothing
  6. */
  7. public void rotateString(char[] str, int offset) {
  8. // write your code here
  9. if (str==null || str.length==0) return;
  10. offset %= str.length;
  11. if (offset == 0) return;
  12. reverse(str, 0, str.length-1);
  13. reverse(str, 0, offset-1);
  14. reverse(str, offset, str.length-1);
  15. }
  16.  
  17. public void reverse(char[] str, int l, int r) {
  18.  
  19. while (l < r) {
  20. char temp = str[l];
  21. str[l] = str[r];
  22. str[r] = temp;
  23. l++;
  24. r--;
  25. }
  26. }
  27. }

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. Ubuntu 14.04 为 root 帐号开启 SSH 登录

    1. 修改 root 密码 sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config : xxx@ubuntu14:~$ su ...

  2. wflag

    http://stackoverflow.com/questions/41312622/how-to-echo-an-alert-in-php-a-string-some-confusion-with ...

  3. Levenshtein distance

    https://en.wikipedia.org/wiki/Levenshtein_distance 验证码识别 图片中的二维码截取

  4. Python - 求斐波那契数列前N项之和

    n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...

  5. Startssl 现在就启用 HTTPS,免费的!

    为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面  选择国家 和 输入 邮箱 他们会通过 ...

  6. 我的第一个chrome扩展(0)——目标

    当前有两个方向: 一.实现一个自动解码的地址栏监视器 扩展程序在后台不断监视地址栏输入,地址栏输入并回车后检查输入,若输入符合解码条件则调用网站信息进行解码,并将结果输出到地址栏,否则不改变: 初始阶 ...

  7. EFI

    有CSM的UEFI BIOS应该可以支持EFI Native和legacy两种启动方式吧,在BIOS SETUP选项里面有的选. EFI在开机时的作用和BIOS一样,就是初始化PC,但在细节上却又不一 ...

  8. Linux的常用基本命令

    Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中“系统管理员”拥有最高权限. 在启动Linux后屏幕出现如下界面显示: …… Re ...

  9. grep与find

    grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print -c:只输出匹配行的计数.-I:不区分大小写(只适 ...

  10. webKit和chromium的文章地址

     http://blog.csdn.net/column/details/yongsheng.html?&page=1