LeetCode 79. Word Search

原题链接在这里:https://leetcode.com/problems/word-search/

题目:

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[['A','B','C','E'],['S','F','C','S'],['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

题解:

类似N-Queens. dfs, 终止条件若能走到index == word.length(), 返回true.

若是做不到就是中间出现了越界或者不等,返回false.

用之前要把used 对应位置改为true, 用完后记得把used 对应位置改为false. 

最后返回res.

Time Complexity: O(m^2 * n^2).  对于board每一个点search都是一次dfs, dfs用了O(V+E), 矩阵表示时就是O(m*n). 并且board一共有m*n个点.

Space: O(m*n). 因为用了used存储。这里m=board.length, n = board[0].length.

AC Java:

 1 public class Solution {
 2     public boolean exist(char[][] board, String word) {
 3     if(word==null || word.length()==0)  
 4         return true;  
 5     if(board==null || board.length==0 || board[0].length==0)  
 6         return false;  
 7     boolean[][] used = new boolean[board.length][board[0].length];  
 8     for(int i=0;i<board.length;i++)  
 9     {  
10         for(int j=0;j<board[0].length;j++)  
11         {  
12             if(search(board,word,0,i,j,used))  
13                 return true;  
14         }  
15     }  
16     return false;  
17 }  
18 private boolean search(char[][] board, String word, int index, int i, int j, boolean[][] used)  
19 {  
20     if(index == word.length())  
21         return true;  
22     if(i<0 || j<0 || i>=board.length || j>=board[0].length || used[i][j] || board[i][j]!=word.charAt(index))  
23         return false;  
24     used[i][j] = true;  
25     boolean res = search(board,word,index+1,i-1,j,used)   
26                 || search(board,word,index+1,i+1,j,used)  
27                 || search(board,word,index+1,i,j-1,used)   
28                 || search(board,word,index+1,i,j+1,used);  
29     used[i][j] = false;  
30     return res;  
31     }
32 }

 跟上Word Search II

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4944270.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404770.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux shell 字符串操作(长度,查找,替换)详解

在做shell批处理程序时候&#xff0c;经常会涉及到字符串相关操作。有很多命令语句&#xff0c;如&#xff1a;awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号&#xff0c;可以达到类似效果&#xff0c;大家知道&#xff0c;使用内部操作符会省略启动外部程序等…

深入浅出UML类图

From: http://www.uml.org.cn/oobject/201211231.asp 在UML 2.0的13种图形中&#xff0c;类图是使用频率最高的UML图之一。Martin Fowler在其著作《UML Distilled: A Brief Guide to the Standard Object Modeling Language, Third Edition》&#xff08;《UML精粹&#xff1a;…

lib60870-IEC 60870-5-101 / 104 协议对总查询的处理

1、描述 服务器端使用InterrogationHandler回调函数来处理Interrogation请求。根据QOI&#xff08;询问限定符&#xff09;的值&#xff0c;返回不同的信息对象。对于一个简单的系统&#xff0c;仅处理 总查询 请求就足够了&#xff08;QOI 20&#xff09;。QOI值21-36用于询问…

[react] React16新特性有哪些?

[react] React16新特性有哪些&#xff1f; 1.使用Error Boundary处理错误组件 2.render支持2种新的返回类型(数组、字符串) 3.使用createProtal 将组件渲染到当前组件树之外 4.自定义DOM属性 &#xff1a;把不会识别的属性传递给DOM 5.setState传入null时不会再触发更新 个人…

linux笔记1(创建用户、安装gcc、安装五笔输入法)

linux笔记1(创建用户、安装gcc、安装五笔输入法) linux简说 内核&#xff1a;在计算机启动时载入基本内存&#xff0c;管理一些基本的输入输出&#xff0c;管理一些进程的初始化以及进程之间的调 试。控制硬件的运行。 Shell:系统的命令解释器&#xff0c;用户进程与kernel的桥…

Qt 按钮菜单

settingMenu new QMenu;updateAction new QAction(settingMenu); updateAction->setText("更新");authenAction new QAction(settingMenu); authenAction->setText("认证");aboutAction new QAction(settingMenu); aboutAction->setText(&quo…

[转]使用 LDAP 组或角色限制访问,包含部分单点登录SSO说明

参考&#xff1a;http://www-01.ibm.com/support/knowledgecenter/api/content/SSEP7J_10.2.2/com.ibm.swg.ba.cognos.crn_arch.10.2.2.doc/c_restrict_access_using_ldap_groups_or_roles.html#Restrict_Access_Using_LDAP_Groups_or_Roles?localezh 使用 LDAP 组或角色限制访…

[react] 在React中如何判断点击元素属于哪一个组件?

[react] 在React中如何判断点击元素属于哪一个组件&#xff1f; 首先 import {findDOMNode} from react-dom <div onClick{(e)>{ e.target findDOMNode(this.refs.xxxx) }}> <SubcomPonent ref"xxxx" /> </div> 注意&#xff0c;重点是 findD…

samba加入windows 2003域

1。vi /etc/resolv.conf nameserver 192.168.2.110 &#xff03; linux 加入 windows 2003 域&#xff0c;也要在 Linux 将 DNS 服务器的地址指向 windows 域的 DNS 服务器&#xff0c;默认一般就是域控制器,如果没有在Linux中设置 DNS 服务器地址的话&#xff0c;…

[react] 举例说明什么是高阶组件(HOC)的反向继承

[react] 举例说明什么是高阶组件(HOC)的反向继承 import React from react;const hoc (WrappedComponent) > {// 集成需要包装的 Componentreturn class extends WrappedComponent {constructor(props) {super(props);}// 重写 component 生命周期componentDidMount() {co…

TI基于MSP430F67641的电能表技术方案

1、三相四线硬件架构 1.1 阻容降压供电 1.2 电压输入 1.3 电流输入 2、RMS有效值计算 3、计量参数算法 3.1 有功和无功 3.2 视在功率 3.3 三相总功率 3.4 电能 3.5 频率 3.6 功率因数

函数模板特化(一)

/*演示: 模板函数及其特化函数的使用 */ #include <stdio.h> #include <iostream> #include <string> #include <map>using namespace std;template<typename T> T add(T n1, T n2) // 通用模板函数 {return n1 n2; }//template<>…

Oracle查看对象依赖关系

查看table或view是否被其他程序依赖。select * from user_dependencies t where t.referenced_name TABLE_NAME;当然也可以在其它方案对该表的依赖关系 select * from all_dependencies t where t.referenced_name TABLE_NAME;

[react] React中怎么操作虚拟DOM的Class属性

[react] React中怎么操作虚拟DOM的Class属性 render() { this.debug(render ....); this.components.push(<AttentionScreen ref"attention"message{this.state.message} />); this.components.forEach(function(component, index) { if(index > 0 &&a…

类模板特化之经典(一)

#include <iostream> using namespace std;template<int x> class sum {public:enum { number x sum<x-1>::number }; };template<> class sum<0> // 模板特化1 {public:enum { number 0 }; };template<> class sum<1> …

comsol 多物理场仿真流程-以开关柜为例

1、主要方法 在SolidWorks中对于开关柜的内部与外部结构进行建模&#xff0c;生成能够导入软件计算的3D模型。将SolidWorks中的模型导入comsol软件中&#xff0c;设置参数进行多物理场耦合仿真。 1、具体步骤 删除高压开关柜中对其内部电磁环境无较大影响的结构&#xff0c;如…

平板电脑选择

今天看了看网上关于平板电脑的一些问题&#xff0c;总结如下&#xff1a; 1、操作系统 &#xff08;1&#xff09;WindowXP/Vista/7/8 微软Windows操作系统是典型的X86架构的操作系统&#xff0c;只应用于X86架构的平板电脑&#xff0c;优点是PC软件不用改程序了&#xff0c;直…

2.UiSelector API 详细介绍

一、UiSelector类介绍 //通过各种属性与节点关系定位组件简单实例&#xff1a;public void testDemo2() throws UiObjectNotFoundException{UiSelector l new UiSelector().text("相机");//搜索条件UiObject objectnew UiObject(l);//一句搜索条件找到对象object.cli…

[react] render在什么时候会被触发?

[react] render在什么时候会被触发&#xff1f; 在 React 中&#xff0c;只要执行了 setState 方法&#xff0c;就一定会触发 render 函数执行&#xff1b; 组件的 props 改变了&#xff0c;不一定触发 render 函数的执行&#xff0c;除非 props 的值来自于父组件或者祖先组件…

C++类模板的三种特化类型

From: http://blog.sina.com.cn/s/blog_65d069c601010fb3.html 说起C的模板及模板特化&#xff0c; 相信很多人都很熟悉 &#xff0c;但是说到模板特化的几种类型&#xff0c;相信了解的人就不是很多。我这里归纳了针对一个模板参数的类模板特化的几种类型&#xff0c; 一是特化…