AcWing 78. 左旋转字符串-Java
原题链接
简单
代码
class Solution {
public String leftRotateString(String str, int n) {
char[] s = str.toCharArray();
for(int i = 0, j = s.length - 1; i < j; i++, j--){
swap(s,i,j);
}
for (int i = 0, j = s.length - n - 1; i < j; i++, j--){
swap(s,i,j);
}
for (int i = s.length - n , j = s.length - 1; i < j; i++, j--){
swap(s,i,j);
}
String res = "";
for (char c : s){
res += c;
}
return res;
}
public void swap(char[] s, int i, int j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
//使用工具类搞面试官心态
public String leftRotateString(String str, int n) {
return str.substring(n, str.length()) + str.substring(0,n);
}
}