leetcode 题解 word search。递归可以更简洁。

先写上我的代码:

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

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

bool exist(char** board, int m, int n, char* word) {
    if(word == NULL)    return true;
    if(board == NULL || m*n == 0)   return false;
    
    bool ans= false;
    
    bool **used = (bool**)malloc(m*sizeof(bool*));
    
    for(int i = 0; i < m; i++)
    {
        used[i] = (bool*)malloc(n*sizeof(bool));
        memset(used[i],false,n*sizeof(bool));
    }
    
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(exist_from(board,used,i,j,m,n,word,0))   
            {
                ans = true;
                goto exit;
            }
        }
    }
exit: 
    for(int i = 0; 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] == 0)    return true;    //the end of the string
    
    if(row >=m ||row <0 || col >=n || col < 0)  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-1,col,m,n,word,k+1) || exist_from(board,used,row+1,col,m,n,word,k+1)||
       exist_from(board,used,row,col-1,m,n,word,k+1)||exist_from(board,used,row,col+1,m,n,word,k+1))
        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 == 0)   return false;
    
    bool **used = (bool**)malloc(m*sizeof(bool*));
    
    for(int i = 0; i < m; i++)
    {
        used[i] = (bool*)malloc(n*sizeof(bool));
        memset(used[i],false,n*sizeof(bool));
    }
    
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(word[0] == board[i][j])
            {
                used[i][j] = true;
                if(exist_from(board,used,i,j,m,n,word,1))   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] == 0)    return true;
    
    if(row > 0 && !used[row-1][col] && board[row-1][col] == word[k] )
    {
        used[row-1][col] = true;
        ans = exist_from(board,used,row-1,col,m,n,word,k+1);
        used[row-1][col] = false;
        
        if(ans == true) return true;
    }
    if(row < m-1 && !used[row+1][col] && board[row+1][col] == word[k] )
    {
        used[row+1][col] = true;
        ans = exist_from(board,used,row+1,col,m,n,word,k+1);
        used[row+1][col] = false;
        
        if(ans == true) return true;
    }
    
    if(col > 0 && !used[row][col-1] && board[row][col-1] == word[k] )
    {
        used[row][col-1] = true;
        ans = exist_from(board,used,row,col-1,m,n,word,k+1);
        used[row][col-1] = false;
        
        if(ans == true) return true;
    }
    if(col < n-1 && !used[row][col+1] && board[row][col+1] == word[k] )
    {
        used[row][col+1] = true;
        ans = exist_from(board,used,row,col+1,m,n,word,k+1);
        used[row][col+1] = false;
        
        if(ans == true) return true;
    }
    
    return false;
}

 

其实,如果把范围判断放在更深层,会写出更简洁的代码。。

转载于:https://www.cnblogs.com/buddho/p/8037048.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值