Leetcode之Word Search II

题目:

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

 

Example:

Input: 
board = [
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]
words = ["oath","pea","eat","rain"]

Output: ["eat","oath"]

 

Note:

  1. All inputs are consist of lowercase letters a-z.
  2. The values of words are distinct.

代码:

方法一——超时普通解法:

class Solution {
public:
   bool helper(string s, int i, int j, vector<vector<char>>&board) {
	if (s == "")return true;
	if (i >= board.size() || i < 0 || j < 0 || j >= board[i].size())return false;
	if (s[0] != board[i][j])return false;
	board[i][j] = '-';
	bool res= helper(s.substr(1), i + 1, j, board) || helper(s.substr(1), i, j + 1, board) || helper(s.substr(1), i - 1, j,board) || helper(s.substr(1), i, j - 1, board);
	board[i][j] = s[0];
	return res;
}
bool isfind(vector<vector<char>>& board, string s) {
	for (int i = 0; i < board.size(); i++) {
		for (int j = 0; j < board[i].size(); j++) {
			if (helper(s, i, j, board))return true;
		}
	}
	return false;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
	vector<string> res;
	for (int i = 0; i < words.size(); i++) {
		if (isfind(board, words[i])) {
			res.push_back(words[i]);
		}
	}
	return res;
}
};

方法二——使用了字典树:

class Trie {
public:
	char c;
	vector<Trie*> v;
	bool is_word = false;

	Trie() {
		for (int i = 0; i < 26; i++) {
			v.push_back(NULL);
		}
		c = '.';
	}

	void insert(string s) {
		int len = s.length();
		Trie* root = this;
		for (int i = 0; i < len; i++) {
			if (root->v[s[i] - 'a'] == NULL) {
				root->v[s[i] - 'a'] = new Trie();
				root->v[s[i] - 'a']->c = s[i];
			}
			root = root->v[s[i] - 'a'];
		}
		root->is_word = true;
	}

	bool search(string s) {
		int len = s.length();
		Trie* root = this;

		for (int i = 0; i < len; i++) {
			if (root->v[s[i] - 'a'] == NULL)return false;
			root = root->v[s[i] - 'a'];
		}

		if (!root->is_word)return false;
		return true;
	}
	bool prefix(string s) {
		int len = s.length();
		Trie* root = this;
		for (int i = 0; i < len; i++) {
			if (root->v[s[i] - 'a'] == NULL)return false;
			root = root->v[s[i] - 'a'];
		}
		return true;
	}
};
class Solution {
public:
	vector<string> res = {};
	void helper(vector<vector<char>>& board, int i, int j, Trie* t, string s) {
		if (!t)return;
		if (board[i][j] != t->c)return;
		s.push_back(t->c);
		if (t->is_word&&(find(res.begin(),res.end(),s)==res.end())) {
            
			res.push_back(s);

		}
		board[i][j] = '.';
		if (i > 0 && board[i - 1][j] != '.') {
			helper(board, i - 1, j, t->v[board[i - 1][j] - 'a'], s);
		}

		if (i < board.size() - 1 && board[i + 1][j] != '.') {
			helper(board, i + 1, j, t->v[board[i + 1][j] - 'a'], s);
		}

		if (j > 0 && board[i][j - 1] != '.') {
			helper(board, i, j - 1, t->v[board[i][j - 1] - 'a'], s);
		}

		if (j < board[i].size() - 1 && board[i][j + 1] != '.') {
			helper(board, i, j + 1, t->v[board[i][j + 1] - 'a'], s);
		}

		board[i][j] = t->c;
	}

	vector<string> findWord(vector<vector<char>>& board, Trie* tr, vector<string>& words) {

		int len = words.size();
		for (int i = 0; i < len; i++) {
			tr->insert(words[i]);

		}
		for (int i = 0; i < board.size(); i++) {
			for (int j = 0; j < board[i].size(); j++) {
				helper(board, i, j, tr->v[board[i][j] - 'a'], "");
			}
		}
		return res;
	}
     vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        Trie* tr = new Trie();
	    return  findWord(board, tr, words);
    }
};

想法:

踩过的坑——

(1),没用字典树

(2), 遇到isword的词过早return,没想过它下面也可能有isword的情况

(3),没考虑vector中的重复情况

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值