diff --git a/README.md b/README.md index ea076d819f..e1e278f219 100644 --- a/README.md +++ b/README.md @@ -954,7 +954,7 @@ _If you like this project, please leave me a star._ ★ |34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_34.java)||Medium|Array, Binary Search |33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_33.java)||Medium|Binary Search |32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_32.java)||Hard|Stack, DP -|31|[Next Permutation](https://leetcode.com/problems/parents-permutation)|[Solution](../master/src/main/java/com/fishercoder/solutions/_31.java)||Medium|Array +|31|[Next Permutation](https://leetcode.com/problems/parents-permutation)|[Solution](../master/src/main/java/com/fishercoder/solutions/_31.java), [C++](../master/cpp/_31.cpp)||Medium|Array |30|[Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_30.java)||Hard| HashMap |29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_29.java)||Medium| |28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_28.java)||Easy| String diff --git a/cpp/_31.cpp b/cpp/_31.cpp new file mode 100644 index 0000000000..dcf1138f46 --- /dev/null +++ b/cpp/_31.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + void nextPermutation(vector& nums) { + + int i1; + int i2; + bool hasPermutation = false; + + for(int i=nums.size()-1; i>0; i--){ + if(nums[i-1]i1; i--){ + if(nums[i]>nums[i1]){ + j=i; + break; + } + } + swap(nums[i1], nums[j]); + reverse(nums.begin()+i1+1, nums.end()); + }else{ + sort(nums.begin(), nums.end()); + } + + } +};