public class LeetCodeCN_0058_LCOF { public String reverseLeftWords(String s, int n) { char[] str = s.toCharArray(); rotate(str, 0, n - 1, s.length() - 1); return String.valueOf(str); } public void rotate(char[] arr, int L, int M, int R) { reverse(arr, L, M); reverse(arr, M + 1, R); reverse(arr, L, R); } public void reverse(char[] str, int l, int r) {
while (l < r) {
swap(str, l++, r--);
}
} public void swap(char[] str, int l, int r) { char tmp = str[l]; str[l] = str[r]; str[r] = tmp; }}
完整代码
[code]public class LeetCode_1470_ShuffleTheArray { public int[] shuffle(int[] arr, int n) { shuffle(arr); for (int i = 0; i < arr.length; i+=2) { reverse(arr,i,i+1); } return arr; } public static void shuffle(int[] arr) { if (arr == null || arr.length == 0 || (arr.length & 1) != 0) { return; } shuffle(arr, 0, arr.length - 1); } public static void swap(int[] nums, int L, int R) { if (nums == null || nums.length 0) { int len = R - L + 1; int base = 3; int k = 1; while (base