首页 > 其他分享 >[剑指offer] 回溯篇

[剑指offer] 回溯篇

时间:2023-09-16 11:12:04浏览次数:40  
标签:offer int res nextY static 回溯 nextX public

JZ12 矩阵中的路径

 1 /* DFS */
 2 public class JZ12_1
 3 {
 4     public static boolean[][] vis;
 5     public static int[] dx = new int[]{-1, 1, 0, 0};
 6     public static int[] dy = new int[]{0, 0, -1, 1};
 7 
 8     public static boolean hasPath(char[][] matrix, String word)
 9     {
10         int m = matrix.length, n = matrix[0].length;
11         vis = new boolean[m][n];
12         for (int i = 0; i < m; i++)
13             for (int j = 0; j < n; j++)
14                 if (dfs(matrix, word, i, j, 0)) return true;
15         return false;
16     }
17 
18     public static boolean dfs(char[][] matrix, String word, int curX, int curY, int idx)
19     {
20         int m = matrix.length, n = matrix[0].length, nextX = -1, nextY = -1;
21         boolean res = false;
22         if (matrix[curX][curY] != word.charAt(idx)) return false;
23         if (idx == word.length() - 1) return true;
24         vis[curX][curY] = true;
25         for (int i = 0; i < 4; i++)
26         {
27             nextX = curX + dx[i];
28             nextY = curY + dy[i];
29             if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || vis[nextX][nextY])
30                 continue;
31             res = res || dfs(matrix, word, nextX, nextY, idx + 1);
32         }
33         vis[curX][curY] = false;
34         return res;
35     }
36 }

JZ13 机器人的运动范围

 1 /* DFS */
 2 public class JZ13_1
 3 {
 4     public static boolean[][] vis;
 5     public static int[] dx = new int[]{-1, 1, 0, 0};
 6     public static int[] dy = new int[]{0, 0, -1, 1};
 7 
 8     public static int movingCount(int threshold, int rows, int cols)
 9     {
10         vis = new boolean[rows][cols];
11         return dfs(threshold, rows, cols, 0, 0);
12     }
13 
14     public static int dfs(int threshold, int m, int n, int curX, int curY)
15     {
16         int nextX = -1, nextY = -1, res = 1;
17         vis[curX][curY] = true;
18         for (int i = 0; i < 4; i++)
19         {
20             nextX = curX + dx[i];
21             nextY = curY + dy[i];
22             if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || vis[nextX][nextY] || cal(nextX) + cal(nextY) > threshold)
23                 continue;
24             res += dfs(threshold, m, n, nextX, nextY);
25         }
26         return res;
27     }
28 
29     public static int cal(int nums)
30     {
31         int res = 0;
32         while (nums != 0)
33         {
34             res += nums % 10;
35             nums /= 10;
36         }
37         return res;
38     }
39 }

标签:offer,int,res,nextY,static,回溯,nextX,public
From: https://www.cnblogs.com/VividBinGo/p/17706459.html

相关文章

  • 代码随想录算法训练营-回溯算法|455. 分发饼干、376. 摆动序列
    1.贪心算法一般分为如下四步:将问题分解为若干个子问题找出适合的贪心策略求解每一个子问题的最优解将局部最优解堆叠成全局最优解 455. 分发饼干1.局部最优就是大饼干喂给胃口大的,充分利用饼干尺寸喂饱一个,全局最优就是喂饱尽可能多的小孩。时间复杂度:O(nlogn)空间......
  • [剑指offer] 模拟篇
    JZ29 顺时针打印矩阵1/*模拟*/2publicclassJZ29_13{4publicstaticArrayList<Integer>printMatrix(int[][]matrix)5{6ArrayList<Integer>res=newArrayList<>();7intleft=0,right=matrix[0].length-......
  • [剑指offer] 链表篇
    JZ6从尾到头打印链表1/*从尾到头递归*/2publicclassJZ6_13{4privatestaticArrayList<Integer>res=newArrayList<>();56publicstaticArrayList<Integer>printListFromTailToHead(ListNodelistNode)7{8......
  • 代码随想录算法训练营第8天| ● 344.反转字符串 ● 541. 反转字符串II ● 剑指Offer 0
    344.反转字符串mydemo--(一次就过)--(成功)classSolution{public:voidreverseString(vector<char>&s){intlen=s.size();chartmp;inti=0;intj=len-1;while(i<j){tmp=s[i];......
  • 剑指Offer面试题3题目二:不修改数组找出重复的数字
    一、题目在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。例如,如果输入长度为8的数组{2,3,5,4,3,2,6,7},那么对应的输出是重复的数字2或3。输入参数:一个整数数组numbers,......
  • 回溯理论
    回溯本质:  回溯模板: ......
  • 剑指 Offer 41. 数据流中的中位数
    classMedianFinder{public:/**initializeyourdatastructurehere.*///注意小根堆的定义方式priority_queue<int,vector<int>,greater<int>>up;//小根堆,默认放从大到小后一半的数priority_queue<int>down;//大根堆,默认放从小到大排序前一半......
  • 爆肝总结2023Android面试,看完学会它,公司追着给你offer
    前言想要在面试中脱颖而出吗?想要在最短的时间内快速掌握Android的核心知识点吗?想要成为一位优秀的Android工程师吗?本篇文章能助你一臂之力!金九银十,目前正值招聘求职旺季,很多朋友对一些新技术名词都能侃侃而谈,但对一些核心原理理解的不够透彻,特别是对Android的一些核心基础知识点掌......
  • 剑指offer面试题3:数组中重复的数字
    一、知识点:数组相关知识二、描述在一个长度为n的数组里的所有数字都在0~n-1范围内,数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道重复几次。请找出数组中任意一个重复的数字,例如:输入长度为7的数组[2,3,2,4,3,3,5],那么输出2或者3都是正确的,存在不合法的输入的话输出-1。......
  • 剑指 Offer 67. 把字符串转换成整数
    题目链接:剑指Offer67.把字符串转换成整数题目描述:写一个函数StrToInt,实现把字符串转换成整数这个功能。不能使用atoi或者其他类似的库函数。解法思路:直接模拟题代码:funcstrToInt(sstring)int{s=strings.Trim(s,"")minus:=1varansint64=......