原题地址

依次枚举起始点,DFS+回溯

代码:

 bool dfs(vector<vector<char> > &board, int r, int c, string word) {
int m = board.size();
int n = board[].size();
int dir[][] = {{-, }, {, }, {, -}, {, }}; if (word.empty())
return true; if (r >= && r < m && c >= && c < n && board[r][c] == word[]) {
for (int i = ; i < ; i++) {
char tmp = board[r][c];
board[r][c] = ;
if (dfs(board, r + dir[i][], c + dir[i][], word.substr()))
return true;
board[r][c] = tmp;
}
} return false;
} bool exist(vector<vector<char> > &board, string word) {
if (board.empty() || board[].empty()) return false; int m = board.size();
int n = board[].size(); for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (dfs(board, i, j, word))
return true; return false;
}

Leetcode#79 Word Search的更多相关文章

  1. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. [LeetCode] 79. 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. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. LeetCode 79. Word Search(单词搜索)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  6. LeetCode 79. Word Search单词搜索 (C++)

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

  7. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

随机推荐

  1. GridView自带分页 1总页数 首页 下一页 上一页 尾页 X 页 go 实现方法 .

    在前台GRIDVIEW中添加如下代码 <PagerTemplate> <table> <tr> <td style="text-align: rig ...

  2. swift-自定义无限轮播图

    一  前言 1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift. 2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正. 3.我决定一步步 ...

  3. Java关键字及其作用

    Java关键字及其作用 一. 关键字总览 访问控制 private protected public             类,方法和变量修饰符 abstract class extends fin ...

  4. (转)RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)

    在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇文章中,我们将会 ...

  5. 分享8款最新HTML5/CSS3功能插件及源码下载

    1.HTML5/CSS3鬼脸表情下拉菜单 超级可爱 这款HTML5/CSS3鬼脸表情下拉菜单真的很特别,虽然菜单的实现并没有利用复杂的HTML5/CSS3技术,但是创意的确不错. 在线演示 源码下载 ...

  6. 关于CORS

    前几天碰到CORS问题,只要在“Access-Control-Allow-Origin”响应头中添加对应域名即可. 今天做一个上传文件的demo,利用XMLHttpRequest向服务器发送post请 ...

  7. 《NFS文件共享服务的搭建》RHEL

    首先要清楚一点:NFS服务的共享是建立在linux和linux之间的. 配置任何服务之前我们要做的2件事: iptables  -F setenforce 0    NFS服务内核防火墙影响不大,主要 ...

  8. 《服务器的追踪与审计》RHEL6

    在linux系统/etc目录下有两个文件: 服务器的追踪: 当其他人访问我的主机时,通过日志监控到那台主机什么时间通过什么方式登陆,做什么?

  9. c# WinForm 编程总结

    1.清空DataGridView /// <summary> /// 清空DataGridView /// </summary> /// <param name=&quo ...

  10. 初测WIN10

    WIN10已经发布,通过百度直通车把WIN7升级成了WIN10,改变较大,不太习惯,用着不是很顺手. 吐槽几个问题 1.微软的Visual Studio 2015 Community版本,宣布是免费的 ...