Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
class Solution { public: void nextPermutation(vector
& nums) { // Find the anchor where a[i] < a[i+1] int anchor = -1; int n = nums.size(); for(int i = 0; i < n - 1; i++) { if(nums[i] < nums[i+1]) anchor = i; } if(anchor == -1) { // That means the array is sorted in increasing fashion, // reverse the array return std::reverse(nums.begin(), nums.end()); } // After finding the anchor index, find the next greater // element than anchor, by traversing through the end to start for(int i = n-1; i > anchor; i--) { if(nums[i] > nums[anchor]) { // Swap the anchor element and the next greater int temp = nums[anchor]; nums[anchor] = nums[i]; nums[i] = temp; break; } } return std::reverse(nums.begin() + anchor + 1, nums.end()); } };
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)