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 ...
随机推荐
- spring4.3新注解之:@RequestMapping变种(@GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PatchMapping)
Spring 4.3 中引进了下面的注解 @RequestMapping 在方法层级的变种,来帮助简化常用 HTTP 方法的映射,并更好地表达被注解的方法的语义.比如,@GetMapping可以读作 ...
- Java的大内存分页支持
原文:http://kilik.iteye.com/blog/677253 最近在研究java的性能调优,顺手写了一个小程序来测试性能问题.这个程序用来进行矩阵乘法运算,如下: for (int i ...
- [UE4]碰撞机制
应用于两种情况: 一.射线追踪,LineTrace 1.射线来自某个Trace Channel 2.Trace Channel 默认有两个:Visibility(不是可见的意思.只是Channel名称 ...
- 博客搬入CNBLOG
由于无法改变的事实,原来在163blog中的博文永久停止更新.但博文内容仍然保留,在此也谢谢网易不杀之恩.毕竟那都是博主们一个字一个字的敲出来的心血.以后新的内容会在CNBLOG中进行更新.立贴为证. ...
- CRM 2016 执行IFrame 子页面中函数
CRM代码: var iframe = Xrm.Page.getControl("IFRAME_xxx").getObject(); iframe.contentWindow.Re ...
- C语言:结构体,共用体
结构体: 一个变量,存储不同类型的数据项共用体:一个变量,存储不同类型的数据项,相同的内存位置,存储不同的数据类型 #include <stdio.h> #include <stri ...
- PHP unset()函数销毁变量
<?php // 销毁单个变量 unset ($var); // 销毁单个数组元素 unset ($arr['a']); // 销毁多个变量 unset ($var1, $var2, $var3 ...
- Android Studio连接不到MuMu模拟器;
网易推出的mumu模拟器还挺好用的,主要是流畅占内存小: 但是安装mumu模拟器后,as连接不到mumu模拟器: 好了,教程来了:两步走: 先把模拟器运行起来! 第一步:打开Terminal,输入: ...
- Linux 文件,目录,压缩,解压缩操作
2018/11/20 1.find -name 'pom.xml' | xargs perl -pi -e 's|oldString|newString|g' (批量替换) ( 命令行中使用Perl ...
- NodeJs安装以及注意事项
1.测试NodeJs是否安装成功 node --version npm -v 配置node的可执行文件路径到环境变量path 2.安装相关环境 npm install express -g npm i ...