题目如下:

解题思路:对于任意一个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. Photon学习(一)——Photon Networking Free网络组件学习

    一般前端untiy程序员都很想自己学会后端网络编程,这样一个人就可以把前后端都做了,做网络游戏可比单机游戏好玩多了,笔者我对喜欢的就是mmo多人对战游戏,一起组队打副本,一起体验多人对战的乐趣.从业以 ...

  2. 笨办法学Python(learn python the hard way)--练习程序39-40

    下面是练习39-练习40,基于python3 #ex39.py 1 ten_things = "Apples Oranges Crows Telephone Light Sugar" ...

  3. PHPStorm + Xdebug 调试PHP代码 有大用

    星期四, 12/26/2013 - 19:54 - shipingzhong PHPStorm + Xdebug 调试PHP代码 http://e.v-get.com/2013-11-20 16:55 ...

  4. laravel5.6 操作数据 Eloquent ORM

    建立Users模型 <?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class ...

  5. HDU 1003 解题报告

    问题描述:求最大连续字串 分析:一道简单的DP,状态转移方程是d[i] = ( d[i-1]+a[i] > a[i] ) ? d[i-1]+a[i] : a[i] d[i]表示以第i个数字结尾的 ...

  6. machine learning 之 Recommender Systems

    整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...

  7. 【SQL SERVER】常见问题

    [账户] 偷懒默认安装了全部默认功能到虚拟服务器上,所以并没有设置sa账户的过程,自然只能从windows身份验证进入数据库.于是还得自己来为sa账户添加登陆权限. 步骤如下: 1. windows身 ...

  8. 让Debian以root登录

    Debian默认不允许root登录,所以修改之. (1)让Debian以root登录 修改gdm3的登录pam文件 #vi /etc/pam.d/gdm3 将auth required pam_suc ...

  9. mybatis多对一

    产品和分类的多对一关系 多个产品属于一个分类 public class Product { private int id; private String name; private float pri ...

  10. 记一次 Json 对象转换为 Java 对象的问题

    1.描述 最近在使用 Jackson 将 Json 串转换回 Java 对象的时候遇到了 ClassCastException 错误,特此记述. 2.问题复现 问题出现的节点在于属性节点的 JavaT ...