算法练习(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);
        }
    }
}
相关推荐
跟着珅聪学java1 小时前
spring boot +Elment UI 上传文件教程
java·spring boot·后端·ui·elementui·vue
我命由我123451 小时前
Spring Boot 自定义日志打印(日志级别、logback-spring.xml 文件、自定义日志打印解读)
java·开发语言·jvm·spring boot·spring·java-ee·logback
lilye661 小时前
程序化广告行业(55/89):DMP与DSP对接及数据统计原理剖析
java·服务器·前端
想跑步的小弱鸡4 小时前
Leetcode hot 100(day 3)
算法·leetcode·职场和发展
战族狼魂5 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
xyliiiiiL6 小时前
ZGC初步了解
java·jvm·算法
杉之6 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
爱的叹息6 小时前
RedisTemplate 的 6 个可配置序列化器属性对比
算法·哈希算法
hycccccch7 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
独好紫罗兰7 小时前
洛谷题单2-P5713 【深基3.例5】洛谷团队系统-python-流程图重构
开发语言·python·算法