Share my solution with O(N^2)


  • 0
    L

    public class Solution {

    public void NextPermutation(int[] nums) {
        
        int len = nums.Length;
        int index = FindIndex(nums,len);
        
        for(int i = index+1;i<len;i++)
        {
            for(int j=i+1;j<len;j++)
            {
                if(nums[i] > nums[j])
                {
                    int t = nums[i];
                    nums[i] = nums[j];
                    nums[j] = t;
                }
            }
        }
        
        
    }
    
    private int FindIndex(int[] nums,int len)
    {
        for(int i=len-1;i>=0;i--)
        {
            for(int j=len-1;j>i;j--)
            {
                if(nums[i] < nums[j])
                {
                    int t = nums[i];
                    nums[i] = nums[j];
                    nums[j] = t;
                    
                    return i;
                }
            }
            
        }
        
        return -1;
    }
    

    }


Log in to reply
 

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