面试题58.2:左旋转字符串
题目要求:
实现一个函数完成字符串的左旋转功能。比如,输入abcdefg和数字2,输出为cdefgab。
解题思路:
类似于58.翻转单词顺序。首先对于字符串“abcdefg”整体翻转,得到“gfedcba”;然后对于后2个字符“ba”进行翻转,对于剩下的字符“gfedc”进行翻转,得到“cdefgab”。
package chapter6;
/**
* Created with IntelliJ IDEA
* Author: ryder
* Date : 2017/8/18
* Time : 16:05
* Description:左旋转字符串
* abcdeftg 2 => cdefgab
**/
public class P286_LeftRotateString {
public static String leftRotateString(String str,int i){
if(str==null||str.length()==0||i<=0||i>=str.length())
return str;
StringBuilder stringBuilder = new StringBuilder(str);
reverseSubString(stringBuilder,0,stringBuilder.length()-1);
reverseSubString(stringBuilder,0,stringBuilder.length()-i-1);
reverseSubString(stringBuilder,stringBuilder.length()-i,stringBuilder.length()-1);
return stringBuilder.toString();
}
//翻转stringBuilder[start,end]
public static void reverseSubString(StringBuilder stringBuilder,int start,int end){
for(int i=start;i<=start+(end-start)/2;i++){
char temp = stringBuilder.charAt(i);
stringBuilder.setCharAt(i,stringBuilder.charAt(end-i+start));
stringBuilder.setCharAt(end-i+start,temp);
}
}
public static void main(String[] args){
String str = "abcdefg";
System.out.println(leftRotateString(str,2));
}
}
运行结果
cdefgab