My solution that uses Arrays.sort, very simple!


  • 0
    C
    public class Solution {
        private void swap(int[] num, int a, int b) {
            num[a] = num[a] ^ num[b];
            num[b] = num[a] ^ num[b];
            num[a] = num[a] ^ num[b];
        }
        public void nextPermutation(int[] num) {
            for (int i = num.length - 1; i > 0; i--) {
    
                if (num[i] > num[i - 1]) {
                    Arrays.sort(num, i, num.length);
                    for (int j = i; j < num.length; j++) {
                        if (num[j] > num[i - 1]) {
                            swap(num, j, i - 1);
                            break;
                        }
                    }
                    return;
                }
            }
            
            Arrays.sort(num);
        }
    }

Log in to reply
 

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