美容医疗 网站建设宽屏网站宽度
web/
2025/10/2 8:39:13/
文章来源:
美容医疗 网站建设,宽屏网站宽度,微信公众平台开发技术,网站登录接口怎么做提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、力扣322. 零钱兑换二、力扣509. 斐波那契数三、力扣46. 全排列四、力扣51. N 皇后五、力扣52. N 皇后 II 前言 一、力扣322. 零钱兑换
class Solution {pu… 提示文章写完后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 前言一、力扣322. 零钱兑换二、力扣509. 斐波那契数三、力扣46. 全排列四、力扣51. N 皇后五、力扣52. N 皇后 II 前言 一、力扣322. 零钱兑换
class Solution {public int coinChange(int[] coins, int amount) {int[] dp new int[amount1];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. 斐波那契数
class Solution {public int fib(int n) {if(n 0 || n 1){return n;}int[] dp new int[n1];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. 全排列
class Solution {ListListInteger res new ArrayList();ListInteger path new ArrayList();boolean[] flag;public ListListInteger 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 皇后
class Solution {ListListString res new ArrayList();public ListListString 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){ListString 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,depth1, 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
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,depth1, 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;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85528.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!