lintcode 旋轉字符串

public class Solution {
    /*
     * @param str: An array of char
     * @param offset: An integer
     * @return: nothing
     */
    public void rotateString(char[] str, int offset) { 
        if(str.length==0){
            return;
        }
        offset=offset%str.length;
        char[] temp=new char[offset];
        int j=0;
        for(int i=str.length-offset;i<str.length;i++){
            
            temp[j]=str[i];
            j++;
        }
        for(int i=str.length-1;i>=offset;i--){
            str[i]=str[i-offset];
        }
        for(int i=0;i<offset;i++){
            str[i]=temp[i];
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章