算法题1答案 public static void main(String[] args) { char[] list = {'B', 'A', 'C', 'B', 'C', 'A', 'B', 'A', 'C', 'A', 'C', 'B', 'C', 'A', 'B', 'A', 'C', 'A', 'C', 'C'}; int p1 = 0, p2 = 0, p3 = list.length-1; while (p2 != p3) { if (list[p1] == 'B') { p1++; p2 = p1; } if (list[p3] == 'C') { p3--; } if (list[p2] == 'A') { p2++; } if (list[p2] == 'B') { // 交换p1与p2对应的值 swap(list, p1, p2); p1++; } if (list[p2] == 'C') { swap(list, p2, p3); p3--; } } System.out.println(Arrays.toString(list)); } public static void swap(char[] list, int i, int j) { char temp = list[i]; list[i] = list[j]; list[j] = temp; }
6 10

相关推荐

牛客网
牛客企业服务