先写上我的代码:

我总是不知道何时把任务交给下一个递归。以致于,写出的代码很臃肿!

放上别人递归的简洁代码:

bool exist(char** board, int m, int n, char* word) {
if(word == NULL) return true;
if(board == NULL || m*n == ) return false; bool ans= false; bool **used = (bool**)malloc(m*sizeof(bool*)); for(int i = ; i < m; i++)
{
used[i] = (bool*)malloc(n*sizeof(bool));
memset(used[i],false,n*sizeof(bool));
} for(int i = ; i < m; i++)
{
for(int j = ; j < n; j ++)
{
if(exist_from(board,used,i,j,m,n,word,))
{
ans = true;
goto exit;
}
}
}
exit:
for(int i = ; i < m; i++)
{
free(used[i]);
}
free(used);
return ans;
}
bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k) //find kth
{ if(word[k] == ) return true;    //the end of the string if(row >=m ||row < || col >=n || col < ) return false;    //out of range if(used[row][col] || word[k] != board[row][col]) return false;  //dumplicates or not equal used[row][col] = true; if(exist_from(board,used,row-,col,m,n,word,k+) || exist_from(board,used,row+,col,m,n,word,k+)||
exist_from(board,used,row,col-,m,n,word,k+)||exist_from(board,used,row,col+,m,n,word,k+))
return true; used[row][col] = false; return false;
}

非常不递归风格的代码。。

bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k);   //find kth

bool exist(char** board, int m, int n, char* word) {
if(word == NULL) return true;
if(board == NULL || m*n == ) return false; bool **used = (bool**)malloc(m*sizeof(bool*)); for(int i = ; i < m; i++)
{
used[i] = (bool*)malloc(n*sizeof(bool));
memset(used[i],false,n*sizeof(bool));
} for(int i = ; i < m; i++)
{
for(int j = ; j < n; j ++)
{
if(word[] == board[i][j])
{
used[i][j] = true;
if(exist_from(board,used,i,j,m,n,word,)) return true;
used[i][j] = false;
}
}
} return false;
} bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k) //find kth
{
bool ans= false; if(word[k] == ) return true; if(row > && !used[row-][col] && board[row-][col] == word[k] )
{
used[row-][col] = true;
ans = exist_from(board,used,row-,col,m,n,word,k+);
used[row-][col] = false; if(ans == true) return true;
}
if(row < m- && !used[row+][col] && board[row+][col] == word[k] )
{
used[row+][col] = true;
ans = exist_from(board,used,row+,col,m,n,word,k+);
used[row+][col] = false; if(ans == true) return true;
} if(col > && !used[row][col-] && board[row][col-] == word[k] )
{
used[row][col-] = true;
ans = exist_from(board,used,row,col-,m,n,word,k+);
used[row][col-] = false; if(ans == true) return true;
}
if(col < n- && !used[row][col+] && board[row][col+] == word[k] )
{
used[row][col+] = true;
ans = exist_from(board,used,row,col+,m,n,word,k+);
used[row][col+] = false; if(ans == true) return true;
} return false;
}

其实,如果把范围判断放在更深层,会写出更简洁的代码。。

leetcode 题解 word search。递归可以更简洁。的更多相关文章

  1. 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 ...

  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] 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 ...

  4. leetcode 79. Word Search 、212. Word Search II

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

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

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

  7. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  8. 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 ...

  9. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

随机推荐

  1. onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生

    onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生

  2. Java-Runoob-高级教程-实例-方法:04. Java 实例 – 斐波那契数列

    ylbtech-Java-Runoob-高级教程-实例-方法:04. Java 实例 – 斐波那契数列 1.返回顶部 1. Java 实例 - 斐波那契数列  Java 实例 斐波那契数列指的是这样一 ...

  3. (转)区分LTE,EPS,EPC和SAE

    LTE:Long Term Evolution长期演进,是无线接口部分向4G演进的工作项目. 3GPP:The 3Rd Generation Partnership Project,第三代合作伙伴计划 ...

  4. sas spawner

    注册spawner 服务"c:\Program Files\SASHome\SASFoundation\9.4\cntspawn.exe" -install -service 51 ...

  5. SqlServer存储过程输出参数

    if exists(select 1 from sysobjects where name='P_PreOrderInfo') drop Procedure P_PreOrderInfo go Cre ...

  6. Chapter1:Qt概念

    信号和槽1.信号与槽机制的连接方式(1):一个信号可以与另一个信号相连,代码如下: connect(Object1,SIGNAL(signal1),Object2,SIGNAL(signal1)); ...

  7. Linux性能及调优指南1.2之Linux内存架构

    本文为IBM RedBook的Linux Performanceand Tuning Guidelines的1.2节的翻译原文地址:http://www.redbooks.ibm.com/redpap ...

  8. linux:ubuntu安装mysql(一)

    1.下载MySQL安装包(MySQL Community Server) 地址:https://dev.mysql.com/downloads/mysql/ 2.解压 tar -xvf mysql-s ...

  9. VS2015 无法启动 IIS服务器

    打开VS2012解决方案资源管理器 -> 点选 Web 项目选择 -> 属性 -> Web ->创建虚拟目录. 再次运行Web项目,成功.

  10. 关于elment-ui树形控件Tree的使用

    如果简单使用的tree树形控件,按照Element ui Tree 树形控件 官方文档使用即可,基本上能够完成大部分的需求. 但是如果需要对树形结构进行额外操作,仅仅根据官方文档提供的方案的话,可能就 ...