Title: #31 - Next Permutation - LeetCode JavaScript Solutions
Open Graph Title: #31 - Next Permutation - LeetCode JavaScript Solutions
X Title: #31 - Next Permutation - LeetCode JavaScript Solutions
Description: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement ...
Open Graph Description: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement ...
X Description: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/next-permutation
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/next-permutation","name":"#31 - Next Permutation - LeetCode JavaScript Solutions","description":"Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @return {void} Do not return anything, modify nums in-place instead.\n */\nvar nextPermutation = function(nums) {\n let i = nums.length - 2;\n\n while (i >= 0 && nums[i + 1] <= nums[i]) {\n i--;\n }\n\n if (i >= 0) {\n let cursor = nums.length - 1;\n while (nums[cursor] <= nums[i]) {\n cursor--;\n }\n swap(nums, i, cursor);\n }\n\n reverse(nums, i + 1);\n};\n\nfunction reverse(nums, start) {\n let i = start;\n let j = nums.length - 1;\n\n while (i < j) {\n swap(nums, i, j);\n i++;\n j--;\n }\n}\n\nfunction swap(nums, i, j) {\n const temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n}","keywords":"LeetCode 31, #31 - Next Permutation, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/next-permutation |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/next-permutation/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0031-next-permutation.js |
| Array | https://leetcodejavascript.com/tags/array |
| Two Pointers | https://leetcodejavascript.com/tags/two-pointers |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow