Topic describes
Their thinking
The problem solving code
var nextPermutation = function(nums) {
// Find the first descending position from the left
let right = nums.length-1;
let flag = false;
while (right) {
if (nums[right] > nums[right-1]) {
right--;
flag = true;
break;
} else{ right--; }}if(! flag) { nums.sort((next,pre) = > next - pre)
} else {
let sorted = nums.splice(right+1).sort((next,pre) = > next - pre)
let move;
for (let i = 0; i < sorted.length; i++) {if (sorted[i] > nums[right]) {
move = i;
break; }}let temp = sorted[move];
sorted[move] = nums[right];
nums[right] = temp;
sorted.sort((next,pre) = >next - pre); nums.push(... sorted) } nums };Copy the code
revelation
- Learn to use sorted, Splice, Slice, and other common apis.
- Learn to introduce third party variables to aid our thinking.