汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZde

这道题要注意字符串为空的情况,否则通不过 

public class Solution {
    public String LeftRotateString(String str,int n) {
            StringBuffer buf=new StringBuffer();
        int length=str.length();
        if(length==0)
        	return "";
        buf.append(str);
        String str1=buf.substring(0,n);
        System.out.println(str1);
        String str2=buf.substring(n, str.length());
        System.out.println(str2);
        str1=new StringBuffer(str1).reverse().toString();
        str2=new StringBuffer(str2).reverse().toString();
        return new StringBuffer(str1+str2).reverse().toString();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42039996/article/details/84616891
今日推荐