算法练习(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);
        }
    }
}
相关推荐
drebander10 分钟前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
乌啼霜满天24913 分钟前
Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
java·spring boot·spring·mvc
tangliang_cn18 分钟前
java入门 自定义springboot starter
java·开发语言·spring boot
莫叫石榴姐19 分钟前
数据科学与SQL:组距分组分析 | 区间分布问题
大数据·人工智能·sql·深度学习·算法·机器学习·数据挖掘
程序猿阿伟19 分钟前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
Grey_fantasy29 分钟前
高级编程之结构化代码
java·spring boot·spring cloud
弗锐土豆36 分钟前
工业生产安全-安全帽第二篇-用java语言看看opencv实现的目标检测使用过程
java·opencv·安全·检测·面部
Elaine20239136 分钟前
零碎04 MybatisPlus自定义模版生成代码
java·spring·mybatis
小小大侠客1 小时前
IText创建加盖公章的pdf文件并生成压缩文件
java·pdf·itext
一二小选手1 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis