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的更多相关文章

  1. 【leetcode79】Single Number III

    题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...

  2. [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 ...

  3. 【1】【leetcode-79】 单词搜索

    (典型dfs,知道思想写错) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单 ...

  4. Leetcode79 Word Search

    题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...

  5. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  6. python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题

    笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...

随机推荐

  1. windows2008 转 centos7 数据磁盘NTFS无损挂载

    转换时 原win硬盘里作为系统稳盘的硬盘必须重新格式化才能装机 数据盘在安装ntfs-3g可以直接挂载 几个重要命令: #lsblk  //查看硬盘情况 df -T 只可以查看已经挂载的分区和文件系统 ...

  2. redis 分布式锁实现

    我们实现的分布式锁,使用redis提供的SET NX功能,由于redis server的单线程模型,保证了天然并发安全. https://stackoverflow.com/questions/116 ...

  3. 关于windows下的makefile学习

    文中部分引用自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=408225 windows下makefile环境配置见于:http ...

  4. java 程序编译和运行过程

    java整个编译以及运行的过程相当繁琐,我就举一个简单的例子说明: Java程序从源文件创建到程序运行要经过两大步骤: 1.源文件由编译器编译成字节码(ByteCode): 2.字节码由java虚拟机 ...

  5. python写入txt文件时的覆盖和追加

    python写入文件时的覆盖和追加 在使用Python进行txt文件的读写时,当打开文件后,首先用read()对文件的内容读取,然后再用write()写入,这时发现虽然是用"r+" ...

  6. c# android 全局捕获未处理异常

    [Application] public class MyApp : Application { public MyApp(IntPtr javaReference, JniHandleOwnersh ...

  7. C# 线程安全集合

    转载 对于并行任务,与其相关紧密的就是对一些共享资源,数据结构的并行访问.经常要做的就是对一些队列进行加锁-解锁,然后执行类似插入,删除等等互斥操作. .NetFramework 4.0 中提供了一些 ...

  8. phpmyadmin nginx设置

    1,解压缩phpmyadmin4.2.8压缩包到/usr/local/phpMyAdmin 2,复制config.sample.inc.php为config.inc.php 3,修改nginx.con ...

  9. django基础 -- 8.cookie 和 session

    一. cookie 1.cookie 的原理 工作原理是:浏览器访问服务端,带着一个空的cookie,然后由服务器产生内容, 浏览器收到相应后保存在本地:当浏览器再次访问时,浏览器会自动带上Cooki ...

  10. 回顾ThreadLocal

    ThreadLocal作为解决特定场景下并发的一种方案,在Spring等框架及面试中经常会被问到,它是Java必须要掌握的基础知识之一. ThreadLocal类的作用是抽象线程内变量的抽象,这类对象 ...