成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 336. Palindrome Pairs

lentoo / 1875人閱讀

Problem

Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.

Example 1:

Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]
Example 2:

Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]]
Explanation: The palindromes are ["battab","tabbat"]

Solution
class Solution {
    public List> palindromePairs(String[] words) {
        List> res = new ArrayList<>();
        if (words == null || words.length == 0) return res;
        Map map = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            map.put(words[i], i);
        }
        
        if (map.containsKey("")) {
            int blankIndex = map.get("");
            for (int i = 0; i < words.length; i++) {
                if (isPalindrome(words[i]) && i != blankIndex) {
                    res.add(Arrays.asList(blankIndex, i));
                    res.add(Arrays.asList(i, blankIndex));
                }
            }
        }
        
        for (int i = 0; i < words.length; i++) {
            String reversed = new StringBuilder(words[i]).reverse().toString();
            if (map.containsKey(reversed)) {
                int index = map.get(reversed);
                if (index != i) {
                    res.add(Arrays.asList(i, index));
                }
            }
        }
        
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            for (int j = 1; j < word.length(); j++) {
                if (isPalindrome(word.substring(0, j))) {
                    String reversed = new StringBuilder(word.substring(j)).reverse().toString();
                    if (map.containsKey(reversed)) {
                        int index = map.get(reversed);
                        if (index != i) {
                            res.add(Arrays.asList(index, i));
                        }
                    }
                }
                if (isPalindrome(word.substring(j))) {
                    String reversed = new StringBuilder(word.substring(0, j)).reverse().toString();
                    if (map.containsKey(reversed)) {
                        int index = map.get(reversed);
                        if (index != i) {
                            res.add(Arrays.asList(i, index));
                        }
                    }
                }
            }
        }
        
        return res;
    }
    
    private boolean isPalindrome(String word) {
        if (word == null || word.length() <= 1) return true;
        int i = 0, j = word.length()-1;
        while (i < j) {
            if (word.charAt(i) != word.charAt(j)) return false;
            i++;
            j--;
        }
        return true;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72276.html

相關(guān)文章

  • LeetCode 336. Palindrome Pairs

    摘要:描述給定一組唯一的單詞,找出所有不同的索引對(duì),使得列表中的兩個(gè)單詞,,可拼接成回文串。遍歷每一個(gè)單詞,對(duì)每一個(gè)單詞進(jìn)行切片,組成和。 Description Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenatio...

    TigerChain 評(píng)論0 收藏0
  • 336. Palindrome Pairs

    摘要:容易出的兩個(gè)地方以為例。這兩個(gè)互為的在尾部加上也就是在頭部加上所以后部分不能為空,否則就和頭部為空的情況重復(fù)了??臻g復(fù)雜度因?yàn)橛昧祟~外的來(lái)儲(chǔ)存,需要空間。時(shí)間復(fù)雜度每個(gè)分為兩個(gè)部分,調(diào)用前后兩部分總長(zhǎng)度為所以每次調(diào)用為一共次。 Given a list of unique words, find all pairs of distinct indices (i, j) in the g...

    Guakin_Huang 評(píng)論0 收藏0
  • [LeetCode] Palindrome Pairs

    摘要:和的區(qū)別是,所以對(duì)于,效率更高不允許作為鍵值,而允許一個(gè)鍵和無(wú)限個(gè)值有一個(gè),叫,便于查詢可預(yù)測(cè)的迭代順序。這道題依然選擇的話 Problem Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the...

    DangoSky 評(píng)論0 收藏0
  • Palindrome Pairs & Shortest Palindrome

    摘要:部分是回文的,在里面找是否有的。這里的范圍是,最小是,因?yàn)橐WC是兩個(gè)單詞,最大是,這時(shí)候要找出另一個(gè)和他相反的串。判斷為回文,可以直接暴力,每個(gè)都判斷一次。兩個(gè)方向都找一遍就可能出現(xiàn)重復(fù)的情況,注意避免重復(fù)。例如,結(jié)果應(yīng)該是。 Palindrome Pairs 鏈接:https://leetcode.com/problems... 這道題沒(méi)想出來(lái)思路,參考了這個(gè)博客的內(nèi)容:http:...

    CNZPH 評(píng)論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開(kāi)始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒(méi)有按順序?qū)懍F(xiàn)在翻起來(lái)覺(jué)得蠻亂的。可能大家看著也非常不方便。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開(kāi)始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒(méi)有按順序?qū)憽F(xiàn)在翻起來(lái)覺(jué)得蠻亂的??赡艽蠹铱粗卜浅2环奖恪K栽谶@里做個(gè)索引嘻嘻。 順序整理 1~50 1...

    leo108 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<