旋转字符串

旋转字符串

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(offset == 0 || str.length == 0){
            return;
        }
    
        offset = offset % str.length;
        char[] temp = new char[offset];
        for(int i = 0; i < offset; i++){
            temp[i] = str[str.length - offset + i];
        }
        for(int i = str.length - offset - 1; i >= 0; i--){
            str[i + offset] = str[i];
        }
        for(int i = 0; i < offset; i++){
            str[i] = temp[i];
        }
    }
}

你可能感兴趣的:(旋转字符串)