MCPcopy Create free account
hub / github.com/camilamaia/jornada-big-tech / Solution

Class Solution

leetcode/src/NextPermutationLeetCodeSolution.java:2–32  ·  view source on GitHub ↗

Problem: https://leetcode.com/explore/interview/card/google/59/array-and-strings/3050/

Source from the content-addressed store, hash-verified

1/** Problem: https://leetcode.com/explore/interview/card/google/59/array-and-strings/3050/ */
2public class Solution {
3 public void nextPermutation(int[] nums) {
4 int i = nums.length - 2;
5 while (i >= 0 && nums[i + 1] <= nums[i]) {
6 i--;
7 }
8 if (i >= 0) {
9 int j = nums.length - 1;
10 while (nums[j] <= nums[i]) {
11 j--;
12 }
13 swap(nums, i, j);
14 }
15 reverse(nums, i + 1);
16 }
17
18 private void reverse(int[] nums, int start) {
19 int i = start, j = nums.length - 1;
20 while (i < j) {
21 swap(nums, i, j);
22 i++;
23 j--;
24 }
25 }
26
27 private void swap(int[] nums, int i, int j) {
28 int temp = nums[i];
29 nums[i] = nums[j];
30 nums[j] = temp;
31 }
32}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected