当前位置:网站首页>Leetcode7 DFS + dynamic programming + double pointer
Leetcode7 DFS + dynamic programming + double pointer
2022-07-19 04:12:00 【Fairy wants carry】
DFS

Ideas : Traverse the two-dimensional array of Islands , If the current number is 1, Then enter the infection function and count the number of Islands +1
Infection function : In fact, it is a recursive annotation process , It will connect all connected 1 All marked as 2. Why label ? This avoids repeated counting during traversal , All of an island 1 All become 2 after , When traversing, there will be no repeated traversal . Suggest students who don't understand draw a picture to see .
class Solution {
public int numIslands(char[][]grid){
int islandNum=0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
// There is a position of 1 That's the island dfs Judge
if(grid[i][j]=='1'){
// recursive
dfs(grid,i,j);
islandNum++;
}
}
}
return islandNum;
}
// Recursive function
public void dfs(char[][]grid,int i,int j){
//1. The end condition
if(i<0||i>= grid.length||j<0||j>=grid[0].length||grid[i][j]!='1'){
return;
}
//2. If the conditions are met, change the number to 2
grid[i][j]='2';
//3. recursive
dfs(grid,i+1,j);
dfs(grid,i-1,j);
dfs(grid,i,j+1);
dfs(grid,i,j-1);
}
}Dynamic programming

class Solution {
// The sliding window
public int fib(int n) {
if(n<2) return n;
int a=0,b=1,c=0;
for(int i=1;i<n;i++){
c=a+b;
a=b;
b=c;
}
return c;
}
}The recursive method :
class Solution {
public int fib(int n) {
if(n<2){
return n;
}
return fib(n-1)+fib(n-2);
}
}
Dynamic programming :
class Solution {
public int uniquePaths(int m, int n) {
int[][] f = new int[m][n];
for (int i = 0; i < m; ++i) {
f[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
f[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
return f[m - 1][n - 1];
}
}
Double pointer ( This problem is not completely understood ,mad Since I can tear this question half a year ago , Now it turns out that the same solution has not been written )
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> ans = new ArrayList<>();
int n = s.length(), m = p.length();
int[] c1 = new int[26], c2 = new int[26];
for (int i = 0; i < m; i++) c2[p.charAt(i) - 'a']++;
for (int l = 0, r = 0; r < n; r++) {
c1[s.charAt(r) - 'a']++;
if (r - l + 1 > m) c1[s.charAt(l++) - 'a']--;
if (check(c1, c2)) ans.add(l);
}
return ans;
}
boolean check(int[] c1, int[] c2) {
for (int i = 0; i < 26; i++) {
if (c1[i] != c2[i]) return false;
}
return true;
}
}
Dynamic programming :
class Solution {
// Dynamic programming , That's sliding the window
public int climbStairs(int n) {
if(n<=2){
return n;
}
//1 and 2 floor
int a=1;
int b=2;
for(int i=3;i<=n;i++){
int temp=a+b;
a=b;
b=temp;
}
return b;
}
}边栏推荐
- How to realize the association between interfaces in JMeter?
- 小程序毕设作品之微信电子书阅读小程序毕业设计(4)开题报告
- Chapter 6 performance platform godeye source code analysis - Custom expansion module
- Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
- 最小生成树
- V4L2学习资料收集
- ffmpeg中AVFrame\AVPacket与自己的数据交互
- Unity Shader - “快速“ 次散射 (Fast SSS : Fast Subsurface Scattering)
- Unity - how to modify a package or localize it
- Laradock restart MySQL found
猜你喜欢
![[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education](/img/5e/8553594c0e496e87dbfee5f395cb43.jpg)
[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

C'est génial de jouer vscode comme un effet idea.

基于OpenVINO Model Server打造人像抠图服务器

Build a portrait matting server based on openvino model server

Chapter 3 performance platform godeye source code analysis - memory module

PAC Decade: witness HPC from CPU era to XPU Era

IDEA配置SFTP,SSH非常方便的部署以及定位错误日志

小程序毕设作品之微信电子书阅读小程序毕业设计(7)中期检查报告

Kubernetes learning persistent storage storageclass (4)
随机推荐
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
GNN系列 GCN简述 推导理解 及 DGL 源码解析
Chapter 0 performance platform godeye source code analysis - Course Introduction
Wechat online education video on demand learning applet graduation project (4) opening report
GNN series GCN brief derivation and understanding and DGL source code analysis
leetcode7-dfs+动态规划+双指针
Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino
小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
Laradock restart MySQL found
sql界面切换不能获取焦点
Introduction to PLC OPC information model (DI, PLCopen nodesets)
baddy:初始化内存域
FTXUI基础笔记(botton按钮组件基础)
巧用企业网盘收取报告或总结
[seventh issue of notebook series] download and use of openvino pre training model
To build agile teams, these methods are indispensable
[database] must know and be able at the end of the term ----- Chapter 11 concurrency control
1. PostgreSQL queries the data of nearly 24 hours according to the dynamic table name
笔记本电脑插入耳机仍然外放(亲测有效)
小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT