classSolution{//交换publicstaticvoidchangePosition(int arr[],int i,int j){int temp = arr[i];arr[i]= arr[j];arr[j]= temp;}publicvoidsortColors(int[] nums){int n = nums.length;int left =0;//指向交换位置for(int right =0;right<n;right++){//将0换到left位置if(nums[right]==0)changePosition(nums,left++,right);}for(int right = left;right<n;right++){//将1换到left位置if(nums[right]==1)changePosition(nums,left++,right);}}}