배움 저장소
[LeetCode] 031 Next Permutation 본문
https://leetcode.com/problems/next-permutation/
Next Permutation - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
풀이
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int l, r;
int n = nums.size();
for(l= n-1; 0<l; --l)
{
if(nums[l-1]<nums[l])
break;
}
if(l-1 <0)
{
reverse(nums.begin(), nums.end());
return;
}
for(r=n-1; 0<r; --r)
{
if(nums[l-1]<nums[r])
break;
}
swap(nums[l-1], nums[r]);
reverse(nums.begin()+l, nums.end());
}
};
Next Permutation은 크기값 순서대로 다음 Permutation을 출력해야 한다.
Comments