Palindrome Pairs
题目描述:
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"]
解题思路:
对于一个字符串,例如"abcd",其反转后的字符串为"dcba",把反转后的字符串加到原字符串的前面和后面都可以构成一个回文字符串,"dcbaabcd"和"abcddcba"均为回文字符串。因此可以对words中的每一个非空字符串求其反转后的字符串,然后判断反转后的字符串是否也在words中即可。由于把反转后的字符串的一部分加到原字符串就可以构成回文字符串,例如对于字符串"sssll",其反转后的字符串为"llsss",把"llsss"的子串"lls"加到原字符串就可以构成回文字符串,因此需要对反转后的字符串的每一个子串都进行测试,即把反转后的字符串的每一个子串加到原字符串,看是否可以构成回文字符串。
AC代码如下:
class Solution {
public:vector<vector<int>> palindromePairs(vector<string>& words) {set<vector<int>> ans;int n = words.size();if (n <= 1) return vector<vector<int>>();map<string, int> dicts;for (int i = 0; i < n; ++i){dicts[words[i]] = i;}for (int i = 0; i < n; ++i){if (words[i] == "") continue;if (isPalindrome(words[i])){if (dicts.find("") != dicts.end()){vector<int> pos1;pos1.push_back(dicts[""]);pos1.push_back(i);ans.insert(pos1);vector<int> pos2;pos2.push_back(i);pos2.push_back(dicts[""]);ans.insert(pos2);}}string str_reverse = string(words[i].rbegin(), words[i].rend());int len = str_reverse.size();for (int j = 1; j <= len; ++j){string str1 = str_reverse.substr(0, j);//forwardif (isPalindrome(str1 + words[i]) && dicts.find(str1) != dicts.end()){vector<int> pos;pos.push_back(dicts[str1]);pos.push_back(i);if (dicts[str1]!=i)ans.insert(pos);}//backwardstring str2 = str_reverse.substr(len - j);if (isPalindrome(words[i] + str2) && dicts.find(str2) != dicts.end()){vector<int> pos;pos.push_back(i);pos.push_back(dicts[str2]);if (dicts[str2] != i)ans.insert(pos);}}}return vector<vector<int>>(ans.begin(), ans.end());}
private:bool isPalindrome(const string& str){for (int i = 0, j = str.size() - 1; i < j; ++i, --j)if (str[i] != str[j]) return false;return true;}
};
Palindrome Pairs相关推荐
- 【To Understand!回文串8 哈希表】LeetCode 336. Palindrome Pairs
LeetCode 336. Palindrome Pairs Solution1:我的答案 最笨的方法,果然因为时间复杂度太高而无法AC-- 难怪是hard 只是记录一下-- class Soluti ...
- LeetCode-336 Palindrome Pairs
题目描述 Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so t ...
- 336. Palindrome Pairs 回文对
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串. 示例 1: 输入: ["abcd",& ...
- 336(see). Palindrome Pairs 5.(see)
336题:两种方法:1笨方法,注意在判断a+b组成的串是否为回旋的时候,不要将两者相加,直接判断就不会超时.2.将输入的vector<string>& words反向后用map存储 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- Leetcode Trie Conclusion
Implement Trie (Prefix Tree) 208. Implement Trie (Prefix Tree) Add and Search Word - Data structure ...
- LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本
1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...
最新文章
- No module named 'MySQLdb'
- Android数据存储方式
- Titanic数据分析
- TF之AutoML之AdaNet框架:AdaNet框架的简介、特点、使用方法详细攻略
- 左值和左值引用、右值和右值引用
- Python 3实现k-邻近算法以及 iris 数据集分类应用
- C#中使用NPIO实现导入导出Excel简单操作
- py2neo 数据类型
- android的listview点击获取当前选项值的方法
- 【python】pyhton中的and
- 把二叉树打印成多行(C++)
- 同域下iframe父页面和子页面调用
- nvidia显卡驱动安装失败怎么办?
- 华为路由器ip地址和mac地址绑定命令
- php图片颤抖,PHP-使用jquery 怎么做出图片的震荡效果
- 第二章-FPGA的概要-《FPGA的原理与结构》
- 2021-01-22
- Python3.7 安装Airflow 报错tenacity.async import AsyncRetrying
- NOIP学习之函数与过程抽象:91.质数的和与积
- 对于分布式集群,应该思考什么问题?