【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. ...
随机推荐
- OSI参考模型和网络排错
OSI七层协议 应用层 应用程序通信服务 表示层 显示 加密 数据格式 会话层 服务器和客户机建立会话 netstat -nb 查看会话 mscofig 传输层 可靠回话传输 分段 ...
- apache主要配置详解
1. # Deny access to the entirety of your server's filesystem. You must # explicitly permit access to ...
- ORA-02298问题处理
参考:http://blog.163.com/yvtong@126/blog/static/8753524720132223343722/ ORA-39083: Object type REF_CON ...
- 安卓中java和js如何交互
1.安卓中java和js如何交互 在Android上怎样实现JAVA和JS交互呢?Android的webview是基于webkit内核的,webview中集成了js与java互调的接口函数,通过add ...
- 阅读笔记06-架构师必备最全SQL优化方案(2)
四.基础优化 1.优化思路? 定位问题点吮吸:硬件-->系统-->应用-->数据库-->架构(高可用.读写分离.分库分表). 处理方向:明确优化目标.性能和安全的折中.防患未然 ...
- vc code 一个非常不错的插件
https://marketplace.visualstudio.com/items?itemName=CoenraadS.bracket-pair-colorizer 这个是地址,前提下是安装了vc ...
- Excelvba从文件中逐行读取并写入excel中
Sub 読み込む() Dim result As Long Dim dialog As FileDialog Set dialog = Application.FileDialog(msoFileDi ...
- [BZOJ 4771]七彩树(可持久化线段树+树上差分)
[BZOJ 4771]七彩树(可持久化线段树+树上差分) 题面 给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节点的颜色为c[i].如果c[i] ...
- html中head示例
<!DOCTYPE html> <!-- #统一的规范--> <!--类似html这种 ,html标签<html>dasdasd</html>&g ...
- NGUI的Tween动画的使用
一,在创建Tween有,alpha,color,width,height,position,rotation,scale和transfrom这几种动画类型 1>alpha:颜色由浅变深(透明度) ...