题目如下:

解题思路:对于任意一个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. 基于ES6的tinyJquery

    原文地址:Bougie的博客 jQuery作为曾经Web前端的必备利器,随着MVVM框架的兴起,如今已稍显没落.但它操作DOM的便利性无出其右.我用ES6写了一个基于class简化版的jQuery,包 ...

  2. drawArc

    1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...

  3. (转)cat > file << EOF 的用法

    转:https://www.cnblogs.com/chenjingchao/p/6259572.html cat> 文件名<<eof 用来创建文件在这之后输入任何东西 都是在 文件 ...

  4. leetcode 217. 存在重复元素 (python)

    给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1]输出: true示 ...

  5. 【Visual Studio】 使用EF、 Linq2Sql快速创建数据交互层(一)

    项目伊始,创建数据库交互层代码是底层框架的首要任务.常用的做法包括手动编码.Hibernate或者动软之类的代码生成器,而多数人忽略了.Net环境下VS提供的两套非常好用的数据层工具. EF和Linq ...

  6. mybatis有结果返回null

    解决:application.yml 中mybatis此项(解决驼峰及数据库字段有下划线问题) map-underscore-to-camel-case: true 问题: mybatis debug ...

  7. Nginx 模块 - ngx_core_module

    原文地址 示例配置 指令 accept_mutex accept_mutex_delay daemon debug_connection debug_points env error_log even ...

  8. Amber

    训练做的题里有板子单独拉出来. 欧拉筛 ],prim[N+]; int cnt; void Eular() { vis[]=vis[]=; ;i<N;i++) if(!vis[i]) { pri ...

  9. 网络流强化-HDU2732

    第一次遇到加了“多余”的边会导致WA的——在我看来是很多余,见代码191行 之后会思考为什么,想出来再更. 问题弄明白了,如果你在连接边连了一条到没有柱子的点的边,这个没有柱子的点是不可能连到终点的, ...

  10. 爬虫(五)—— 解析库(二)beautiful soup解析库

    目录 解析库--beautiful soup 一.BeautifulSoup简介 二.安装模块 三.Beautiful Soup的基本使用 四.Beautiful Soup查找元素 1.查找文本.属性 ...