[Leetcode] 336. Palindrome Pairs_Hard
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
Example 1:
Given words = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]
Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
这道题的思路就是把每个word和index存到diction里面, 然后针对每个word, for loop, 拆分为两部分, 如果前面部分是palindrome, 那么把后面部分的单词reverse, 如果reverse后的单词在diction里面, ans.append(d[back], index); 同理, 如果后面部分是palindrome, 把前面部分的单词reverse, 如果reverse后的单词在diction里面, ans.append(index, d[back]). 此时需要注意的是在后面check的时候就不要考虑整个单词reverse的情况, 因为前面单词reverse的时候已经考虑到了. 如果不明白的话就用set去去重, 最保险的做法.
参考Solution.
class Solution(object):
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
def checkpal(w):
return w == w[::-1] # 反正O(n), 所以用最粗暴的方式 d, ans = {w:index for index, w in enumerate(words)},[]
for word, index in d.items():
l = len(word)
for i in range(l+1): # l+1 因为pref要到[:l]
pref = word[:i]
suf = word[i:]
if checkpal(pref):
back = suf[::-1]
if back != word and back in d:
ans.append([d[back], index]) if i != l and checkpal(suf): # delete the duplicates
back = pref[::-1]
if back != word and back in d:
ans.append([index, d[back]])
return ans
[Leetcode] 336. Palindrome Pairs_Hard的更多相关文章
- leetcode@ [336] Palindrome Pairs (HashMap)
		https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ... 
- LeetCode 336. Palindrome Pairs
		原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ... 
- [LeetCode] 131. Palindrome Partitioning 回文分割
		Given a string s, partition s such that every substring of the partition is a palindrome. Return all ... 
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
		Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ... 
- Palindrome Pairs -- LeetCode 336
		Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ... 
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ... 
- 【LeetCode】Palindrome Pairs(336)
		1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ... 
- 【leetcode】336. Palindrome Pairs
		题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ... 
- [LeetCode] Longest Palindrome 最长回文串
		Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ... 
随机推荐
- css笔记 - 张鑫旭css课程笔记之 relative 篇
			relative地址 relative 对 absolute的限制作用 限制left/top/right/bottom(方位值)定位 限制描述:absolute设置了方位值时,这些方位值是相对于pos ... 
- EGit 在使用时出现 git-upload-pack not found 的错误
			在 Ubuntu 上使用Eclipse 的插件 EGit 从 GitHub 上下载资源时出现 git-upload-pack not found 的错误,如图 (另,EGit 在 Windows 上的 ... 
- 原生js--编码请求主体(异步请求)
			1.表单编码请求 需要对每个表单元素进行普通的URL编码,使用“=”把编码后的名字和值分开,并使用“&”分开名值对. 例如:a=b&c=d 表单数据编码的MIME类型:applicat ... 
- 中国标准时间、‘yyyy-MM-dd’格式时间转为时间戳
			中国标准时间转为时间戳 let _time="Tue Mar 20 2018 00:00:00 GMT+0800 (中国标准时间)"; console.log(Date.parse ... 
- Cordova 3.3 开发环境搭建(视频)
			图文文章参见: http://www.cnblogs.com/mlzs/p/3332199.html 视频共享链接 百度:http://pan.baidu.com/s/1c0EHfqC 
- magent实现memcached集群的一个问题
			之前我们小组封装了一个memcached类库,里面有一个名为RemoveStartWith的方法可以根据起始字符串删除所有节点中负责键值规则的缓存项.它实现的原理就是通过stats命令获取每个节点的所 ... 
- Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1
			退出当前环境: logout (再重新登录进去) yum install python-devel -yyum install libevent-devel -y 把环境更新下yum instal ... 
- 记录一下使用Ubuntu16.0.4配置和使用docker registry
			h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ... 
- Unity3D笔记 英保通三 脚本编写 、物体间通信
			一.脚本编写 1.1.同一类型的方法JS和C#的书写方式却不一样主要还是语法,在工程中创建一个Cube 分别把JSTest.js和CSharp.cs 添加到Cube中 JSTest.js #pragm ... 
- iOS - 仿微信朋友圈视频剪切功能
			分析需求 我们先看一看微信的界面 微信效果图 1.页面下部拖动左边和右边的白色竖条控制剪切视频的开始和结束时间,预览界面跟随拖动位置跳到视频相应帧画面,控制视频长度最长15秒,最短5秒 2.拖动下部图 ... 
