参考资料:
leetcode
程序中用了递归所以导致内存消耗比较大,以后应该尽量使用非递归程序。
思路:
过段时间在写,程序还可以再优化利用剪枝
public List<String> findWords(char[][] board, String[] words) {
if(words.length==0){
return new ArrayList<>();
}
TrieNode root = new TrieNode();
root = this.head;
this.board = board;
this.length =board.length;
this.width = board[0].length;
for (String str : words
) {
this.insert(str);
}
Set<String> store = new LinkedHashSet<>();
List<String> mylist = new ArrayList<>();
for (int i = 0; i < board.length;i++ ){
for(int j = 0;j < board[0].length;j++){
int [][] mark = new int[board.length][board[0].length];
Stack stack = new Stack();
this.collect(this.head,store,stack,i,j,mark) ;
}
}
Iterator<String> iterator =store.iterator();
while (iterator.hasNext()){
mylist.add(iterator.next());
}
System.out.println(mylist.size());
return mylist;
}
public static void main(String[] args) {
char chars[][] =new char[][]{{'a','b'},{'c','d'}};
String words[]= new String[]{"ab","cb","ad","bd","ac","ca","da","bc","db","adcb","dabc","abb","acb"};
Solution solution = new Solution();
solution.findWords(chars,words);
System.out.println(solution.findWords(chars,words));
}
public void collect(TrieNode node, Set<String> store, Stack<Character> stack, int i, int j,int[][] mark){
if(i==length||j==width||i<0||j<0){
return;
}
Character c = board[i][j];
System.out.println("当前事"+c);
if(node.get(c)==null || mark[i][j]==1){
return;
}else{
node = node.get(c);
mark[i][j]=1;
stack.add(c);
if(node.isEnd()){
// System.out.println("i:"+i+"j:"+j);
// System.out.println("node:"+c);
// System.out.println("加入"+queue.toString());
StringBuilder word=new StringBuilder("");
for (char ch:stack
) {
word.append(ch);
}
store.add(word.toString());
System.out.println("word is"+word);
stack.pop();
// return;
}
collect(node,store,stack,i+1,j,mark);
collect(node,store,stack,i-1,j,mark);
collect(node,store,stack,i,j-1,mark);
collect(node,store,stack,i,j+1,mark);
}
}
/**
* Initialize your data structure here.
*/
private TrieNode head;
private int length;
private int width;
private char[][] board;
public Solution() {
head = new TrieNode();
}
/**
* Inserts a word into the trie.
*/
public void insert(String word) {
TrieNode root = head;
for (int i = 0; i < word.length(); i++) {
if (!root.containsKey(word.charAt(i))) {
root.put(word.charAt(i), new TrieNode());
}
root = root.get(word.charAt(i));
}
root.setEnd();
}
/**
* Returns if the word is in the trie.
*/
public boolean search(String word) {
TrieNode root = head;
for (int i = 0; i < word.length(); i++) {
if (root.get(word.charAt(i)) == null) {
return false;
} else {
root = root.get(word.charAt(i));
}
}
return root.isEnd();
}
/**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
TrieNode root = head;
for (int i = 0; i < prefix.length(); i++) {
if (root.get(prefix.charAt(i)) == null) {
return false;
} else {
root = root.get(prefix.charAt(i));
}
}
return true;
}
class TrieNode {
// R links to node children
private TrieNode[] links;
private final int R = 26;
private boolean isEnd;
public TrieNode() {
links = new TrieNode[R];
}
public boolean containsKey(char ch) {
return links[ch - 'a'] != null;
}
public TrieNode get(char ch) {
return links[ch - 'a'];
}
public void put(char ch, TrieNode node) {
links[ch - 'a'] = node;
}
public void setEnd() {
isEnd = true;
}
public boolean isEnd() {
return isEnd;
}
}