8. Rotate String

原创 2018年04月15日 13:32:40

8. Rotate String

Description

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"

Solution

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 = offset % str.length;
        String s = String.copyValueOf(str);
        String ss = s.substring(s.length()-offset) + s.substring(0,s.length()-offset);
        for(int i=0;i<ss.length();i++){
            str[i] = ss.charAt(i);
        }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foradawn/article/details/79948618

String类

介绍String类的初始化 String类的各种方法 String类的各种应用实例
  • 2017年03月27日 10:26

Lintcode8 Rotate String solution 题解

【题目描述】 Given a string and an offset, rotate string by offset. (rotate from left to right) ...
  • jane_silva
  • jane_silva
  • 2017-04-05 12:41:20
  • 215

#8 Rotate String

题目描述: Given a string and an offset, rotate string by offset. (rotate from left to right) ...
  • haifischxia
  • haifischxia
  • 2016-08-17 11:33:07
  • 113

[Lintcode]Rotate String 两步翻转法

两步翻转法 public class Solution { /** * @param str: an array of char ...
  • jc69186918
  • jc69186918
  • 2016-04-10 18:43:50
  • 837

LintCode 8. Rotate String

题目 思路 三次翻转。 代码 class Solution: &quot;&quot;&quot; @param str: An array of char ...
  • y12345678904
  • y12345678904
  • 2018-02-23 14:29:15
  • 22

【LintCode】Rotate String 旋转字符串

给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)样例 对于字符串 “abcdefg”.offset=0 => “abcdefg” offset=1 => “gabcdef” of...
  • wutingyehe
  • wutingyehe
  • 2015-07-14 22:36:06
  • 2296

LeetCode题解--8. String to Integer (atoi)

题目Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases...
  • gatieme
  • gatieme
  • 2016-04-02 23:10:39
  • 1272

[Leetcode] 796. Rotate String 解题报告

题目: We are given two strings, A and B. A shift on A consists of taking string A and moving...
  • magicbean2
  • magicbean2
  • 2018-03-30 20:08:14
  • 24

【LeetCode】796. Rotate String

题目:思路:这道题目关键在思路吧,没什么想法的话,这里先给一个提示:string str = A + A,先思考一下然后再看答案代码。下面贴代码,一些解释写在代码注释里#include &amp;lt...
  • Mint_r
  • Mint_r
  • 2018-03-13 17:55:35
  • 63

python--leetcode796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the left...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2018-03-19 20:23:30
  • 31
收藏助手
不良信息举报
您举报文章:8. Rotate String
举报原因:
原因补充:

(最多只允许输入30个字)