leetcode-单词探索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] 给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
思路:回溯算法,返回false的条件是探索到达了边界,已经探索过了,探索的字母与给定字符串的字符不相等。
探索是向上下左右探索,因此有四个回溯(bcakTrace())。
class Solution {
public boolean exist(char[][] board, String word) {
int rows=board.length; //长
int cols=board[0].length;
boolean res=false;
boolean[][] mark=new boolean[rows][cols];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(word.charAt(0)==board[i][j]){ //先找到字符串的起点
res=backTrace(0,word,board,i,j ,mark );
if(res==true)return true;
}
}
}
return false;
}
public boolean backTrace(int index,String word,char[][] board,int row,int col,boolean[][] mark){
if(index==word.length())return true; //回溯的边界条件
if(row>=board.length||row<0||col>=board[0].length||col<0)return false;
if(mark[row][col]==true||word.charAt(index)!=board[row][col])return false;
mark[row][col]=true;
if(backTrace(index+1,word,board,row+1,col,mark)||
backTrace(index+1,word,board,row-1,col,mark)||
backTrace(index+1,word,board,row,col+1,mark)||
backTrace(index+1,word,board,row,col-1,mark))
return true;
mark[row][col]=false; //每次探索至边界,不成立时,都要将标记清除
return false;
}
}
leetcode-单词探索的更多相关文章
- LeetCode —— 单词接龙(Python)
使用字典,降低查找的复杂度.使用list会超时. class Solution: def nextWordsList(self, word, wordDict): res_list = [] for ...
- 8月leetcode刷题总结
刷题链接:https://leetcode-cn.com/explore/ 根据leetcode的探索栏目,八月份一直在上面进行刷题.发现算法题真的好难,真-计算机思维. 核心是将现实问题转化为计算机 ...
- leetcode探索中级算法
leetcode探索中级答案汇总: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/ 1)数 ...
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- DZNSegmentedControl和XLForm联合使用
前言: 可能我还没有掌握IOS开发的精髓, 总感觉写ios代码像调bug, 任何一个功能开发完成之后总会有莫名其妙的问题, 最终这些问题很大概率会归结为"系统特性". 正文: 问题 ...
- iOS:UICollectionView流式布局及其在该布局上的扩展的线式布局
UICollectionViewFlowLayout是苹果公司做好的一种单元格布局方式,它约束item的排列规则是:从左到右依次排列,如果右边不够放下,就换一行重复上面的方式排放,,,,, 常用的 ...
- numpy如何使用
numpy介绍 创建numpy的数组 一维数组是什么样子 可以理解为格子纸的一行就是一个一维数据 two_arr = np.array([1, 2, 3]) 二维数组什么样子 理解为一张格子纸, 多个 ...
- php.ini修改后,重启无效
今天做项目,上传图,需要修改php.ini.发现修改后,多次长期服务器也没用,在网上找了好多方案.介绍一下我的流程 1.使用phpinfo()找到php.ini的位置,如果位置不准确,修改肯定没有任何 ...
- 电子相册之bitmap
位图文件主要分为3部分:1. 文件信息头 14Byte 2. 位图信息头 40Byte 3. RGB颜色阵列 由图像长宽尺寸决定 1. 文件信息头 定义结构体: typedef s ...
- day2-exercise
# Author: 刘佳赐-Isabelle October 22,2018 """ 1.有变量name = "aleX leNb" 完成如下操作: ...
- SQL盲注
一.首先输入1和-1 查看输入正确和不正确两种情况 二.三种注入POC LOW等级 ... where user_id =$id 输入 真 and 假 = 假 (1)...where u ...
- centos7 杂记
yum 源 https://www.cnblogs.com/renpingsheng/p/7845096.html 安装nginx php mysql https://www.cnblogs.com/ ...
- Python 爬虫 (四)
requests: 练手 雪qiu网 import requests import json import re import pymysql url = 'https://xueqiu.com/v4 ...
- C语言基础——链表的相关操作
1 #include <stdio.h> #include <malloc.h> #include <string.h> #include <math.h&g ...