力扣labuladong——一刷day06

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣322. 零钱兑换](#一、力扣322. 零钱兑换)
  • [二、力扣509. 斐波那契数](#二、力扣509. 斐波那契数)
  • [三、力扣46. 全排列](#三、力扣46. 全排列)
  • [四、力扣51. N 皇后](#四、力扣51. N 皇后)
  • [五、力扣52. N 皇后 II](#五、力扣52. N 皇后 II)

前言


一、力扣322. 零钱兑换

java 复制代码
class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i = 0; i < coins.length; i ++){
            for(int j = coins[i]; j <= amount; j ++){
                if(dp[j-coins[i]] != Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j],dp[j-coins[i]] + 1);//最大值加1就会变成负数了
                }
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1:dp[amount];
    }
}

二、力扣509. 斐波那契数

java 复制代码
class Solution {
    public int fib(int n) {
        if(n == 0 || n == 1){
            return n;
        }
        int[] dp = new int[n+1];
        dp[0] = 0;
        dp[1] = 1;
        for(int i = 2; i <= n; i ++){
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
}

三、力扣46. 全排列

java 复制代码
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permute(int[] nums) {
        flag = new boolean[nums.length];
        fun(nums);
        return res;
    }
    public void fun(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i ++){
            if(flag[i] == true){
                continue;
            }
            flag[i] = true;
            path.add(nums[i]);
            fun(nums);
            flag[i] = false;
            path.remove(path.size()-1);
        }
    }
}

四、力扣51. N 皇后

java 复制代码
class Solution {
    List<List<String>> res = new ArrayList<>();
    public List<List<String>> solveNQueens(int n) {
        char[][] ch = new char[n][n];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                ch[i][j] = '.';
            }
        }
        fun(ch,0,n);
        return res;
    }
    public void fun(char[][] ch, int depth,int n){
        if(depth == n){
            List<String> path = new ArrayList<>();
            for(char[] temp : ch){
                path.add(new String(temp));
            }
            res.add(path);
            return;
        }
        for(int i = 0; i < n; i ++){
            if(flag(ch, depth,i,n)){
                ch[depth][i] = 'Q';
                fun(ch,depth+1, n);
                ch[depth][i] = '.';
            }
        }
    }
    public boolean flag(char[][] ch, int row, int col, int n){
        //判断行
        for(int i = 0; i < col;i ++){
            System.out.println("row = " + row + " __ col = " + i);
            if(ch[row][i] == 'Q'){
                return false;
            }
        }
        //判断列
        for(int i = 0; i < row; i ++){
            if(ch[i][col] == 'Q'){
                return false;
            }
        }
        //判断45°
        for(int i = row, j = col; i >= 0 && j >= 0 ; i --, j --){
            if(ch[i][j] == 'Q'){
                return false;
            }
        }
        //判断135度
        for(int i = row, j = col; i >= 0 && j < n; i --, j ++){
            if(ch[i][j] == 'Q'){
                return false;
            }
        }
        return true;
    }
}

五、力扣52. N 皇后 II

java 复制代码
class Solution {
    int res = 0;
    public int totalNQueens(int n) {
        char[][] ch = new char[n][n];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                ch[i][j] = '.';
            }
        }
        fun(ch,0,n);
        return res;
    }
    public void fun(char[][] ch, int depth,int n){
        if(depth == n){
            res ++;
            return;
        }
        for(int i = 0; i < n; i ++){
            if(flag(ch, depth,i,n)){
                ch[depth][i] = 'Q';
                fun(ch,depth+1, n);
                ch[depth][i] = '.';
            }
        }
    }
    public boolean flag(char[][] ch, int row, int col, int n){
        //判断行
        for(int i = 0; i < col;i ++){
            System.out.println("row = " + row + " __ col = " + i);
            if(ch[row][i] == 'Q'){
                return false;
            }
        }
        //判断列
        for(int i = 0; i < row; i ++){
            if(ch[i][col] == 'Q'){
                return false;
            }
        }
        //判断45°
        for(int i = row, j = col; i >= 0 && j >= 0 ; i --, j --){
            if(ch[i][j] == 'Q'){
                return false;
            }
        }
        //判断135度
        for(int i = row, j = col; i >= 0 && j < n; i --, j ++){
            if(ch[i][j] == 'Q'){
                return false;
            }
        }
        return true;
    }
}
相关推荐
cur1es27 分钟前
【JVM类加载&双亲委派模型&垃圾回收机制】
java·jvm·gc·垃圾回收·类加载·双亲委派模型
Mr.朱鹏32 分钟前
JVM-GC垃圾回收案例
java·jvm·spring boot·算法·spring·spring cloud·java-ee
焦糖玛奇朵婷1 小时前
实测扭蛋机小程序:开发简单,互动有趣
java·大数据·程序人生·小程序·软件需求
Nan_Shu_6141 小时前
学习: 尚硅谷Java项目之小谷充电宝(3)
java·后端·学习
WJSKad12351 小时前
【DepthPro】实战教程:单目深度估计算法详解与应用
算法
wzqllwy1 小时前
8 大经典排序算法(Java 实现):原理 + Demo + 核心分析
java·算法·排序算法
智能工业品检测-奇妙智能1 小时前
AIFlowy如何实现与现有Spring Boot项目的无缝集成?
java·spring boot·后端
We་ct1 小时前
LeetCode 77. 组合:DFS回溯+剪枝,高效求解组合问题
开发语言·前端·算法·leetcode·typescript·深度优先·剪枝
從南走到北1 小时前
JAVA无人共享无人健身房物联网结合系统源码支持小程序+公众号+APP+H5
java·物联网·小程序
Nuopiane1 小时前
MyPal3(3)
java·开发语言