国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 425. Word Squares

ranwu / 1273人閱讀

Problem

Given a set of words (without duplicates), find all word squares you can build from them.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).

For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically.

b a l l
a r e a
l e a d
l a d y

Note:
There are at least 1 and at most 1000 words.
All words will have the exact same length.
Word length is at least 1 and at most 5.
Each word contains only lowercase English alphabet a-z.
Example 1:

Input:
["area","lead","wall","lady","ball"]

Output:
[
  [ "wall",
    "area",
    "lead",
    "lady"
  ],
  [ "ball",
    "area",
    "lead",
    "lady"
  ]
]

Explanation:
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
Example 2:

Input:
["abat","baba","atan","atal"]

Output:
[
  [ "baba",
    "abat",
    "baba",
    "atan"
  ],
  [ "baba",
    "abat",
    "baba",
    "atal"
  ]
]

Explanation:
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).

Reference:
https://leetcode.com/problems...

Solution - Trie+DFS
class Solution {
    public List> wordSquares(String[] words) {
        List> res = new ArrayList<>();
        if (words == null || words.length == 0) return res;
        int len = words[0].length();
        List temp = new ArrayList<>();
        Trie trie = new Trie(words);
        for (String word: words) {
            temp.add(word);
            dfs(trie, len, temp, res);
            temp.remove(temp.size()-1);
        }
        return res;
    }
    
    private void dfs(Trie trie, int len, List temp, List> res) {
        int size = temp.size();
        if (size == len) {
            res.add(new ArrayList<>(temp));
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (String str: temp) {
            sb.append(str.charAt(size));
        }
        String prefix = sb.toString();
        List words = trie.getWords(prefix);
        for (String word: words) {
            temp.add(word);
            dfs(trie, len, temp, res);
            temp.remove(temp.size()-1);
        }
    }
}

class Trie {
    TrieNode root;
    public Trie(String[] strs) {
        root = new TrieNode();
        for (String str: strs) {
            TrieNode cur = root;
            for (char ch: str.toCharArray()) {
                int index = ch-"a";
                if (cur.children[index] == null) cur.children[index] = new TrieNode();
                cur.children[index].words.add(str);
                cur = cur.children[index];
            }
        }
    }
    
    public List getWords(String prefix) {
        List res = new ArrayList<>();
        TrieNode cur = root;
        for (char ch: prefix.toCharArray()) {
            int index = ch-"a";
            if (cur.children[index] == null) return res;
            cur = cur.children[index];
        }
        res.addAll(cur.words);
        return res;
    }
}

class TrieNode {
    TrieNode[] children;
    List words;
    public TrieNode() {
        children = new TrieNode[26];
        words = new ArrayList<>();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/72165.html

相關(guān)文章

  • 425 Word Squares

    摘要:我們要滿足都可以作為開頭的部分。按照代碼的邏輯,走一遍填寫好每一步被更新的樣子。開始結(jié)束同一層從左向右走的時候會不斷增長,直到最后形成以個單詞相應(yīng)位置長度加一,更新重新進(jìn)行下一次的搜索。 題目描述請見leetcode 425 w a l l a r e a l e a d l a d y 在給定的詞典里,找到一些單詞,組成一個square, 第i行和第i列一樣。(0,0) 這個點(diǎn)找到滿...

    luzhuqun 評論0 收藏0
  • Word Squares

    摘要:題目鏈接暴力遍歷,一個一個檢查看符不符合要求。首先這種需要求出所有結(jié)果的題,一般都是用的。因?yàn)轭}目已經(jīng)說了的長度范圍是到,最多考慮五個單詞即可。首先是肯定都需要的,兩種或者。如果題目要求返回所有以特定的開頭的單詞,那么可以用。 Valid Word Square 題目鏈接:https://leetcode.com/problems... 暴力遍歷,一個一個檢查看符不符合要求。 ...

    JerryZou 評論0 收藏0
  • Leetcode PHP題解--D34 977. Squares of a Sorted Array

    摘要:題目鏈接題目分析本題比較簡單。對給定數(shù)組的每一個數(shù)字的平方。并對結(jié)果進(jìn)行排序。思路遍歷每一個元素,相乘自身。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 977. Squares of a Sorted Array 題目鏈接 977. Squares of a Sorted Array 題目分析 本題比較簡單。對給定數(shù)組的每一個數(shù)字的平方。并對結(jié)果進(jìn)行排序。 思路 遍歷每一個元素,...

    Kaede 評論0 收藏0
  • [Leetcode] Perfect Squares 完美平方數(shù)

    摘要:動態(tài)規(guī)劃復(fù)雜度時間空間思路如果一個數(shù)可以表示為一個任意數(shù)加上一個平方數(shù),也就是,那么能組成這個數(shù)最少的平方數(shù)個數(shù),就是能組成最少的平方數(shù)個數(shù)加上因?yàn)橐呀?jīng)是平方數(shù)了。 Perfect Squares Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4...

    Moxmi 評論0 收藏0
  • [LeetCode] 279. Perfect Squares

    Problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exampl...

    mist14 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<