DFS & BFS

Posted 鱼与海洋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DFS & BFS相关的知识,希望对你有一定的参考价值。

DFS : depth first search

BFS: breadth first search

 

DFS : 

46. 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],
  [3,2,1]
]

public class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> member = new ArrayList<Integer>();
        if(nums == null || nums.length == 0){
            return res;
        }
        dfs(res, member, nums);
        return res;
    }
    public void dfs(List<List<Integer>> res, List<Integer> member, int[] nums){
        if(member.size()== nums.length){
            res.add(new ArrayList<Integer>(member));
            return;
        }
        for(int i = 0; i < nums.length; i ++){
            if(member.contains(nums[i])) continue;
            member.add(nums[i]);
            dfs(res, member, nums);
            member.remove(member.size()-1);
        }
    }
}

47. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:

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

public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>>  res = new ArrayList<>();
        List<Integer> member = new ArrayList<>();
        boolean visited[] = new boolean[nums.length];
        if(nums == null || nums.length == 0){
            return res;
        }
        Arrays.sort(nums);
        dfs(res, member, nums, visited);
        return res;
    }
    public void dfs(List<List<Integer>> res, List<Integer> member, int[] nums, boolean[] visited){
        if(member.size() == nums.length){ 
            if(!res.contains(member)){
                res.add(new ArrayList<>(member));
                return;
            }
        }
        for(int i = 0; i<nums.length; i++){
            if(!visited[i]){
                if(i > 0 && nums[i] == nums[i - 1] && !visited[i -1] )
                    continue;
                visited[i] = true;
                member.add(nums[i]);
                dfs(res, member, nums, visited);
                visited[i] = false;
                member.remove(member.size() - 1);
            }
        }
    }
}

31. Next Permutation(不是 dfs)

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

public class Solution {
    // 四步
    // 1.从后向前找 first 小于second的两个值
    // 2.从后向前找第一个大于first的值 revertnum
    // 3.swap first和 revertnum
    // 4. reverse second到数组末尾
    public void nextPermutation(int[] nums) {
        if(nums == null || nums.length == 0 || nums.length == 1) return;
        int first = 0 ;
        int second = 0;
        int revertNum = 0;
        for(int i = nums.length - 1; i > 0 ; i--){
            if( nums[i - 1] < nums[i] ){
                first = i - 1;
                second = i;
                break;
            }
        }
        for(int i = nums.length - 1; i > 0 ; i--){
            if(nums[i] > nums[first]){
                revertNum = i;
                break;
            }
        }
        swap(first, revertNum, nums);
        reverse(second, nums.length -1, nums);
    }
    
    public void swap(int left, int right, int[] nums){
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
    
    public void reverse(int left, int right, int[] nums){
        while(left < right){
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }
}

 

以上是关于DFS & BFS的主要内容,如果未能解决你的问题,请参考以下文章

BFS&DFS遍历

BFS&DFS遍历

DFS & BFS

图的两种遍历-DFS&BFS

CSDN 年度征文|回顾 2021,展望 2022Java版高级数据结构图论基础(DFS&BFS)

DFS & BFS