又是一年秋招季
后台回复【java面试】获取3G面试资料
后台回复【python面试】获取面试资料
1. 题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。
2. 示例
例如
这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
3. 解题思路
采用回溯法(探索与回溯法),它是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。
基本思想:
0.根据给定数组,初始化一个标志位数组,初始化为false,表示未走过,true表示已经走过,不能走第二次
1.根据行数和列数,遍历数组,先找到一个与str字符串的第一个元素相匹配的矩阵元素,进入judge
2.根据i和j先确定一维数组的位置,因为给定的matrix是一个一维数组
3.确定递归终止条件:越界,当前找到的矩阵值不等于数组对应位置的值,已经走过的,这三类情况,都直接false,说明这条路不通
4.若pathLength,就是待判定的字符串str的索引已经判断到了最后一位,此时说明是匹配成功的
5.下面就是本题的精髓,递归不断地寻找周围四个格子是否符合条件,只要有一个格子符合条件,就继续再找这个符合条件的格子的四周是否存在符合条件的格子,直到k到达末尾或者不满足递归条件就停止。
6.走到这一步,说明本次是不成功的,我们要还原一下标志位数组index处的标志位,进入下一轮的判断。
4. Java实现
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str){
int size = matrix.length;
if (rows * cols != size || size == 0 || str.length==0) return false;
// 初始化时,全部元素为 false
boolean[] visited = new boolean[size]; // 如果被访问,则位true,记录matrix元素访问
int pathLength = 0; // 记录遍历的索引值
for (int i = 0; i for (int j = 0; j // 如果有一条路径满足,则为 true
if (hasPathHelper(matrix, rows, cols, str, i, j, visited, pathLength)){
return true;
}
}
}
return false;
}
private boolean hasPathHelper(char[] matrix, int rows, int cols, char[] str, int x, int y, boolean[] visited, int pathLength){
if (str.length == pathLength){
return true;
}
// 使用递归回溯的方法
int index = x * cols + y; // matrix的索引
boolean result = false;
// 最后2个满足的条件是:没有别访问,且找到指定字符
if (0 <= x && x 0 <= y && y false && matrix[index]== str[pathLength]){
pathLength += 1;
visited[index] = true;
// 遍历上下左右 四个方位
boolean x1 = hasPathHelper(matrix, rows, cols, str, x-1, y, visited, pathLength);
boolean x2 = hasPathHelper(matrix, rows, cols, str, x+1, y, visited, pathLength);
boolean x3 = hasPathHelper(matrix, rows, cols, str, x, y-1, visited, pathLength);
boolean x4 = hasPathHelper(matrix, rows, cols, str, x, y+1, visited, pathLength);
result = x1 || x2 || x3|| x4;
if (result == false){ //回溯上一层
pathLength -= 1;
visited[index] = false;
}
}
return result;
}
}
5. Python实现
# -*- coding:utf-8 -*-
class Solution:
def hasPath(self, matrix, rows, cols, path):
# write code here
if not matrix or not path or len(matrix) != rows * cols:
return False
visited = [False] * len(matrix)
pathLength = 0 #存储path的长度
for i in range(rows):
for j in range(cols):
if self.search(matrix, rows, cols, path, j, i, visited, pathLength):
return True
return False
def search(self, matrix, rows, cols, path, x, y, visited, pathLength):
if len(path) == pathLength:
return True
# 其中 x代表了列数,y代表了行数
result = False
index = y * cols + x
#判断是否满足递归条件
#需要满足倒数第2个条件是 其个字符是要相同的
if 0 <= x and 0 <= y and path[pathLength] == matrix[index] and not visited[index]:
pathLength += 1
visited[index] = True
x1 = self.search(matrix, rows, cols, path, x-1, y, visited, pathLength)
x2 = self.search(matrix, rows, cols, path, x, y-1, visited, pathLength)
x3 = self.search(matrix, rows, cols, path, x+1, y, visited, pathLength)
x4 = self.search(matrix, rows, cols, path, x, y+1, visited, pathLength)
result = x1 or x2 or x3 or x4
if not result:
pathLength -= 1
visited[index] = False
return result
如果您觉得本文有用,请点个“赞”
你点的每一个在看,我都认真当成了喜欢