public class MoveLeftM {
public static void reverse(StringBuilder string,int st,int end){
int step = (end-st+1)/2;
for(int i = 0; i < step;i++){
char t = string.charAt(st+i);
string.setCharAt(st+i, string.charAt(end-i));
string.setCharAt(end-i, t);
}
}
public static void moveLeftM(StringBuilder string,int step){
int len = string.length();
reverse(string, 0, step-1);
reverse(string, step, len-1);
reverse(string, 0, len-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder string = new StringBuilder("abcdefghijklmn");
MoveLeftM.moveLeftM(string, 6);
System.out.println(string);
}
}将一个字符串不使用额外容器向左移动M位
最新推荐文章于 2025-03-10 09:45:00 发布
本文介绍了一种字符串左移算法的实现方法,通过三次反转操作实现了字符串的高效左移。文章提供了一个Java程序示例,演示了如何将字符串中的字符向左移动指定步数,并附带了一个测试案例。
1789

被折叠的 条评论
为什么被折叠?



