首页 > 其他 > 详细

lintcode-easy-Rotate String

时间:2016-03-07 07:48:11      阅读:230      评论:0      收藏:0      [点我收藏+]

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

Given "abcdefg".

offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
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;
        
        int size = str.length;
        
        offset %= size;
        if(offset == 0)
            return;
        
        for(int i = 0; i < offset; i++)
            rotate(str);
        return;
    }
    
    public void rotate(char[] str){
        char temp = str[str.length - 1];
        
        for(int i = str.length - 1; i > 0; i--)
            str[i] = str[i - 1];
        
        str[0] = temp;
        return;
    }
    
}

 

lintcode-easy-Rotate String

原文:http://www.cnblogs.com/goblinengineer/p/5249265.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!