Skip to content

Commit 0d61dfe

Browse files
Add files via upload
1 parent cd14412 commit 0d61dfe

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Next Permutation/Next_Permutation.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -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+
break;
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

Comments
 (0)