码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode-easy-Rotate String

时间:2016-03-07 07:48:15      阅读:225      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!