leetcode79
class Solution {
public boolean exist(char[][] board, String word) {
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[0].length; j++) {
if(exist(board, i, j, word, 0))
return true;
}
}
return false;
}
public boolean exist(char[][] board, int x, int y, String word, int index) {
if(index == word.length()) return true;
if(x < 0 || y<0 || x>=board.length || y>=board[0].length || board[x][y] != word.charAt(index))
return false;
board[x][y] ^= 128;
boolean exist = exist(board, x-1, y, word, index+1) ||
exist(board, x+1, y, word, index+1) ||
exist(board, x, y-1, word, index+1) ||
exist(board, x, y+1, word, index+1) ;
board[x][y] ^= 128;
return exist;
}
}
补充一个python的实现:
class Solution:
def dfs(self,board,word,index,rows,coloums,visited,i,j):
if index >= len(word):
return True
if i >= rows or i < or j >= coloums or j < or visited[i][j] == or board[i][j] != word[index]:
return False
visited[i][j] =
result = self.dfs(board,word,index+,rows,coloums,visited,i+,j) or self.dfs(board,word,index+,rows,coloums,visited,i-,j) or self.dfs(board,word,index+,rows,coloums,visited,i,j+) or self.dfs(board,word,index+,rows,coloums,visited,i,j-) visited[i][j] = return result def exist(self, board: 'List[List[str]]', word: 'str') -> 'bool':
rows = len(board)
coloums = len(board[])
visited = [[ for a in range(coloums)] for b in range(rows)]
for i in range(rows):
for j in range(coloums):
if self.dfs(board,word,,rows,coloums,visited,i,j):
return True
return False
leetcode79的更多相关文章
- 【leetcode79】Single Number III
题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...
- [Swift]LeetCode79. 单词搜索 | Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【1】【leetcode-79】 单词搜索
(典型dfs,知道思想写错) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单 ...
- Leetcode79 Word Search
题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题
笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...
随机推荐
- java类的高级概念
- 使用Java API方式连接HDFS Client测试
IDEA中新建Maven工程,添加POM依赖, 在IDE的提示中, 点击 Import Changes 等待自动下载完成相关的依赖包. <?xml version="1.0" ...
- Appium·基础+项目
date:2018609 day13 一.Appium Appium是一个开源.跨平台的测试框架.可以用来测试原生以及混合的移动端应用. 1.安装操作 ①.安装Appium-Python-Client ...
- oracle 简单crud
-- 商品表 -- CREATE TABLE "SCOTT"."GOODS" ( "id" NUMBER NOT NULL, "n ...
- C++学习(三十五)(C语言部分)之 单链表
单链表 就好比火车 火车头-->链表头部火车尾-->链表尾部火车厢-->链表的节点火车厢连接的部分-->指针火车中的内容-->链表节点的的数据链表节点包含数据域和指针域数 ...
- FTP服务-filezilla server 配置
一.下载Filezilla Server 官网网址:https://filezilla-project.org/download.php?type=server 二.安装Filezilla Ser ...
- 19/03/30Python笔记
一.三元运算 a = 1 if (条件) else a = 2 #如果条件成立,a = 1,否则a = 2 二.文件的处理 1.读取 f = open("user.txt",&qu ...
- 学习笔记TF050:TensorFlow源代码解析
TensorFlow目录结构. ACKNOWLEDGMENTS #TensorFlow版本声明 ADOPTERS.md #使用TensorFlow的人员或组织列表 AUTHORS #TensorFlo ...
- Oracle的导入和导出
导出命令: EXP 用户名/密码@数据库名 BUFFER=64000 file=G:\dmp\full1.dmp OWNER=用户名 导入命令: IMP 用户名/密码@数据库名 BUFFER=64 ...
- C++类中一个构造函数调用另一个构造函数
class A { int a; int b; int c; public: A(int aa, int bb) : a(aa), b(bb),c(0) { cout << "a ...