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的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...
随机推荐
- Spring 源码学习(1)—— 容器的基本实现
最近在读Spring的源码,参考的是郝佳的<Spring源码深度解析>,这里把一些学习心得分享一下,总结的地方可能还有一些不完善,希望大家指教 IoC(控制反转)是Spring的特性之一, ...
- ubuntu 网络配置
检查网络配置命令:ifconfig 一.通过配置文件配置 新手没怎么用过Ubuntu,所以走了不少弯路,网上找了很多方法,大都没对我起到帮助作用,所以把自己的配置方法写一写. Ubuntu上连了两块网 ...
- 蓝牙协议分析(10)_BLE安全机制之LE Encryption
1. 前言 前面文章介绍了两种BLE的安全机制:白名单[4]和LL privacy[3].说实话,在这危机四伏的年代,这两种“捂着脸讲话(其它人不知道是谁在讲话,因而不能插话.不能假传圣旨,但讲话的内 ...
- JavaBasic_11
Object默认的实现是比较对象的地址 Object默认的实现是比较对象的地址局部内部类 局部位置内部类:局部是指方法体中 1.可以直接访问外部类的成员(这个特征是所有内部类所共有) 2.可以创建内部 ...
- 浅谈HTTP和HTTPS的区别
这篇随笔我们从六个步骤来学习: 1.了解HTTP和HTTPS的基本概念 2.HTTPS诞生的目的 3.HTTP与HTTPS的区别 4.HTTP和HTTPS的工作原理 5.HTTPS的优缺点 6.如何将 ...
- vue2.0 父子组件通信 兄弟组件通信
父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...
- cmd运行jar
https://blog.csdn.net/lemon_tree12138/article/details/44081909 https://www.cnblogs.com/LanTianYou/p/ ...
- C++学习(三十七)(C语言部分)之 链式栈(推箱子实现)
用链表实现栈一开始在表头插入,就要一直在表头插入一开始在表尾插入,就要一直在表头插尾表头当栈底 也可以把表尾当栈底 实现的测试代码笔记如下: #include<stdio.h> #incl ...
- bzoj 2460 线性基
#include<bits/stdc++.h> #define ll long long #define LL long long #define int long long using ...
- arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression
在裁剪uboot的时候出现下面错误: LDS u-boot.lds LD u-boot arm-linux-: ignoring invalid character `#' in expression ...