19lines JAVA Solution


  • 0
    P
    public class Solution {
        public static void nextPermutation(int[] nums) {
        	boolean flag = false;
        	int i,j = 0;
        	for(i = nums.length-2; i>=0; i--) {
        		for(j = nums.length-1; j >=i; j--) {
        			if(nums[j] > nums[i]) {
        				nums[i] = nums[i] ^ nums[j];
        				nums[j] = nums[i] ^ nums[j];
        				nums[i] = nums[i] ^ nums[j];
        				flag = true;
        				break;
        			}
        		}
        		if(flag == true) break;
        	}
        	if(i+1 < nums.length-1) Arrays.sort(nums, i+1, nums.length);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.