算法练习(6):牛客在线编程06 递归/回溯

java 复制代码
package jz.bm;

import java.io.PushbackInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

public class bm6 {
    /**
     * BM55 没有重复项数字的全排列
     */
    ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        if (num.length == 0) {
            return res;
        }
        Arrays.sort(num);
        boolean[] used = new boolean[num.length];
        Arrays.fill(used, false);
        dfs(num, used, new ArrayList<>());
        return res;
    }

    private void dfs(int[] num, boolean[] used, ArrayList<Integer> list) {
        if (list.size() == num.length) {
            res.add(new ArrayList<>(list));
        } else {
            for (int i = 0; i < num.length; i++) {
                if (used[i]) {
                    continue;
                }
                used[i] = true;
                list.add(num[i]);
                dfs(num, used, list);
                used[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }


    /**
     * BM56 有重复项数字的全排列
     */
    public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
        if (num.length == 0) {
            return res;
        }
        Arrays.sort(num);
        boolean[] used = new boolean[num.length];
        Arrays.fill(used, false);
        dfs56(num, used, new ArrayList<>());
        return res;
    }

    private void dfs56(int[] num, boolean[] used, ArrayList<Integer> list) {
        if (list.size() == num.length) {
            res.add(new ArrayList<>(list));
        } else {
            for (int i = 0; i < num.length; i++) {
                if (used[i]) {
                    continue;
                }
                if (i > 0 && num[i] == num[i - 1] && used[i - 1]) {
                    continue;
                }
                used[i]  = true;
                list.add(num[i]);
                dfs56(num, used, list);
                used[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }
    /**
     * BM57 岛屿数量
     */
    public int solve (char[][] grid) {
        int m = grid.length;
        if (m == 0) {
            return 0;
        }
        int n = grid[0].length;
        if (n == 0) {
            return 0;
        }
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    res++;
                    dfs57(grid, m, n, i, j);
                }
            }
        }
        return res;
    }

    private void dfs57(char[][] grid, int m, int n, int i, int j) {
        grid[i][j] = '0';
        if (i - 1 >= 0 && grid[i - 1][j] == '1') {
            dfs57(grid, m, n, i - 1, j);
        }
        if (i + 1 < m && grid[i + 1][j] == '1') {
            dfs57(grid, m, n, i + 1, j);
        }
        if (j - 1 >= 0 && grid[i][j - 1] == '1') {
            dfs57(grid, m, n, i, j - 1);
        }
        if (j + 1 < n && grid[i][j + 1] == '1') {
            dfs57(grid, m, n, i, j + 1);
        }
    }
    /**
     * BM58 字符串的排列
     */
    ArrayList<String> strings = new ArrayList<>();
    public ArrayList<String> Permutation(String str) {
        if (str == null || "".equals(str)) {
            return strings;
        }
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        boolean[] used = new boolean[chars.length];
        Arrays.fill(used, false);
        dfs58(chars, used, new StringBuilder());
        return strings;
    }

    private void dfs58(char[] chars, boolean[] used, StringBuilder stringBuilder) {
        if (stringBuilder.length() == chars.length) {
            strings.add(new String(stringBuilder));
        } else {
            for (int i = 0; i < chars.length; i++) {
                if (used[i]) {
                    continue;
                }
                if (i > 0 && chars[i] == chars[i - 1] && used[i - 1]) {
                    continue;
                }
                stringBuilder.append(chars[i]);
                used[i] = true;
                dfs58(chars, used, stringBuilder);
                used[i] = false;
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }
        }
    }
    /**
     * BM60 括号生成
     */
    ArrayList<String> arrayList = new ArrayList<>();
    public ArrayList<String> generateParenthesis (int n) {
        if (n == 0) {
            return arrayList;
        }
        dfs60(n, 0, 0, new StringBuilder());
        return arrayList;
    }

    private void dfs60(int n, int left, int right, StringBuilder stringBuilder) {
        if (left == n && right == n) {
            arrayList.add(new String(stringBuilder));
            return;
        }
        if (left < n) {
            stringBuilder.append("(");
            dfs60(n, left + 1, right, stringBuilder);
            stringBuilder.deleteCharAt(left + right);
        }
        if (right < left && right < n) {
            stringBuilder.append(")");
            dfs60(n, left, right + 1, stringBuilder);
            stringBuilder.deleteCharAt(left + right);
        }
    }

    /**
     * BM61 矩阵最长递增路径
     */
    int max = 0;
    public int solve (int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        //这题dfs不需要记录visited, 因为能走的路径是单向的
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dfs61(matrix, m, n, i, j, 1);
            }
        }
        return max;
    }

    private void dfs61(int[][] matrix, int m, int n, int i, int j, int count) {
        if (i - 1 >= 0 && matrix[i - 1][j] > matrix[i][j]) {
            max = Math.max(count + 1, max);
            dfs61(matrix, m, n, i - 1, j, count + 1);
        }
        if (i + 1 < m &&  matrix[i + 1][j] > matrix[i][j]) {
            max = Math.max(count + 1, max);
            dfs61(matrix, m, n, i + 1, j, count + 1);
        }
        if (j - 1 >= 0 && matrix[i][j - 1] > matrix[i][j]) {
            max = Math.max(count + 1, max);
            dfs61(matrix, m, n, i, j - 1, count + 1);
        }
        if (j + 1 < n && matrix[i][j + 1] > matrix[i][j]) {
            max = Math.max(count + 1, max);
            dfs61(matrix, m, n, i, j + 1, count + 1);
        }
    }
}
相关推荐
hunzi_110 分钟前
Java开发的商城系统怎样
java
master-dragon16 分钟前
CPU & 缓存基础知识
java·缓存
loss_rose77721 分钟前
【集合】单列集合和双列集合
java
liwulin050625 分钟前
【JAVA】获取windows内存使用率排名前十的进程信息、总的cpu和内存使用率
java·开发语言·windows
程序员入门中36 分钟前
深入理解 @Transactional 注解与 Spring 事务机制
java·数据库·spring
程序趣谈1 小时前
算法随笔_21:字符的最短距离
算法
Long_poem1 小时前
【第六天】零基础入门刷题Python-算法篇-数据结构与算法的介绍-一种常见的贪心算法(持续更新)
python·算法·贪心算法
翻晒时光1 小时前
Java 反射机制:春招面试中的关键知识点
java·面试
索然无味io1 小时前
中间件安全
java·笔记·学习·安全·web安全·网络安全·中间件
<余晖>1 小时前
插入排序
数据结构·算法·排序算法