leetcode——212. 单词搜索 II

参考资料:
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;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

沉默终止

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

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

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

打赏作者

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

抵扣说明:

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

余额充值