算法学习之剑指offer(十二)
一
题目描述
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
int[] record = new int[matrix.length];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
if(hasPathCore(matrix,rows,cols,i,j,str,0,record))
return true;
return false;
}
public boolean hasPathCore(char[] matrix, int rows, int cols,int i ,int j,char[] str,int k,int[] record )
{
int index = i*cols+j;
if (i < 0 || i >= rows || j < 0 || j >= cols || matrix[index] != str[k] || record[index] == 1)
return false;
if(k==str.length-1)
return true;
record[index]=1;
if(hasPathCore(matrix,rows,cols,i-1,j,str,k+1,record)||hasPathCore(matrix,rows,cols,i+1,j,str,k+1,record)
||hasPathCore(matrix,rows,cols,i,j+1,str,k+1,record)||hasPathCore(matrix,rows,cols,i,j-1,str,k+1,record))
return true;
record[index]=0;
return false;
}
}
二
题目描述
public class Solution {
public int movingCount(int threshold, int rows, int cols) {
boolean[][] flag = new boolean[rows][cols];//记录已到达过的格子
return helper(0, 0, rows, cols, flag, threshold);
}
private int helper(int i, int j, int rows, int cols, boolean[][] flag, int threshold) {
//出界、到过的、不符合的都不算,返回能到达数为0(即不允许执行后面的继续递归和+1操作)
if (i < 0 || i >= rows || j < 0 || j >= cols || numSum(i) + numSum(j) > threshold || flag[i][j])
return 0;
//到达新地方。后面return会去+1
flag[i][j] = true;
//不断查到四个方向还可以到多个个格子
return helper(i - 1, j, rows, cols, flag, threshold)
+ helper(i + 1, j, rows, cols, flag, threshold)
+ helper(i, j - 1, rows, cols, flag, threshold)
+ helper(i, j + 1, rows, cols, flag, threshold)
+ 1;//每到一个新地方+1
}
//计算符合条件所需的方法
private int numSum(int i) {
int sum = 0;
while (i >= 10) {
sum += i % 10;
i = i / 10;
}
sum += i;
return sum;
}
}
算法学习之剑指offer(十二)的更多相关文章
- 算法学习之剑指offer(二)
题目1 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. import java.util.Stack; public class Solution { ...
- 算法学习之剑指offer(十)
一 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...
- 算法学习之剑指offer(十一)
一 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. import java.util.*; ...
- 算法学习之剑指offer(九)
一 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). public class Solution ...
- 算法学习之剑指offer(五)
题目1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution ...
- 剑指offer十二之数值的整数次方
一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...
- 算法学习之剑指offer(八)
题目一 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没 ...
- 算法学习之剑指offer(六)
题目1 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.*; public cl ...
- 算法学习之剑指offer(四)
题目1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) /** public class TreeNode { int val = 0; Tree ...
随机推荐
- RedisTemplate.opsForValue 常用方法
RedisTemplate.opsForValue 常用方法 1.set(K key, V value) 新增一个字符串类型的值,key是键,value是值. redisTemplate.opsFor ...
- spring boot使用guava缓存
1.pom中插入依赖: <!--guava缓存cache--> <dependency> <groupId>com.google.guava</groupId ...
- Maven学习归纳(二)——几个常用命令解析
Maven的常用命令 第一次执行命令的时候,因为需要下载执行命令的基础环境,所以会从远程仓库下载该环境到本地仓库中 运行mvn命令,必须在pom.xml文件所在的目录 一. JavaProject的p ...
- Django之FBV和CBV的用法
FBV FBV,即 func base views,函数视图,在视图里使用函数处理请求. 以用户注册代码为例, 使用两个函数完成注册 初级注册代码 def register(request): &qu ...
- FreeSql 新手上路系列教程已发布在 cnblogs
FreeSql 是一个功能强大的对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.5+(QQ群:4336577) FreeSql采用MIT开源协议托管 ...
- 对android中ActionBar中setDisplayHomeAsUpEnabled和setHomeButtonEnabled和setDisplayShowHomeEnabled方法的理解
转自: http://blog.csdn.net/lovexieyuan520/article/details/9974929 http://blog.csdn.net/cyp331203/artic ...
- Python基础(十一)
今日主要内容 补充:三目运算 f-strings 迭代器 生成器 补充:三目运算 三目运算(三元运算)结构: 表达式1 if 条件表达式 else 表达式2 c = a if a > b els ...
- Day 14 查找文件 find
find 查找方式 1.按照名称进行查找 [root@oldboyedu ~]# find ./ -name "*eth0" 2.按照名称查找(不区分大小写) [root@oldb ...
- MOOC 数据库系统笔记(一):初步认识数据库系统
概述 什么是数据库 数据库是电子化信息的集合 数据库起源于规范化"表(Table)"的处理. Table:以按行按列形式组织及展现的数据. E.F.Codd,基于对"表( ...
- Vue学习之vue中的v-if,v-show,v-for
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...