2023-11-10
思路:
先转为char【】,再原地反转(不用取出来),最后转为字符串
class Solution { public String reverseStr(String s, int k) { //先转为char【】,再原地反转(不用取出来),最后转为字符串 int n = s.length(); char[] arr = s.toCharArray(); for (int i = 0; i < n; i += 2 * k) {//起点是i,终点是i+k ,注意一下不要超了 reverse(arr, i, Math.min(i + k, n) - 1); } return new String(arr); } public void reverse(char[] arr, int left, int right) { while (left < right) { char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } }
标签:char,arr,right,int,II,541,字符串,left From: https://www.cnblogs.com/youye9527/p/17824845.html