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:
Given words = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]

Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]

这道题的思路就是把每个word和index存到diction里面, 然后针对每个word, for loop, 拆分为两部分, 如果前面部分是palindrome, 那么把后面部分的单词reverse, 如果reverse后的单词在diction里面, ans.append(d[back], index); 同理, 如果后面部分是palindrome, 把前面部分的单词reverse, 如果reverse后的单词在diction里面, ans.append(index, d[back]). 此时需要注意的是在后面check的时候就不要考虑整个单词reverse的情况, 因为前面单词reverse的时候已经考虑到了. 如果不明白的话就用set去去重, 最保险的做法.

参考Solution.

class Solution(object):
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
def checkpal(w):
return w == w[::-1] # 反正O(n), 所以用最粗暴的方式 d, ans = {w:index for index, w in enumerate(words)},[]
for word, index in d.items():
l = len(word)
for i in range(l+1): # l+1 因为pref要到[:l]
pref = word[:i]
suf = word[i:]
if checkpal(pref):
back = suf[::-1]
if back != word and back in d:
ans.append([d[back], index]) if i != l and checkpal(suf): # delete the duplicates
back = pref[::-1]
if back != word and back in d:
ans.append([index, d[back]])
return ans

[Leetcode] 336. Palindrome Pairs_Hard的更多相关文章

  1. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  2. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  3. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. Palindrome Pairs -- LeetCode 336

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  7. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  8. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  9. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

随机推荐

  1. Android Studio 3.1.2 版本包下载

    Android Studio 3.1.2 bug 修复版已发布,本次更新修复了一些错误,并改进了某些场景下 lint 审查的速度.详细的修复内容请查看 Android Studio 3.1.2 的发布 ...

  2. [转]13 Hours: The Secret Soldiers of Benghazi

    转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...

  3. 升级后重启造成fsck.ext3: Unable to resolve UUID

    这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...

  4. 部署OpenStack问题汇总(六)-- OpenStack入门需要知道的概念

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 一.网络问题-network 更多网络原理机制可以参考<OpenStack ...

  5. JavaScript arguments对象详解

    1. 什么是 arguments MDN 上解释: arguments 是一个类数组对象.代表传给一个function的参数列表. 我们先用一个例子直观了解下 JavaScript 中的 argume ...

  6. 4606: [Apio2008]DNA

    4606: [Apio2008]DNA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 63  Solved: 36[Submit][Status][D ...

  7. 如何使用Gradle的maven-publish将jar包或者war包上传到nexus仓库

    首先,在build.gradle里边声明依赖maven-publish插件: apply plugin: 'maven-publish' 然后,配置项目的信息和和nexus的信息: publishin ...

  8. Css中!important的用法

    !important为开发者提供了一个增加样式权重的方法.应当注意的是!important是对整条样式的声明,包括这个样式的属性和属性值 <!DOCTYPE HTML> <html& ...

  9. 在jmeter测试中模拟不同的带宽环境

    怎么去测试在手机app中和在web的不同的连接速度对服务器的影响呢? 大部分情况下,手机终端用户通过移动网络访问网站.所以在不同的网络连接数据下,我们的网站或程序可以同时处理多少链接?今天,这篇文件就 ...

  10. JAVA unicode转换成中文

    /** * * unicode 转换成 中文 * @param theString * @return */ public static String decodeUnicode(String the ...