标签:lis int esc eve 时间 origin -- 翻转 style
Example:
Input: s = "abcdefg", k = 2 Output: "bacdfeg"Restrictions:
1 class Solution { 2 public String reverseStr(String s, int k) { 3 char[] array = s.toCharArray(); 4 for ( int i = 0 ; i < s.length() ; i += 2*k ){ 5 reverse(array,i,i+k-1); 6 } 7 return String.valueOf(array); 8 } 9 10 private void reverse(char[] array, int start, int end) { 11 if ( start > array.length ) return; 12 int low = start; 13 int high = Math.min(end,array.length-1); 14 while ( low < high ){ 15 char c = array[low]; 16 array[low] = array[high]; 17 array[high] = c; 18 low++; 19 high--; 20 } 21 } 22 }
运行时间3ms,击败100%的提交。
这里可以总结一下字符串翻转的小技巧,就是用两个指针来完成。
标签:lis int esc eve 时间 origin -- 翻转 style
原文地址:https://www.cnblogs.com/boris1221/p/9316974.html