leetcode 之Permutation(七)

首先是next permutation的算法的描述和分析如下:

这题一是要知道思路,编程中注意STL的用法

 void nextPermutaion(vector<int> &num)
      {
          next_permutation(num.begin(), num.end());
      }
private:
    template<typename BidiIt>
    bool next_permutation(BidiIt first, BidiIt last)
    {
        //反向,注意!
        auto rfirst = reverse_iterator<BidiIt>(last);
        auto rlast = reverse_iterator<BidIt>(first);
        auto pivot = next(rfirst);
        while (pivot != rlast && *pivot > *prev(pivot))
            pivot++;

        if (pivot == rlast)
        {
            reverse(rfist, rlast);
            return false;
        }
        //注意用法
        auto change = find_if(rfirst, pivot, bind1st(less<int>(), *pivot));
        swap(*pivot, *change);
        reverse(rfirst, pivot);

        return true;
    }

接着是Permutation Sequence

 

 有人用康托编码来解这个问题,不过个人觉得不太好理解,其实完全可以用上题的思路

string getPermutaion(int n, int k)
      {
          string s(n, ‘0‘);
          for (int i = 0; i < n; i++)
              s[i] += i + 1;

          for (int i = 0; i < k; i++)
              next_permutation(s.begin(), s.end());

          return s;
      }

时间: 2024-10-09 05:31:23

leetcode 之Permutation(七)的相关文章

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

LeetCode OJ--Next Permutation *

求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int> &num) { if(num.size() == 0) return; c

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

LeetCode: Next Permutation [030]

[题目] 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 Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s

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 replaceme

[leetcode]Next Permutation @ Python

原题地址:https://oj.leetcode.com/problems/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 p

LeetCode Next Permutation

原题链接在这里:https://leetcode.com/problems/next-permutation/ 参考了这篇博文:http://blog.csdn.net/linhuanmars/article/details/20434115 分两种情况: 1. 从前往后一路数值一直变小,如"654321", 返回的应该是"123456"也就是整个反转 2. 数值没有一直变下,如"236541", 此时从后往前找到第一个数值下降的点3<6,