当前位置:网站首页>每日一题(回溯)
每日一题(回溯)
2022-07-28 14:16:00 【梦鱼yx】
①、题目
饼干:
力扣
https://leetcode.cn/problems/assign-cookies/
路径:
力扣
https://leetcode.cn/problems/all-paths-from-source-to-target/
②、解题
饼干:
class Solution {
public int findContentChildren(int[] g, int[] s) {
//尽量满足胃口小的小孩,并在此基础上尽量用小的饼干
Arrays.sort(g);
Arrays.sort(s);
int answer=0;
for (int i=0,j=0; i < g.length && j < s.length; j++) {
if (g[i]<=s[j]){
answer++;
i++;
}
}
return answer;
}
}路径:
class Solution {
private int[][] graph;
private List<List<Integer>> answer=new ArrayList<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
this.graph=graph;
ArrayList<Integer> list = new ArrayList<>();
list.add(0);
dfc(0,list);
return answer;
}
private void dfc(int a,List<Integer> temp){
//来到第n-1个节点:满足,终止
if (a==graph.length-1)
//不能直接将temp添加,否则此后temp变化,answer里面也跟着变
//因为二者指向同一个地址
answer.add(new ArrayList<Integer>(temp));
for (int i = 0; i < graph[a].length; i++) {
temp.add(graph[a][i]);
dfc(graph[a][i],temp);
//回溯:
temp.remove(temp.size()-1);
}
}
}③、运行截图

边栏推荐
- Iframe tag
- Gradle -- package multiple variants with gradle
- 3511. 倒水问题
- No files or folders found to process
- Buuctf partial solution
- SQL error [1810] [22008]: ora-01810: format code occurs twice
- shellcode编写学习-环境
- [complete installation package & tutorial] sqlserver basic installation_ Sqlserver completely uninstalled_ Sqlserver custom installation_ Getting started with sqlserver_ SQLSERVER database
- Analysis vulnerability introduction
- 软件开发三大痛点!小程序容器如何解决?
猜你喜欢

MLX90640 红外热成像仪传感器模块开发笔记(八)

经典Dijkstra与最长路

Talk about low code / zero code tools
![What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]](/img/a9/336390db64d871fa1655800c1e0efc.png)
What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]

PS how to crop photos

JS study notes 18-23

Hard disk partition method

3588. 排列与二进制

Chapter I Introduction

Image steganography method
随机推荐
Specific operation process of network security emergency response
Install scikit learn journey
Solution: attributeerror: type object 'h5py.h5a AttrID has no attribute __ reduce_ cython__
SystemVerilog
Idea2020.1.4 packages package collapse
Three pain points of software development! How to solve the applet container?
Three pop-up boxes commonly used in JS
Pytorch GPU installation
SQL learning
Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas
Downloading PIP package is too slow
Solve blast database error: error pre fetching sequence data
iframe 标签
【游戏测试工程师】初识游戏测试—你了解它吗?
Picture Trojan principle production prevention
安全与隐私计算在国内发展现状
4、 C language operators
How to conduct risk assessment related to intellectual property rights
即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
Keras reported an error using tensorboard: cannot stop profiling