We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cd14412 commit 0d61dfeCopy full SHA for 0d61dfe
Next Permutation/Next_Permutation.cpp
@@ -0,0 +1,33 @@
1
+// Runtime: 8 ms, faster than 77.55% of C++ online submissions for Next Permutation.
2
+// Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Next Permutation.
3
+
4
+class Solution
5
+{
6
+public:
7
+ void nextPermutation(vector<int>& nums)
8
+ {
9
+ bool found = false;
10
+ int k;
11
+ for (k = nums.size() - 2; k >= 0; --k)
12
13
+ if (nums[k] < nums[k + 1])
14
15
+ found = true;
16
+ break;
17
+ }
18
19
20
+ if (found)
21
22
+ int i;
23
+ for (i = nums.size() - 1; i > k; --i)
24
+ if (nums[i] > nums[k])
25
26
27
+ swap(nums[i], nums[k]);
28
+ reverse(nums.begin() + k + 1, nums.end());
29
30
+ else
31
+ reverse(nums.begin(), nums.end());
32
33
+};
0 commit comments