LeetCode知识点总结 - 541
LeetCode 541. Reverse String II考点难度StringEasy题目Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.思路Step through the string 2k characters at a time : reverse first k, leave next k untouched, repeat答案publicclassSolution{publicStringreverseStr(Strings,intk){char[]arrs.toCharArray();intnarr.length;inti0;while(in){intjMath.min(ik-1,n-1);swap(arr,i,j);i2*k;}returnString.valueOf(arr);}privatevoidswap(char[]arr,intl,intr){while(lr){chartemparr[l];arr[l]arr[r];arr[r--]temp;}}}