Leetcode 79. 单词搜索(DAY 94) ----Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
public:
    bool backtracking(const vector<vector<char>>& board,vector<vector<bool>>& visit,const string& word,int pos,int x,int y)
    {
        if(visit[x][y] || board[x][y] != word[pos]) return false;
        if(pos == word.size()-1)    return true;
        int xsize = board.size(),ysize = board[0].size();
        visit[x][y] = true;
        if(x && board[x-1][y] == word[pos+1])   
            if(backtracking(board,visit,word,pos+1,x-1,y))  return true;
        if(y && board[x][y-1] == word[pos+1])   
            if(backtracking(board,visit,word,pos+1,x,y-1))  return true;
        if(x+1<=xsize-1)
            if(backtracking(board,visit,word,pos+1,x+1,y))  return true;
        if(y+1<=ysize-1)
            if(backtracking(board,visit,word,pos+1,x,y+1))  return true;
        visit[x][y] = false;   
        return false;
    }

    bool exist(vector<vector<char>>& board, string word) {
        int xsize = board.size(),ysize = board[0].size();
        vector<vector<bool>> visit(xsize,vector<bool>(ysize,false));
        for(int i=0;i<xsize;++i)
        {
            for(int j=0;j<ysize;++j)
            {
                if(board[i][j] == word[0])
                    if(backtracking(board,visit,word,0,i,j))    return true;
            }
        }
        return false;
    }
};

代码实现(二刷自解 DAY 159 C++)


class Solution {
public:
    bool search(vector<vector<char>>& board,vector<vector<bool>>& visit,const string& word,int pos,int x,int y)
    {
        if(x<0 || x>= board.size() || y<0 || y>=board[0].size() || visit[x][y]
            || board[x][y] != word[pos]) return false;
        visit[x][y] = true;
        if(pos == word.size()-1)    return true;
        int delnum = 1;
        for(int i=0;i<4;++i)
        {
            if(i<=1)
            {
                if(search(board,visit,word,pos+1,x+delnum,y))
                    return true;
            }
            else
            {
                if(search(board,visit,word,pos+1,x,y+delnum))
                    return true;
            }
            delnum *= -1;
        }
        visit[x][y] = false;
        return false;   
    }

    bool exist(vector<vector<char>>& board, string word) {
        vector<vector<bool>> visit(board.size(),vector<bool>(board[0].size(),false));
        for(int i=0;i<board.size();++i)
        {
            for(int j=0;j<board[0].size();++j)
            {
                if(search(board,visit,word,0,i,j))
                    return true;
            }
        }
        return false;
    }
};

代码实现(三刷自解 DAY 14 golang)


func depthfirstsearch(board [][]byte, visit [][]bool, word string, x, y, pos int) bool {
  if pos == len(word) {
    return true
  }
  if x < 0 || x >= len(visit) || y < 0 || y >= len(visit[0]) || visit[x][y] || 
     byte(word[pos]) != board[x][y] {
    return false
  }

  visit[x][y] = true
  ret := depthfirstsearch(board, visit, word, x + 1, y, pos + 1)
  if !ret {
    ret = depthfirstsearch(board, visit, word, x - 1, y, pos + 1)
  }
  if !ret {
    ret = depthfirstsearch(board, visit, word, x, y + 1, pos + 1)
  }
  if !ret {
    ret = depthfirstsearch(board, visit, word, x, y - 1, pos + 1)
  }
  visit[x][y] = false
  return ret
}

func exist(board [][]byte, word string) bool {
  visit := make([][]bool, len(board))
  for index, _ := range visit {
    visit[index] = make([]bool, len(board[0]))
  }

  for i := 0; i < len(board); i++ {
    for j := 0; j < len(board[0]); j++ {
      if depthfirstsearch(board, visit, word, i, j, 0) {
        return true
      }
    }
  }

  return false
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值