8. Rotate String

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
个人分类: LintCode
上一篇2. Trailing Zeros
下一篇9. Fizz Buzz
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭