[leetcode] 60. 第k个排列

60. 第k个排列

还是使用之前用过多次的nextPermutation方法。。。(几乎所有跟排列相关的题都是同一个题- -)

class Solution {
    public String getPermutation(int n, int k) {
        int[] nums = new int[n];
        for (int i = 1; i <= n; i++) {
            nums[i - 1] = i;
        }
        while (k > 1) {
            nextPermutation(nums);
            k--;
        }
        StringBuilder ans = new StringBuilder();
        for (int num : nums) {
            ans.append(num);
        }
        return ans.toString();
    }

    // 当没有下一个排列时return false
    public boolean nextPermutation(int[] nums) {
        if (nums.length == 1) {
            return false;
        }
        int p = -1;
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]) {
                p = i;
                break;
            }
        }

        if (p != -1) {
            int tmp = nums[p];
            int q = nums.length - 1;
            while (nums[q] <= tmp) {
                q--;
            }

            nums[p] = nums[q];
            nums[q] = tmp;

            reverse(p + 1, nums);
        } else {
//            reverse(0, nums);
            return false;
        }

        return true;
    }

    public void reverse(int k, int[] nums) {
        if (k >= nums.length) return;
        int i = k;
        int j = nums.length - 1;
        while (i < j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            i++;
            j--;
        }
    }
}

原文地址:https://www.cnblogs.com/acbingo/p/9357612.html

时间: 2024-09-30 16:50:58

[leetcode] 60. 第k个排列的相关文章

leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "

60. 第k个排列

心得:使用字符串的方法的时候一定要知道如下 错误:str.substring(a,b) 正确:str.substring(a,b) 这道题没有用巧妙的方法,主要使用了回溯,回溯一定要掌握啊 感觉还是不透彻啊 1 class Solution { 2 int index=0; 3 String ss=""; 4 ArrayList<Integer> ans=null; 5 public String getPermutation(int n, int k) { 6 int[]

leetcode排列,求第k个排列

stl 中的下一个排列在写一遍忘了 写个1个多小时,使用递归写的,错误就在我使用一个list保存当前剩下的数,然后利用k/(n-1)!的阶乘就是删除的数字,但进过观察, 比如 list={1,2,3} 分成3组: 1  {2,3} 2 {1,3} 3 {1,2} 确定位于哪个组,然后确定位于哪个组的第几个nyoj 511. 求第3个排列   ,3%2=1,删除 list就是第3个数3,其实呢是第2个树2 ,所以   计算方法为 (k-1)/(n-1)! 另外一个对于下一组,k%(n-1)!也不行

LeetCode 笔记21 生成第k个排列

题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312"

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 ord

第k个排列

给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 1 ≤ n ≤ 9 样例 对于 n = 3, 所有的排列如下: 123 132 213 231 312 321 如果 k = 4, 第4个排列为,231. 1 public class PaiLieK 2 { 3 public String getPermutation(int n, int k) 4 { 5 List<Integer> list = new ArrayList<>(); 6 List<

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

UVA - 12335 Lexicographic Order (第k大排列)

Description A Lexicographic Order Input: Standard Input Output: Standard Output The alphabet of a certain alien language consists of n distinct symbols. The symbols are like the letters of English alphabet but their ordering is different. You want to