博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法复习22
阅读量:3745 次
发布时间:2019-05-22

本文共 1959 字,大约阅读时间需要 6 分钟。

矩阵中的路径

import java.util.*;public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { if(matrix==null || matrix.length==0 || str==null || str.length==0 || matrix.length!=rows*cols || rows<=0 || cols<=0 || rows*cols < str.length) { return false ; } boolean[] visited = new boolean[rows*cols] ; int[] pathLength = {
0} ; for(int i=0 ; i<=rows-1 ; i++) { for(int j=0 ; j<=cols-1 ; j++) { if(hasPathCore(matrix, rows, cols, str, i, j, visited, pathLength)) { return true ; } } } return false ; } public boolean hasPathCore(char[] matrix, int rows, int cols, char[] str, int row, int col, boolean[] visited, int[] pathLength) { boolean flag = false ; if(row>=0 && row
=0 && col

机器人的运动范围

public class Solution {
public int movingCount(int threshold, int rows, int cols) { boolean[] visited=new boolean[rows*cols]; return movingCountCore(threshold, rows, cols, 0,0,visited); } private int movingCountCore(int threshold, int rows, int cols, int row,int col,boolean[] visited) { if(row<0||row>=rows||col<0||col>=cols) return 0; int i=row*cols+col; if(visited[i]||!checkSum(threshold,row,col)) return 0; visited[i]=true; return 1+movingCountCore(threshold, rows, cols,row,col+1,visited) +movingCountCore(threshold, rows, cols,row,col-1,visited) +movingCountCore(threshold, rows, cols,row+1,col,visited) +movingCountCore(threshold, rows, cols,row-1,col,visited); } private boolean checkSum(int threshold, int row, int col) { int sum=0; while(row!=0){ sum+=row%10; row=row/10; } while(col!=0){ sum+=col%10; col=col/10; } if(sum>threshold) return false; return true; }}

源代码:

转载地址:http://thrin.baihongyu.com/

你可能感兴趣的文章
spark的API操作
查看>>
SparkSql
查看>>
SparkRdd-scala版本
查看>>
spark常见算子
查看>>
scala符号初体验
查看>>
kafka生产者常用参数含义
查看>>
mysql编写函数
查看>>
面试笔试题之hql
查看>>
sql函数之cast()
查看>>
hql中substr函数截取字符串匹配
查看>>
mysql之指定ip、用户、数据库权限
查看>>
zookeeper的读和写数据流程(有图欧)
查看>>
bin/schematool -dbType mysql -initSchema HiveMetaException: Failed to get schema version.
查看>>
flink知识总结
查看>>
flink之检查点(checkpoint)和保存点(savepoint)的区别
查看>>
Linux系统编程---进程I/O
查看>>
spring学习知识补充
查看>>
杂文之生成随机字符串
查看>>
springBoot基础(一)
查看>>
springBoot基础(二)
查看>>