zoukankan      html  css  js  c++  java
  • Leetcode Permutations

    Given a collection of distinct numbers, return all possible permutations.
    
    For example,
    [1,2,3] have the following permutations:
    [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

    一. 广度优先搜索BFS

    这种方法有些像广度优先搜索BFS,扩展所有可能

     1 public class Solution {
     2     public List<List<Integer>> permute(int[] nums) {
     3         int len = nums.length;
     4         List<List<Integer>> ans = new ArrayList<List<Integer>>();
     5         ans.add(new ArrayList<Integer>());
     6         for(int i = 0; i < len; i++)
     7         {
     8             List<List<Integer>> new_ans = new ArrayList<List<Integer>>();
     9             for(int j = 0; j <= i; j++)
    10             {
    11                 for(int k = 0; k < ans.size(); k++)
    12                 {
    13                    List<Integer> temp = new ArrayList<Integer>(ans.get(k));
    14                    temp.add(j,nums[i]);
    15                    new_ans.add(temp);
    16                 }
    17             }
    18             ans = new_ans;
    19         }
    20         return ans;
    21     }
    22 
    23 
    24 }

     类似的做法还有手机键盘字母组合问题

     Letter Combinations of a Phone Number

    https://leetcode.com/discuss/11261/my-iterative-sollution-very-simple-under-15-lines

    二. 回溯backtracking 

    这道题的回溯算法很典型

     1 class Solution {
     2 public:
     3     vector<vector<int> > permute(vector<int>& nums) {
     4         vector<vector<int> > result;
     5         if(nums.size() == 0)
     6             return result;        
     7         permutation(nums, 0, result);
     8         return result;
     9     }
    10     void permutation(vector<int>& nums, int index, vector<vector<int> >& result)
    11     {
    12         int len = nums.size();
    13         if(index == len)
    14             result.push_back(nums);
    15         else{
    16             for(int i = index; i < len; i++){
    17                 swap(nums[i], nums[index]);
    18                 permutation(nums, index+1, result);
    19                 swap(nums[i], nums[index]); //palindrome
    20             }
    21         }
    22     }
    23 };

     对于backtracking(or DFS)来讲,可以对比 

    Palindrome Partitioning

    问题

     1 class Solution {
     2 public:
     3     vector<vector<string> > partition(string s) {
     4         vector<vector<string> > result;
     5         vector<string> temp;
     6         dfs(s, 0, temp, result);
     7         return result;
     8     }
     9     void dfs(string &s, int index, vector<string> temp, vector<vector<string> > &result)
    10     {
    11         if(index == s.size())
    12         {
    13             result.push_back(temp);
    14             return;
    15         }
    16 
    17         for(int i = index; i < s.size(); i++)
    18         {
    19             string sub_str = s.substr(index, i - index + 1);
    20             if(isPalindrome(sub_str))
    21             {
    22                 temp.push_back(sub_str);
    23                 dfs(s, i+1, temp, result);
    24                 temp.pop_back();   //back last, recover enviroment
    25             }
    26         }
    27     }
    28     bool isPalindrome (string s){
    29         if(s.empty())
    30             return false;
    31         int begin = 0, end = s.length() - 1;
    32         while(begin <= end)
    33         {
    34             if(s[begin++] != s[end--])
    35                 return false;
    36         }
    37         return true;
    38     }
    39 };
  • 相关阅读:
    Vue源码解析-源码目录及源码调试运行
    Vue.js源码解析-从scripts脚本看vue构建
    ps 命令显示不完整的问题
    Linux中10个实用命令,千万不要错过
    Linux 下如何使用 fc 和 alias 命令
    Mac 查看正在后台运行(显示)的程序
    Mac 显示桌面
    Mac如何批量关闭同一个程序
    Mac 选中删除
    Mac 中 vscode 打开项目文件夹
  • 原文地址:https://www.cnblogs.com/vin-yuan/p/5439616.html
Copyright © 2011-2022 走看看