下一个排列
解题思路:二分查找+数组反转
class Solution { public void nextPermutation(int[] nums) { int len = nums.length; if(len<=1){ return; } int i = 0; for(i=len-1;i>0;--i){ if(nums[i]>nums[i-1]){ int end = erfen(nums,i,len-1,nums[i-1]); nums[i-1]=nums[i-1]^nums[end]; nums[end]=nums[i-1]^nums[end]; nums[i-1]=nums[i-1]^nums[end]; reverse(nums, i, len); break; } } if(i==0){ reverse(nums, 0, len); } } public void reverse(int[] nums, int i, int len){ int leng = i+(len-i)/2; for(int k=i;k<leng;++k){ nums[k] = nums[k]^nums[len-1]; nums[len-1] = nums[k]^nums[len-1]; nums[k] = nums[k]^nums[len-1]; --len; } } public int erfen(int[] nums, int start, int end,int target){ int mid = 0; while(start<=end){ mid = (start+end)/2; if(target>=nums[mid]){ end = mid -1; }else{ start = mid+1; } } return end; } }