题目如下:

解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路。一是遍历wordlist;二是对word本身进行分析,找出能组成回文的words,并判断是否存在于wordlist中。显然,第二种思路比较的次数要少很多。怎么找出能组成回文的words呢?只要把后面的字符依次往前复制,并判断是否为回文,直到全部字符复制完成为止,这就能得到所有能与之组成回文的words。以abcd作为前缀为例,首先把d复制到abcd前面得到dabcd,接下来依次是dcabcd,dcbabdc,dcbaabcd。其中dcbabdc和dcbaabcd是回文,再判断dcb 和 dcba 是否存在于wordlist中即可;同理,abcd作为后缀是一样的。

代码如下:

class Solution(object):
def isPalindrome(self,s):
return s == s[::-1] def palindromePairs(self, words):
dic = {}
for i,v in enumerate(words):
dic[v] = i
res = []
for i,v in enumerate(words):
if v == 'ab':
pass
subs = ''
for j in v[::-1]:
subs += j
if subs in dic and dic[subs] != i and self.isPalindrome(subs + v):
res.append([dic[subs],i]) subs = ''
#考虑abcd和dcbd都存在于list中的情况,这里要少判断一位,避免出现重复的组合
for j in v[:-1]:
subs = j + subs
if subs in dic and dic[subs] != i and self.isPalindrome(v + subs):
res.append([i,dic[subs]]) # space,空能与其他本身就回文word组成回文word,这里单独考虑
space = ''
if space in dic and dic[space] != i and self.isPalindrome(v):
res.append([dic[space],i])
res.append([i,dic[space]])
return res

【leetcode】336. Palindrome Pairs的更多相关文章

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

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

  2. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  3. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  5. 【LeetCode】234. Palindrome Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  7. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 题意:每天往n*m的棋盘上放一颗棋子,求多少天能将棋盘的每行每列都至少有 ...

  2. MySQL innodb的组合索引各个列中的长度不能超过767,

    MySQL索引的索引长度问题   MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中,创建组合索引时,创建的索引长度不能超过1000,注意这里索 ...

  3. 不用print调试 xdebug ubuntu phpstorm 远程断点调试

    即使这会写php也遵守zebra大人的指示:不用print调试!!!!----环境ok  ---gan !!! w http://blog.csdn.net/ty_hf/article/details ...

  4. 奖项-MVP:MVP(微软最有价值专家)百科

    ylbtech-奖项-MVP:MVP(微软最有价值专家)百科 微软最有价值专家(MVP) 是指具备一种或多种微软技术专业知识,并且积极参与在线或离线的社群活动,经常与其他专业人士分享知识和专业技能,受 ...

  5. C++ 编写的DLL导出的函数名乱码含义解析

    C++编译时函数名修饰约定规则: __stdcall调用约定:   1.以"?"标识函数名的开始,后跟函数名:     2.函数名后面以"@@YG"标识参数表的 ...

  6. PHP Yii框架中使用smarty模板

    第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事.尝试着把smarty模板加进来了. date_default_timezone_set("PRC") ...

  7. PTA 1121 Damn Single

    题目链接:1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who i ...

  8. JUnit的基本使用

    一些关于单元测试的理念:    单元测试并不能证明你的代码是正确的,只能证明你的代码是没有错误的. Keep bar green and keep your code cool    关于JUnit的 ...

  9. java中高级开发知识准备要点

    转载来源:https://www.cnblogs.com/JavaArchitect/p/10011253.html 在上周,我密集面试了若干位Java后端的候选人,工作经验在3到5年间.我的标准其实 ...

  10. python自定义迭代器对象以及可迭代对象

    # coding=utf8 from collections import Iterator from collections import Iterable #迭代器对象 class OwnIter ...