class Solution {
public String reverseStr(String s, int k) {
char[] c = s.toCharArray();//将字符串转为字符数组
char temp = '0';//为后面交换位置提供存储中间量
//对数组进行遍历
for (int i = 0;i<c.length ;i+=2*k ) {
int start = i ;//开始的指针
//指向结束的指针
int end = Math.min(c.length-1,start+k-1);//这里用了比较大小的方法来控制他的下标不会超出范围
//进行反转
while(start < end){
temp = c[start];
c[start] = c[end];
c[end] = temp;
start++;
end -- ;
}
}
return new String(c);
}
}
力扣 541. 反转字符串 II
最新推荐文章于 2024-07-15 12:10:21 发布