给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文。
示例 1:
给定 words = ["bat", "tab", "cat"]
返回 [[0, 1], [1, 0]]
回文是 ["battab", "tabbat"]
示例 2:
给定 words = ["abcd", "dcba", "lls", "s", "sssll"]
返回 [[0, 1], [1, 0], [3, 2], [2, 4]]
回文是 ["dcbaabcd", "abcddcba", "slls", "llssssll"]

详见:https://leetcode.com/problems/palindrome-pairs/description/

C++:

class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> res;
unordered_map<string, int> m;
set<int> s;
for (int i = 0; i < words.size(); ++i)
{
m[words[i]] = i;
s.insert(words[i].size());
}
for (int i = 0; i < words.size(); ++i)
{
string t = words[i];
int len = t.size();
reverse(t.begin(), t.end());
if (m.count(t) && m[t] != i)
{
res.push_back({i, m[t]});
}
auto a = s.find(len);
for (auto it = s.begin(); it != a; ++it)
{
int d = *it;
if (isValid(t, 0, len - d - 1) && m.count(t.substr(len - d)))
{
res.push_back({i, m[t.substr(len - d)]});
}
if (isValid(t, d, len - 1) && m.count(t.substr(0, d)))
{
res.push_back({m[t.substr(0, d)], i});
}
}
}
return res;
}
bool isValid(string t, int left, int right)
{
while (left < right)
{
if (t[left++] != t[right--])
{
return false;
}
}
return true;
}
};

参考:https://leetcode.com/problems/palindrome-pairs/description/

336 Palindrome Pairs 回文对的更多相关文章

  1. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  2. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  4. CF932G Palindrome Partition(回文自动机)

    CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  9. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

随机推荐

  1. centos7在grub界面下更改root密码

    想要更改root的密码或者忘记了root的密码的时候可以在grub界面下更改root的密码. 百度了很多内容,更多方法都是适用于centos6及以前版本的,终于找到一个可以的. 1.开机后,在下图界面 ...

  2. FJoi2017 1月20日模拟赛 交错和(等差数列+rmq)

    [题目描述] 无限循环数字串S由长度为n的循环节s构成.设s为12345(n=5),则数字串S为123451234512345… 设Si为S的第i位数字,在上面的例子中,S1=1,S2=2,S6=1. ...

  3. 添物零基础到大型全栈架构师 不花钱学计算机及编程(预备篇)— C语言编程基础

    ​C语言介绍 C语言基本是每个编程人员必学的一面语言,很好掌握,是理解编程的关键.很多编程语言基于其编写或者基于此语言的衍生品编写. C语言是人机交互的一个基础语言之一,虽然是之一,单一般其实就是唯一 ...

  4. 20170613NOIP模拟赛

    共3道题目,时间3小时 题目非原创,仅限校内交流使用 题目名称 Graph Incr Permutation 文件名 graph incr permutation 输入文件 graph.in incr ...

  5. centos7 安装mongodb3.4 及用户管理

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/1.semanage command not found yum ...

  6. kendo grid Hierarchy

    Hierarchy grid中不能使用下面的这段代码,会造成传值传不过来,把下面的代码注释,不用models,直接用form表单传值就行,暂时没搞明白为什么 //parameterMap: funct ...

  7. [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分

    Exercise bzoj-2097 Usaco-2010 Dec 题目大意:题目链接 注释:略. 想法:题目描述生怕你不知道这题在考二分. 关键是怎么验证?我们想到贪心的删边. 这样的策略是显然正确 ...

  8. 一个性能较好的jvm參数配置以及jvm的简单介绍

    一个性能较好的webserverjvm參数配置: -server //服务器模式 -Xmx2g //JVM最大同意分配的堆内存,按需分配 -Xms2g //JVM初始分配的堆内存.一般和Xmx配置成一 ...

  9. javascript 变量声明 和 作用域

    变量的声明 1.变量声明具有提升机制,Javascript在执行时,会把所有的声明都提升到当前作用域前面. 先看一下下面的例子: (function(){ alert(b)//b is not def ...

  10. java中inputstream的使用

    java中的inputstream是一个面向字节的流抽象类,其依据详细应用派生出各种详细的类. 比方FileInputStream就是继承于InputStream,专门用来读取文件流的对象,其详细继承 ...