Palindrome Pairs -- LeetCode 336
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"]
Links: https://leetcode.com/discuss/93599/easy-to-understand-ac-c-solution-o-n-k-2-using-map
Solution 1:
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
unordered_map<string, int> dict;
vector<vector<int>> ans;
// build dictionary
for(int i = ; i < words.size(); i++) {
string key = words[i];
reverse(key.begin(), key.end());
dict[key] = i;
}
// edge case: if empty string "" exists, find all palindromes to become pairs ("", self)
if(dict.find("")!=dict.end()){
for(int i = ; i < words.size(); i++){
if(i == dict[""]) continue;
if(isPalindrome(words[i])) ans.push_back({dict[""], i});
}
} for(int i = ; i < words.size(); i++) {
for(int j = ; j < words[i].size(); j++) {
string left = words[i].substr(, j);
string right = words[i].substr(j, words[i].size() - j); if(dict.find(left) != dict.end() && isPalindrome(right)
&& dict[left] != i) {
ans.push_back({i, dict[left]});
} if(dict.find(right) != dict.end() && isPalindrome(left)
&& dict[right] != i) {
ans.push_back({dict[right], i});
}
}
} return ans;
} bool isPalindrome(string str){
int i = ;
int j = str.size() - ; while(i < j) {
if(str[i++] != str[j--]) return false;
} return true;
} };
Solution 2:
Palindrome Pairs -- LeetCode 336的更多相关文章
- 【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
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 336. Palindrome Pairs(can't understand)
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- Leetcode 336.回文对
回文对 给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串. 示例 1: 输入: ["abcd&quo ...
随机推荐
- 一步一步将Vim打造成C++超级IDE
文/嶽永鹏 最近从MS Windows 转到了Liunx,花了一段时间熟悉和学习Liunx环境.有时候,真的很是怀念MS Vistual Studio那种超级智能的开发环境,总是想在Vim拾起那些曾进 ...
- C#获取全部目录和文件
public class FileAccess{ //储存所有文件夹名 private ArrayList dirs; public FileAccess() { dirs = new ArrayLi ...
- Java开发工具安装步骤内容如下
Java开发工具安装步骤内容如下 安装 开发工具 STS 链接下载网址 eclipse 链接下载网址 JDK安装 jdk链接下载地址 Marven环境 marven链接下载地址 Tomcat tomc ...
- [MISSAJJ原创] UITableViewCell移动及翻转出现的3D动画效果[58同城cell移动效果]
2015-11-20 很喜欢在安静的状态, 听着音乐,敲着键盘, 和代码们浓情对话, 每一份代码的积累, 都让自己觉得很充实快乐!Y(^_^)Y. 看到58同城app的cell有动画移动出现的特效,很 ...
- dstoon系统中学习
$r = $db->get_one("SELECT * FROM {$DT_PRE}company WHERE username='$pay_user'");注意:usern ...
- nginx、Apache、IIS服务器解决 413 Request Entity Too Large问题方法汇总
一.nginx服务器 nginx出现这个问题的原因是请求实体太长了.一般出现种情况是Post请求时Body内容Post的数据太大了,如上传大文件过大.POST数据比较多. 处理方法 在nginx.co ...
- Apache、NGINX支持中文URL
Apache(32位):安装环境:CentOS 5.6 + Apache 2.2.15安装结果:安装后支持“中文图片.文件名”链接直接打开以下为安装过程:1.下载安装包 wget ftp://ftp. ...
- CSS各类标签用法——选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 列王的纷争,COK,675区,有去的没有?加群:159108918,盟的名字准备叫:大话西游
首先我承认我玩物丧志了 679区,有去的没有?加群: 474574809,盟的名字叫:Moon Box 如何练最强5级号,为新区做准备?! 粮食是可以为0的,士兵不会死,这是关键之一. 关键之二是新手 ...
- [转载]Bison-Flex 笔记
FLEX 什么是FLEX?它是一个自动化工具,可以按照定义好的规则自动生成一个C函数yylex(),也成为扫描器(Scanner).这个C函数把文本串作为输入,按照定义好的规则分析文本串中的字符,找到 ...