[LintCode] -- Permutations

说两件事

  • 第一件 这篇文章是补昨天的。。。
  • 第二件 题解换风格。都是看过题和数据并且思索了一番才来百度的,所以根本没必要再放题目和模板上去了,以后直接思路和代码。

  • 大概思路

    这是一个全排序的题,所以可以用DFS深搜树来解决,很简单。

  • 代码

    class Solution {
    public:
      /*
       * @param nums: A list of integers.
       * @return: A list of permutations.
       */
    
      vector<vector<int>> permute(vector<int> &nums) {
          // write your code here
          vector<vector<int>> so;
          fullarrange(nums, nums.size(), 0, so);
          return so;
      }
    
      void swap(int &a, int &b){
         int t = a;
         a = b;
         b = t;
       }
    
      void fullarrange(vector<int> &nums, int size, int n, vector<vector<int>> &so){
         if(n == size){
           so.push_back(nums);
           return;
         }
         for(int i=n; i<size; i++){
           swap(nums[i], nums[n]);
           fullarrange(nums, size, n+1, so);
           swap(nums[i], nums[n]);
         }
       }
    };
  • 题目链接:https://www.lintcode.com/problem/permutations/description

猜你喜欢

转载自blog.csdn.net/qq_24889575/article/details/81876432