leetcode 题解 word search。递归可以更简洁。
先写上我的代码:
我总是不知道何时把任务交给下一个递归。以致于,写出的代码很臃肿!
放上别人递归的简洁代码:
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。递归可以更简洁。的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- [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 ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- android手机截屏、录屏
1. 手动截屏,通过其他第三方软件发送截图,或者从手机取出截图 2. 使用命令截图,将截图保存到手机,再拉取到电脑 #!/bin/sh #运行 sh screenshot name picName=$ ...
- RPM安装MYSQL5.7
RPM安装MYSQL5.7 1:YUM安装依赖库 yum install perl libaio numactl 2:下载安装需要的RPM包 https://dev.mysql.com/get/Dow ...
- C语言强化——排序
1.完成堆排,对比堆排和qsort在排序1亿数的时间差异 #include<stdio.h> #include<time.h> #include<stdlib.h> ...
- shell命令输出
在shell脚本中的打印输出通常会有echo和printf两种,前者会自动换行. 一.echo Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.您可以使用ech ...
- python类的全面介绍
转载:全面介绍python面向对象的编程——类的基础 转载:类的实例方法.静态方法.类方法的区别
- JavaScript、CSS样式收集
JS集: //给from一个名字然后在JavaScript的地方就可以用form的名字来调用form表单里input元素的value属性可以得到值 var val=form_name.input_na ...
- linux驱动开发—基于Device tree机制的驱动编写
前言Device Tree是一种用来描述硬件的数据结构,类似板级描述语言,起源于OpenFirmware(OF).在目前广泛使用的Linux kernel 2.6.x版本中,对于不同平台.不同硬件,往 ...
- sas基础系列(3)-表格标颜色示例
以下代码可以直接在SAS执行查看效果 ods path reset;ods path show;ods html close;options nodate;ods pdf file="Pro ...
- SSH2 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- visual studio 2017调试时闪退。
解决方案: 在工程上右键--->属性--->配置属性--->连接器--->系统--->子系统(在窗口右边)--->下拉框选择控制台(/SUBSYSTEM:CONSO ...