336 Palindrome Pairs 回文对
给定一组独特的单词, 找出在给定列表中不同 的索引对(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 回文对的更多相关文章
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- CF932G Palindrome Partition(回文自动机)
CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
随机推荐
- uva 1444 Knowledge for the masses
uva 1444 Description You are in a library equipped with bookracks that move on rails. There are ma ...
- Codeforces 990D - Graph And Its Complement
传送门:http://codeforces.com/contest/990/problem/D 这是一个构造问题. 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 对于 ...
- pogresql基础学习笔记
命令行工具:psql 可视化工具:pgAdmin 查看所有表: 命令行:\d sql:select * from pg_tables WHERE schemaname='public'; 查看表结构: ...
- nyoj 55 懒省事的小明(priority_queue优先队列)
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...
- HDU 2242 连通分量缩点+树形dp
题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...
- zoj——1311 Network
Network Time Limit: 2 Seconds Memory Limit: 65536 KB A Telephone Line Company (TLC) is establis ...
- Mutual Training for Wannafly Union #5
A(UVA12336) 题意:给一个n*m(n,m<=1e5)的棋盘,棋盘上有一些障碍点不能放棋子,现在要在棋盘上放4个棋子,满足A->B->C->D->A,其中走的规则 ...
- BMP的图像处理
近期碰到了一个问题将图片缩放: 进行了整理发现位图一些主要的结构能够进行整理,得出下面图表: 进行图片缩放的时候会进行一些处理(最临近差值法): 详细的代码例如以下: #include <std ...
- 犀牛Phinoceros 如何切换中文语言
Tools-Options-Rhino Options-Appearance,然后改成中文
- openCV—Python(2)—— 载入、显示和保存图像
一.函数简单介绍 1.imread-读取图像 函数原型:imread(filename, flags=None) filename:读取的图像路径名:比如:"H:\img\lena.jpg& ...