var permute = function(nums) { let last=nums.reverse().join(""); let arr=[]; while(true){ let newstr=nextPermutation(nums); arr.push([...newstr]); if(newstr.join("")==last){ break; } }
return arr; } var nextPermutation = function (nums) { let curpos = -1; let arr = []; for (let i = nums.length - 1; i >= 0; i--) { arr.push(nums[i]); if (nums[i] > nums[i - 1]) { curpos = i - 1; break; } } arr = arr.sort(function (a, b) { return a - b; });
for (let i = 0; i < arr.length; i++) { if (nums[curpos] < arr[i]) { let temp = arr[i]; arr[i] = nums[curpos]; nums[curpos] = temp; break; } } arr = arr.sort(function (a, b) { return a - b; });
for (let i = 0; i < arr.length; i++) { nums[i + curpos + 1] = arr[i]; }