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.

For example,
Given words = ["oath","pea","eat","rain"] and board =

[
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]
Return ["eat","oath"].

Note:
You may assume that all inputs are consist of lowercase letters a-z

本题用到了一个trie树的数据结构,有关trie数的介绍可以看:https://en.wikipedia.org/wiki/Trie

下面是一个java trie树的实现,每个节点有26个孩子,没错,是很占用空间,是一种空间换时间的数据结构,在trie树中,第一个节点也就是根节点没有字符,搜索和查询都是从根节点的孩子开始的。

    public class Trie {
    	TrieNode root;	//这是一个根节点,根节点中没有字符
    	
    	Trie(){	//构造函数
    		root = new TrieNode();
    	}
    	
    	//查找trie树中是否存在某个单词
    	public boolean search(String word){
    		TrieNode temp = root;
    		
    		for(char c:word.toCharArray()){
    			temp = temp.children[c-'a'];
    			if(temp==null){
    				return false;
    			}
    		}
    		if(temp.word.equals(word)){	//虽然前面单词都找到了,也可能不存在这个单词
    			return true;
    		}else{
    			return false;
    		}
    	}
    	
    	public boolean startWith(String word){
    		TrieNode temp = root;
    		for(char c:word.toCharArray()){
    			if(temp.children[c-'a']==null){
    				return false;
    			}
    			temp = temp.children[c-'a'];
    		}
    		
    		return true;
    	}
    	
    	public void insert(String word){
    		TrieNode temp = root;
    		
    		for(char c:word.toCharArray()){
    			if(temp.children[c-'a'] == null){
    				temp.children[c-'a'] = new TrieNode(c);
    			}
    			temp = temp.children[c-'a'];
    		}
    		temp.word = word;
    	}
    	
    	class TrieNode {
    		TrieNode[] children; //每一个节点有26个孩子,每个孩子安装字典顺序进行排列,'a'排在第一个,'b'排在第二个,...,'z'排在最后一个
    		String word = "";	 //到这个节点为止表示的单词,如果没有则为""
    		char c;				 //TrieNode中的字符
    		
    		TrieNode(){
    			children = new TrieNode[26];
    		}
    		
    		TrieNode(char c){
    			this.c = c;
    			children = new TrieNode[26];
    		}
    	}
    }

下面是题目的代码,利用深度遍历进行搜索:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Solution {

	public HashSet<String> res = new HashSet<String>();
	
	//给定一个字符矩阵和一组单词,在矩阵中找单词,如果找到就返回
    public List<String> findWords(char[][] board, String[] words) {
    	
    	Trie trie = new Trie();
    	for(int i=0;i<words.length;i++){
    		trie.insert(words[i]);
    	}
    	int row = board.length;
    	int col = board[0].length;
    	
    	boolean[][] visited = new boolean[row][col];
    	
    	for(int i=0;i<row;i++){
    		for(int j=0;j<col;j++){
    			if(trie.startWith(board[i][j]+"")){
    				dfs(board,visited,"",i,j,trie);
    			}
    		}
    	}
    	return new ArrayList<String>(res);
    }

    
    public void dfs(char[][] board, boolean[][] visited, String str, int i, int j, Trie trie) {
    	if(i>=board.length || i<0 ||j>=board[0].length || j<0 || visited[i][j]){
    		return;
    	}
    	String newWord = str+board[i][j];
    	if(!trie.startWith(newWord)){
    		return;
    	}
    	
    	visited[i][j] = true;
    	if(trie.search(newWord)){
    		res.add(newWord);
    	}
    	dfs(board,visited,newWord,i-1,j,trie);
    	dfs(board,visited,newWord,i+1,j,trie);
    	dfs(board,visited,newWord,i,j-1,trie);
    	dfs(board,visited,newWord,i,j+1,trie);
    	visited[i][j] = false;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值