leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 904b8535aa3a8d77387ade201b070040aaa3d293
parent d6d35d89f594a418c43e6453792331236d015bcf
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 21 Jul 2023 13:46:46 +0200

Random Problem

Diffstat:
AProblems/0031.cpp | 17+++++++++++++++++
MREADME.md | 1+
2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/Problems/0031.cpp b/Problems/0031.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + void nextPermutation(vector<int> &nums) { + int n = nums.size(), k, l; + for (k = n - 2; k >= 0 && nums[k] >= nums[k + 1]; k--) + ; + + if (k >= 0) { + for (l = n - 1; l > k && nums[l] <= nums[k]; l--) + ; + swap(nums[k], nums[l]); + reverse(nums.begin() + k + 1, nums.end()); + } else { + reverse(nums.begin(), nums.end()); + } + } +}; diff --git a/README.md b/README.md @@ -52,6 +52,7 @@ for solving problems. | 0027 | Easy | [Remove Element](Problems/0027.cpp) | | 0028 | Medium | [Find the Index of the First Occurrence in a String](Problems/0028.cpp) | | 0029 | Medium | [Divide Two Integers](Problems/0029.cpp) | +| 0031 | Medium | [Next Permutation](Problems/0031.cpp) | | 0033 | Medium | [Search in Rotated Sorted Array](Problems/0033.cpp) | | 0034 | Medium | [Find First and Last Position of Element in Sorted Array](Problems/0034.cpp) | | 0035 | Easy | [Search Insert Position](Problems/0035.cpp) |