LeetCode: Next Permutation 解题报告

Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

SOLUTION 1:

1.  From the tail to find the first digital which drop
  example: 12 4321

首先我们找到从尾部到头部第一个下降的digit. 在例子中是:2

2. 把从右到左第一个比dropindex大的元素换过来。

3. 把dropindex右边的的序列反序

4. 如果1步找不到这个index ,则不需要执行第二步。

 1 public class Solution {
 2     public void nextPermutation(int[] num) {
 3         if (num == null) {
 4             return;
 5         }
 6
 7         int len = num.length;
 8
 9         // Find the index which drop.
10         int dropIndex = -1;
11         for (int i = len - 1; i >= 0; i--) {
12             if (i != len - 1 && num[i] < num[i + 1]) {
13                 dropIndex = i;
14                 break;
15             }
16         }
17
18         // replace the drop index.
19         if (dropIndex != -1) {
20             for (int i = len - 1; i >= 0; i--) {
21                 if (num[i] > num[dropIndex]) {
22                     swap(num, dropIndex, i);
23                     break;
24                 }
25             }
26         }
27
28         // reverse the link.
29         int l = dropIndex + 1;
30         int r = len - 1;
31         while (l < r) {
32             swap(num, l, r);
33             l++;
34             r--;
35         }
36     }
37
38     public void swap(int[] num, int l, int r) {
39         int tmp = num[l];
40         num[l] = num[r];
41         num[r] = tmp;
42     }
43
44 }

SOLUTION 2:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/permutation/NextPermutation.java

时间: 2024-10-13 01:40:15

LeetCode: Next Permutation 解题报告的相关文章

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

Leetcode:Interleaving String 解题报告

Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.

Leetcode:Scramble String 解题报告

Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string,

LeetCode: Distinct Subsequences 解题报告

Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without di