-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Solution { | ||
public: | ||
void nextPermutation(vector<int>& nums) { | ||
int n = nums.size(); | ||
int i = n - 2; | ||
|
||
// Step 1: 오른쪽에서부터 첫 감소하는 위치를 찾기 | ||
while (i >= 0 && nums[i] >= nums[i + 1]) { | ||
i--; | ||
} | ||
|
||
if (i >= 0) { | ||
// Step 2: 그 값보다 큰 값 중 가장 작은 값과 교체 | ||
int j = n - 1; | ||
while (nums[j] <= nums[i]) { | ||
j--; | ||
} | ||
swap(nums[i], nums[j]); | ||
} | ||
|
||
// Step 3: i 이후의 배열을 오름차순으로 변경 | ||
reverse(nums.begin() + i + 1, nums.end()); | ||
} | ||
}; |