先写上我的代码:

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

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

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. 面向对象的轮播js

    1.自执行函数的前后要加分号 案例: ;(function(){})(); 2.面向对象的最大优势节省了许多内存 正式开写面向对象的轮播: <!DOCTYPE html> <html ...

  2. 开启BBR加速

    在linux里用 wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh chmod +x b ...

  3. 简单的一个MySQL类的实现:

    '''定义MySQL类:1.对象有id.host.port三个属性2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一3.提供两种实例化方式,方式一:用户传入host和por ...

  4. numpy.ravel() vs numpy.flatten()

    首先声明两者所要实现的功能是一致的(将多维数组降为一维),两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响(reflec ...

  5. Jquery ajax回调函数不执行

    ajax如下: $.post( "${pageContext.request.contextPath}/deptHead_assign.action", {"studen ...

  6. 阿里云ECS搭建FTP服务器

    一.开始前先开通21端口权限; 二.添加IIS角色; 三.添加ftp用户; 四.步骤如下: 五.用添加在用户登录ftp;

  7. SqlServer存储过程输出参数

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

  8. DB通用类:SQL Server 通用类库

    SQLServer通用类A using System; using System.Data; using System.Data.SqlClient; using System.Collections ...

  9. Android--普通注册页面实现(无功能)

    reg.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  10. Windows Server 2016 路由和远程访问

    本次实验是将Windows Server 2016 配置成一个路由器,为此网络上的客户端和服务器启用多重协议LAN到LAN,LAN到WAN,虚拟专用网络和网络地址转换路由服务.使用路由和远程访问需配置 ...