class Solution(object):
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
# https://leetcode.com/problems/palindrome-pairs/discuss/79219/Python-solution~
# reverse word and create a word to index map
word2index, res = dict([(w[::-1], i) for i, w in enumerate(words)]), []
for i, word in enumerate(words):
for j in xrange(len(word) + 1):
# Use prefix and postfix
# rather than going through all posible combinations
prefix, postfix = word[:j], word[j:]
# prefix + postfix + reverse(prfix)
if prefix in word2index and i != word2index[prefix] and postfix == postfix[::-1]:
res.append([i, word2index[prefix]])
# reverse(postfix) + prefix + postfix
if j > 0 and postfix in word2index and i != word2index[postfix] and prefix == prefix[::-1]:
res.append([word2index[postfix], i])
return res
python-leetcode面试题解之第336题回文对.zip
需积分: 1 20 浏览量
2024-06-25
09:01:48
上传
评论
收藏 954B ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
m0_57195758
- 粉丝: 1562
- 资源: 474
最新资源
- 用于英文命名实体识别(NER)的大型模型,用于识别英文文本中的命名实体,如人名、地名和组织机构等
- 头歌之计算机组成原理之计算机数据表示实验(HUST)实验运行代码
- windows本地简单调试kafka
- 头歌之计算机组成原理之运算器设计(HUST)实验运行代码
- 使用HTML来构建的时间同步的简单日历
- 第3题_于诗宁_20240512_NN&DL 实验11 搭建 Unet 语义分割 学生_1.ipynb
- 简单弹珠游戏(示例),简单的HTML弹珠游戏涉及到HTML、CSS用于构建界面
- 头歌之计算机组成原理之单总线CPU设计(定长指令周期3级时序)(HUST)实验运行代码
- c语言基础-prediction-maste开发笔记
- 俄罗斯方块c 语言ime-series-mast笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)