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的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...
随机推荐
- Problem D: 类的初体验(IV)
Description 定义一个类Data,只有一个int类型的属性和如下方法: 1. 缺省构造函数,将属性初始化为0,并输出"Data's default constructor.&q ...
- 移动端H5拍照代码实现及外网部署
最近的工作中,遇到了一个需求:对于无APP登陆权限的人员,提供拍照上传功能,以便生成更完善的出工记录.经研究讨论,决定实现的机制为:由合法的人员登陆APP认领相关工作任务,并生成当天当工作的唯一二维码 ...
- Travel notes in Vietnam
I‘m back:) For preparing IELTS exam, I haven't written article for over 70 days. It's a good exper ...
- 剑指Offer 56. 删除链表中重复的结点 (链表)
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- RabbitMQ学习之旅(一)
RabbitMQ学习总结(一) RabbitMQ简介 RabbitMQ是一个消息代理,其接收并转发消息.类似于现实生活中的邮局:你把信件投入邮箱的过程,相当于往队列中添加信息,因为所有邮箱中的信件最终 ...
- leetcode 846.Hand of Straights
对于一个数组中的数分为W组且在每一组内的数是连续存在的. 考虑使用map映射来记录每个数的个数的,并且对于数组中的数进行从小到大的排列的.同时每次需要更新最开始的那个起始数的,可能是以及出现的也可能是 ...
- /etc/profile ~/.bash_profile ~/.bashrc 等文件的执行过程 和 区别
/etc/profile :系统的所有用户共享 ~/.bash_profile:仅针对当前用户有效 我的电脑只用于开发,因此全部配置/etc/profile 参考链接: https://blog.cs ...
- Burpsuite 1.7.33启动的一点小问题。
最近用的burp到期了,就想找个新版本的. https://www.52pojie.cn/thread-691448-1-1.html 上面帖子里贴了最新的几个版本的burp,下载以后,问题出现了,无 ...
- Qt配置cmake;运行带参数的程序
配置cmake编译器,步骤如下: 步骤1: Qt下新建一个project. 步骤2: 在该project目录下创建一个CMakeLists.txt文件,并按规范编写该文件. Tip: projec ...
- C现代编程
1.C语言没有像面向对象语言一样提供访问控制的功能,无法实现数据隐藏,可以通过规定成员命名来规避这个问题,例如不允许直接访问的成员以“_”开头. 2.模板模式,利用函数指针,抽离固有代码,差异代码放到 ...