左旋转字符串
文章目录
public String LeftRotateString(String str,int n) {
if(str==null || str.length()==0){
return "";
}
char[] chs = str.toCharArray();
if(n>chs.length){
return str;
}
reverse(chs,0,chs.length-1);
reverse(chs,0,chs.length-n-1);
reverse(chs,chs.length-n,chs.length-1);
return new String(chs);
}
//将[start,end] 之间的字符串反转
private void reverse(char[] chs,int start,int end){
while (start<end){
char tmp = chs[start];
chs[start] = chs[end];
chs[end] = tmp;
start++;
end--;
}
}https://www.mianshi.online,https://www.i9code.cn
本文由博客一文多发平台 OpenWrite 发布!
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论