代码随想录35期Day24-Java

server/2024/9/23 9:11:01/

Day24 回溯算法

LeetCode77.组合:给出1-n之间每k个数的所有组合

核心思想:回溯,维护一个当前的元素集。注意结束条件

java">class Solution {// path存放当前元素List<Integer> path = new ArrayList<>();// 结果集resList<List<Integer> > res = new ArrayList<>(); public List<List<Integer>> combine(int n, int k) {// 回溯help(1,n,k);return res;}public void help(int start,int n ,int k ){// 如果从start开始到末尾的元素不足k个,则直接返回if(n-start +1< k)  return;if(k == 1){// k记录还 需要几个数字,如果只需要一个for(int i = start ; i <= n ; i ++){// 构建结果集path.add(i);res.add(new ArrayList(path));// 每一个结果都要回溯path.removeLast();}}else{// 否则就继续执行递归for(int i = start; i <= n ; i ++){path.add(i);help(i+1,n,k-1);//回溯path.removeLast();}}}}

LeetCode 51:N皇后问题,额外做的

核心思想:判断斜线是否存在皇后,这点需要画图想想关系。可以使用Boolean数组存储,也可使用for遍历已有元素查询斜线是否满足。Boolean数组我尝试失败了,目前使用for进行了遍历获取。

java">class Solution {List<String> path = new ArrayList<>();List<List<String>> res = new ArrayList<>();int count = 0;// 记录当前行是否有元素boolean[] column = new boolean[10];public List<List<String>> solveNQueens(int n) {help(n);return res;}public void help(int n){// count 记录当前有几行if(count == n){res.add(new ArrayList<>(path));return;}for(int i = 0 ; i < n ; i ++){if(valid(n,count,i)){// 构建当前值// 将这一行占有column[i] = true;// 构建 . . . Q 的形式char[] temp = new char[n];Arrays.fill(temp,'.');temp[i] = 'Q';// 加入pathpath.add(new String(temp));// 当前行数++count++;// 进行下一行的遍历help(n);// 下面都是回溯部分count--;column[i] = false;path.removeLast(); }}}public boolean valid(int n ,int x, int y){// 在这里判断当前这个点的 列、斜线 是否有元素// 行不用判断,因为我每一行固定只放一个元素,count作为行的索引if(column[y]) return false;// x 是第几行for(int i = 0 ; i < x; i ++){// 左斜线if(y-(x-i) >= 0 && path.get(i).charAt(y-(x-i)) == 'Q') return false;// 右斜线if(y+(x-i) < n && path.get(i).charAt(y+(x-i)) == 'Q') return false;}return true;}
}

http://www.ppmy.cn/server/21615.html

相关文章

拉普拉斯分布(Laplace Distribution)

Laplace Distribution 是尖峰长尾分布&#xff0c;其密度函数为&#xff1a; p ( x ∣ μ , b ) 1 2 b e − ∣ x − μ ∣ b p(x|\mu,b) \frac{1}{2b}e^{-\frac{|x-\mu|}{b}} p(x∣μ,b)2b1​e−b∣x−μ∣​ 其期望为 μ \mu μ。方差为 2 b 2 2b^2 2b2。在英伟达论文Prod…

JAVAEE—HTTPS和ssl证书

0[toc] 什么是HTTPS HTTPS 也是一个应用层协议. 是在 HTTP 协议的基础上引入了一个加密层. HTTP 协议内容都是按照文本的方式明文传输的. 这就导致在传输过程中出现一些被篡改的情况而HTTPS则是新采用加密的方式进行传输 为什么需要HTTPS 为什么要使用HTTPS呢&#xff1f;这…

Android Monkey工具介绍与使用

过于爽快的承认失败&#xff0c;就可能发觉不了曾经与正确非常接近。大家好&#xff0c;依旧是在翻看旧文档的时候&#xff0c;发现一篇关于Monkey的介绍和使用&#xff0c;Monkey这款工具在软件测试中主要用于进行压力测试和稳定性测试。它可以模拟大量随机的用户操作&#xf…

【Docker】docker部署lnmp和wordpress网站

环境准备 docker&#xff1a;192.168.67.30 虚拟机&#xff1a;4核4G systemctl stop firewalld systemctl disable firewalld setenforce 0 安装docker #安装依赖包 yum -y install yum-utils device-mapper-persistent-data lvm2 #设置阿里云镜像 yum-config-manager --add…

Android Room使用模板

1&#xff0c;引入依赖 plugins {id kotlin-kapt } dependencies {implementation "androidx.room:room-runtime:2.4.2"kapt "androidx.room:room-compiler:2.4.2" } 2&#xff0c;标记实体类 import androidx.room.Entity import androidx.room.PrimaryKe…

Golang特殊init函数

介绍 init()函数是一个特殊的函数&#xff0c;存在一下特性 不能被其它函数调用&#xff0c;而是子main()函数之前自动调用不能作为参数传入不能有传入参数和返回值 作用&#xff1a; 对变量进行初始化检查/修复程序状态注册运行一次计算 以下是<<the way to go>>…

wegame启动游戏错误代码126,加载x3daudio1_7.dll失败的修复教程

在尝试通过WeGame平台启动某款游戏时&#xff0c;遇到了阻碍&#xff0c;系统反馈了一个特定的错误代码“错误代码126&#xff0c;加载x3daudio1_7.dll失败”。这个错误提示表示游戏无法加载x3daudio17.dll文件&#xff0c;导致游戏无法正常启动。经过一番研究和尝试&#xff0…

【git学习】Git 的基本操作

文章目录 &#x1f680;创建 Git 本地仓库&#x1f680;配置 Git&#x1f680;认识⼯作区、暂存区、版本库&#x1f680;添加⽂件操作 &#x1f680;创建 Git 本地仓库 仓库是进⾏版本控制的⼀个⽂件⽬录。我们要想对⽂件进⾏版本控制&#xff0c;就必须先创建⼀个仓库出来。 …