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 ...
随机推荐
- 廖雪峰Java1-4数组操作-1遍历数组
1.遍历数组 for循环通过下标遍历数组 for each直接遍历数组所有元素 int[] ns1 = {1, 34, 9, 16, 25}; for(int i = 0;i<ns1.lengt ...
- keepalived主备节点都配置vip,vip切换异常案例分析
原文地址:http://blog.51cto.com/13599730/2161622 参考地址:https://blog.csdn.net/qq_14940627/article/details/7 ...
- Js学习(1)
数据类型 简单数据类型 ◆number 数字类型 ◆string 字符串类型 ◆Boolean 布尔类型 ◆true 真 (正确的) ◆false 假(错误的) ◆u ...
- document.createRange剪贴板API
js实现复制到剪贴板 document.createRange() API 选中元素→range→selection是一一对应的,即选区必须连续,不可以有分开的多个区域.另外,被选元素必须在dom树上 ...
- scipy构建稀疏矩阵
from scipy.sparse import csr_matrix import numpy as np indptr = np.array([0, 2, 3, 6]) indices = np. ...
- 02-zip文件打包
package com.day1; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...
- jQuery动态创建DOM节点
var SKU=$("#SKU"); // 通过ID查找到指定表格 var oTable=SKU[0]; // 获取第一个表格 // 获取正文tbody元素 var tBodies ...
- SSH 项目整合
SSH整合:spring + springmvc + hibernate 1.1 生成Maven项目:ar_ssh 1.2 添加jar包:pom.xml 与ssm相比,主要添加了spring与hibe ...
- WordPress版微信小程序2.0版本发布
利用业余时间对WordPress版微信小程序进行的升级,增加了一些功能,程序性能上做了一些优化.经过此次的版本升级,WordPress版微信小程序所需的基本功能已经具备. 开放源码地址:https:/ ...
- samba安装
第一步下载: wget https://download.samba.org/pub/samba/stable/samba-4.6.7.tar.gz 看了下没看到啥有用的直接安装: ./configu ...