zoukankan      html  css  js  c++  java
  • [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 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

    搞清排列过程!

    字典序排列
    把升序的排列(当然,也可以实现为降序)作为当前排列开始,然后依次计算当前排列的下一个字典序排列。

    对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j)。如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束;否则,重新对当前排列从后向前扫描,找到第一个大于i的元素k,交换i和k,然后对从j开始到结束的子序列反转,则此时得到的新排列就为下一个字典序排列。这种方式实现得到的所有排列是按字典序有序的,这也是C++ STL算法next_permutation的思想。

     1 class Solution {
     2 public:
     3     void nextPermutation(vector<int> &num) {
     4         if (num.size() < 2) return;
     5         int i, k;
     6         for (i = num.size() - 2; i >= 0; --i) if (num[i] < num[i+1]) break;
     7         for (k = num.size() - 1; k > i; --k) if (num[i] < num[k]) break;
     8         if (i >= 0) swap(num[i], num[k]);
     9         reverse(num.begin() + i + 1, num.end());
    10     }
    11 };

    如果是找全排列的话,就可以给next_permutation加一个返回值来标记还有没有下一个permutation了,如果没有就反回false。

     1 class Solution {
     2 public:
     3     bool nextPermute(vector<int> &num) {
     4         if (num.size() < 2) return false;
     5         int i, k;
     6         for (i = num.size() - 2; i >= 0; --i) if (num[i] < num[i+1]) break;
     7         for (k = num.size() - 1; k > i; --k) if (num[i] < num[k]) break;
     8         if (i >= 0) swap(num[i], num[k]);
     9         reverse(num.begin() + i + 1, num.end());
    10         return i >= 0;
    11     }
    12     
    13     vector<vector<int> > permute(vector<int> &num) {
    14         vector<vector<int>> res;
    15         sort(num.begin(), num.end());
    16         do {
    17             res.push_back(num);
    18         } while (nextPermute(num));
    19         return res;
    20     }
    21 };

    当然也可以用DFS来做,但是有重复元素的话DFS就有点无能为力了。

     1 class Solution {
     2 public:
     3     void dfs(vector<vector<int>> &res, vector<int> &path, vector<int> &num, int idx) {
     4         if (idx == num.size()) {
     5             res.push_back(path);
     6             return;
     7         }
     8         for (auto v : num) if (find(path.begin(), path.end(), v) == path.end()) {
     9             path.push_back(v);
    10             dfs(res, path, num, idx + 1);
    11             path.pop_back();
    12         }
    13     }
    14     
    15     vector<vector<int> > permute(vector<int> &num) {
    16         vector<vector<int>> res;
    17         vector<int> path;
    18         dfs(res, path, num, 0);
    19         return res;
    20     }
    21 };
  • 相关阅读:
    分布式事务系列--分布式跨库查询解决方案 mysql federated引擎的使用
    【MySQL】跨库join
    实操手册:如何玩转跨库Join?跨数据库实例查询应用实践
    实现数据库的跨库join
    微服务改造中解决跨库问题的思路
    从jar包中加载feignClient
    注入jar包里的对象,用@autowired使用
    使用 IntraWeb (27)
    使用 IntraWeb (26)
    使用 IntraWeb (25)
  • 原文地址:https://www.cnblogs.com/easonliu/p/3632442.html
Copyright © 2011-2022 走看看