标签:while sam cas ase case == sub [] sample
有一个只由0,1,2三种元素构成的整数数组,请使用交换、原地排序而不是使用计数进行排序。
给定一个只含0,1,2的整数数组A及它的大小,请返回排序后的数组。保证数组大小小于等于500。
[0,1,1,0,2,2],6
返回:[0,0,1,1,2,2]
public class ThreeColor { public int[] sortThreeColor(int[] A, int n) { // write code here int i=0; int j=0; int k=n-1; while(j<=k){ if(A[j]==0){ swap(A, j, i++); j++; } else if(A[j]==1){ j++; } else{ swap(A, j, k--); } } return A; } void swap(int[] A,int m,int n){ if(m!=n){ int temp = A[m]; A[m] = A[n]; A[n] = temp; } } }
标签:while sam cas ase case == sub [] sample
原文地址:http://www.cnblogs.com/gugibv/p/6671134.html