zoukankan      html  css  js  c++  java
  • DFS解法的两道题 Leetcode 46 Permutations & Leetcode 78 Subset

    Leetcode 78 Subset

    Given a set of distinct integers, S, return all possible subsets.

    Note:

    • Elements in a subset must be in non-descending order.
    • The solution set must not contain duplicate subsets.

    For example,
    If S = [1,2,3], a solution is:

    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]

    题目解析:
    这种返回all possible的题就可以用bfs的方法来解啦~思路是先把array排序(因为题里要求是non-descending)
    然后思路是从num里面依次读取数字,存到ArrayList<Integer>的list里面, 然后再把list存到ArrayList<ArrayList<Integer>>的res里
    顺序是这样的[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]
    记得边界检测
     1 public class Solution {
     2     public static List<List<Integer>> subsets(int[] S) {
     3         List<List<Integer>> res = new ArrayList<List<Integer>>();
     4         if(S == null || S.length == 0)
     5             return res;
     6         Arrays.sort(S);
     7         ArrayList<Integer> list = new ArrayList<Integer>();
     8         dfs(res, list, S, 0);
     9         return res;
    10     }
    11     public static void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] S, int pos){
    12         res.add(new ArrayList<Integer>(list));
    13         for(int i = pos; i < S.length; i++){
    14             list.add(S[i]);
    15             dfs(res, list, S, i+1);
    16             list.remove(list.size() - 1);
    17         }
    18     }
    19 }
    
    

    Leetcode 46 Permutations

    Given a collection of 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].

    题目解析:

    这道题更是典型的DFS啦~直接用DFS模型就好了~至于检测某个数组的element是否被使用了这种情况就新建一个boolean的数组进行检测就好了~

    public class Solution {
        public static List<List<Integer>> permute(int[] num) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            ArrayList<Integer> list = new ArrayList<Integer>();
            boolean[] flag = new boolean[num.length];
            dfs(res, list, num, flag);
            return res;
        }
        public static void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] num, boolean[] flag){
            if(list.size() == num.length){
                res.add(new ArrayList<Integer>(list));
                return;
            }
            for(int i = 0; i < num.length; i++){
                if(!flag[i]){
                    flag[i] = true;
                    list.add(num[i]);
                    dfs(res, list, num, flag);
                    list.remove(list.size() - 1);
                    flag[i] = false;
                }
            }
        }
    }
  • 相关阅读:
    linux下动态链接库.so文件 静态链接库.a文件创建及使用
    matlab 自动阈值白平衡算法 程序可编译实现
    C++ 迭代器介绍 [转摘]
    C++ Primer 第三章 标准库类型vector+迭代器iterator 运算
    matlab灰度变彩色+白平衡算法实现
    我和奇葩的故事之失联第七天
    C++ Primer 第三章 标准库类型string运算
    OpenCV白平衡算法之灰度世界法(消除RGB受光照影响)
    查看网络情况netstat指令与动态监控top指令
    linux服务
  • 原文地址:https://www.cnblogs.com/sherry900105/p/4343779.html
Copyright © 2011-2022 走看看