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 ...
随机推荐
- [UE4]声音系统概述
一.只能使用wav格式的声音 二.wav声音可以直接播放到打开的UE4编辑器内打开的Content文件夹.也可以直接导入 三.在Content中的文件夹的声音资源可以直接拖放到场景中,会以3D场景声音 ...
- mysql空间类型使用笔记
创建表,填充测试数据 create table geom1(id int not null auto_increment primary key,geo geometry); )); )); sele ...
- socketsever
socketsever 一个集成了TCP.UDP多线程多进程高并发的socket框架,可以用来快速搭建socket应用,并且拥有较好的并发性能. import socketserver class M ...
- 「一本通 6.4 例 4」曹冲养猪(CRT)
复习一下 扩展中国剩余定理 首先考虑两个同余方程 \[ x \equiv a_1\; mod\; m_1\\ x \equiv a_2\; mod\; m_2 \] 化成另一个形式 \[ x = n_ ...
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- mysql给查询的结果添加序号
1.法一: select (@i:=@i+1) i,a.url from base_api_resources a ,(select @i:=0) t2 order by a.id de ...
- Python——列表深浅拷贝
一.深浅拷贝 如果希望将列表复制一份,通过列表的内置方法copy就可以实现: s = [[1,2],3,4] s1 = s.copy() print(s) print(s1) 拷贝出的列表s1与原列表 ...
- Ruby学习笔记5: 动态web app的建立 (2)
上一节里,我们搭建了一个数据库的结构,并用index验证了request-response cycle,如下图: 1. Add show method into Controller 这一节,我们要继 ...
- geiUItabBarItem设置图片颜色和title颜色
设置图片颜色 tabBarVCtrl.tabBar.selectedImageTintColor = [UIColor greenColor];//设置tabBarItem选中时的字图颜色,iOS 8 ...
- 17.scrapy-splash安装-2
scrapy-splash是一个scrapy中支持的javascript渲染的工具. scrapy-splash安装分为两部分.一个是splash服务的安装,具体是通过docker,安装之后,会启动一 ...