【leetcode】336. Palindrome Pairs
题目如下:

解题思路:对于任意一个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的更多相关文章
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【LeetCode】1065. Index Pairs of a String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- [CSP-S模拟测试]:数字(数学+高精度)
题目描述 很简单,给出正整数$n$,求出$n!$在十进制表示下的从最末非零位开始的总共$k$位. 输入格式 第一行一个正整数$T$,表示有$T$组数据接下来$T$行,每行两个正整数$n$和$k$. 输 ...
- sql语句insert into where 错误解析
sql语句中,insert into 代表得是插入一条新得数据,全新得数据,所以你这样得写法是错误得,比如: "insert into klkl_Service_shop(name_real ...
- Step3 - How to: Host and Run a Basic Windows Communication Foundation Service
This is the third of six tasks required to create a Windows Communication Foundation (WCF) applicati ...
- ubuntu 16.4下hadoop配置伪分布式时出现的坑
在ubuntu16.4下spark的单机/伪分布式配置我在此就不在一一赘述,详情请点击如下连接: Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04 我出现问题是 ...
- loj#2391 「JOISC 2017 Day 1」港口设施
分析 https://yhx-12243.github.io/OI-transit/records/uoj356%3Bloj2391%3Bac2534.html 代码 #include<bits ...
- 牛客提高D5t1 deco的abs
分析 傻子题? 对d取模后随便贪心即可 代码 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- PostgreSQL 在视频、图片去重,图像搜索业务中的应用
摘要: PostgreSQL 在视频.图片去重,图像搜索业务中的应用作者digoal日期2016-11-26标签PostgreSQL , Haar wavelet , 图像搜索 , 图片去重 , 视频 ...
- JSP基础--三大指令
JSP指令 1 JSP指令概述 JSP指令的格式:<%@指令名 attr1=”” attr2=”” %>,一般都会把JSP指令放到JSP文件的最上方,但这不是必须的. JSP中 ...
- G a+b+c+d=?
G a+b+c+d=? 链接:https://ac.nowcoder.com/acm/contest/338/G来源:牛客网 题目描述 This is a very simple problem! Y ...
- iBatis——自动生成DAO层接口提供操作函数(详解)
iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...