如把字符串abcdef左旋转2位得到字符串cdefab。请实现字符串左旋转的函数。
要求时间对长度为n的字符串操作的复杂度为O(n),辅助内存为O(1)。
java代码:

//字符串序列的变换

public class StrTrans {
    
    //思路一
    public static String strVary_1(char[] source,int cut){
        if(source==null||source.length==0)
            return null;
        char temp =0;
        int first = 0;
        int last = source.length-1;
        
        while(first<last){
            temp = source[first];
            source[first]=source[last];
            source[last]=temp;
            first++;
            last--;
        }
        
        first=0;
        last = cut;
        while(first<last){
            temp = source[first];
            source[first]=source[last];
            source[last]=temp;
            first++;
            last--;
        }
        
        first = cut+1;
        last = source.length-1;
        while(first<last){
            temp = source[first];
            source[first]=source[last];
            source[last]=temp;
            first++;
            last--;
        }
        return new String(source);
    }
    
    //思路二
    public static String strVary_2(char[] source,int cut){
        if(source==null||source.length==0)
            return null;
        int length = source.length;
        int first = 0;
        cut=cut%length;
        int second =cut;
        char temp =0;
        while(second<length){
            temp=source[first];
            source[first]=source[second];
            source[second]=temp;
            first++;
            second++;
        }
        int tail =cut-length%cut;
        for(int i=0;i<tail;i++){
            int index=first;
            temp =source[index];
            while(index<length-1){
                source[index]=source[index+1];
                index++;
            }
            source[index]=temp;
        }
        return new String(source);
    }
    
    //思路三   方法一
    public static String strVary_3_1(char[] source,int cut){
        int length = source.length;
        cut=cut%length;
        int gcd = gcd(length,cut);
        int sub = length/gcd;
        char temp=0;
        int j=0;
        for( int i=0;i<gcd;i++){
            temp = source[i];
            for(j = 0;j<sub-1;j++)
                source[(i+j*cut)%length]=source[(i+(j+1)*cut)%length];
            source[(i+j*cut)%length]=temp;
        }
        return new String(source);
    }
    
    //思路三   方法二
    public static String strVary_3_2(char[] source,int cut){
        int length =source.length;
        cut = cut%length;
        int gcd = gcd(length,cut);
        char temp = 0;
        int index = 0;
        for(int i=0;i<gcd;i++){
            temp = source[i];
            for(int j=(i+cut)%length;j!=i;j=(j+cut)%length){
                source[index]=source[j];
                index = j;
            }
            source[index]=temp;
        }
        return new String(source);
    }
    
    public static void main(String[] args) {
        char[] source ={'a','b','c','d','e','f','g'};
        int cut =3;
        System.out.println(strVary_1(source, cut));
        System.out.println(strVary_2(source, cut));
        System.out.println(strVary_3_1(source, cut));
        System.out.println(strVary_3_2(source, cut));
//        System.out.println(gcd(10,4));
    }
    
    //求最大公约数
    public static int gcd(int m,int n){
        if(m<n){
            int tem =m;
            m = n;
            n = tem;
        }
        if(m%n==0)
            return n;
        else
            return gcd(n,m%n);
    }
    
    
}

结果:

defgabc

gabcdef

cdefgab

fgabcde


treeandgrass
1.3k 声望16 粉丝

github:[链接]