题目描述:

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相关推荐

  1. 【To Understand!回文串8 哈希表】LeetCode 336. Palindrome Pairs

    LeetCode 336. Palindrome Pairs Solution1:我的答案 最笨的方法,果然因为时间复杂度太高而无法AC-- 难怪是hard 只是记录一下-- class Soluti ...

  2. LeetCode-336 Palindrome Pairs

    题目描述 Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so t ...

  3. 336. Palindrome Pairs 回文对

    给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串. 示例 1: 输入: ["abcd",& ...

  4. 336(see). Palindrome Pairs 5.(see)

    336题:两种方法:1笨方法,注意在判断a+b组成的串是否为回旋的时候,不要将两者相加,直接判断就不会超时.2.将输入的vector<string>& words反向后用map存储 ...

  5. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  6. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  7. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  8. Leetcode Trie Conclusion

    Implement Trie (Prefix Tree) 208. Implement Trie (Prefix Tree) Add and Search Word - Data structure ...

  9. LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本

    1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...

最新文章

  1. No module named 'MySQLdb'
  2. Android数据存储方式
  3. Titanic数据分析
  4. TF之AutoML之AdaNet框架:AdaNet框架的简介、特点、使用方法详细攻略
  5. 左值和左值引用、右值和右值引用
  6. Python 3实现k-邻近算法以及 iris 数据集分类应用
  7. C#中使用NPIO实现导入导出Excel简单操作
  8. py2neo 数据类型
  9. android的listview点击获取当前选项值的方法
  10. 【python】pyhton中的and
  11. 把二叉树打印成多行(C++)
  12. 同域下iframe父页面和子页面调用
  13. nvidia显卡驱动安装失败怎么办?
  14. 华为路由器ip地址和mac地址绑定命令
  15. php图片颤抖,PHP-使用jquery 怎么做出图片的震荡效果
  16. 第二章-FPGA的概要-《FPGA的原理与结构》
  17. 2021-01-22
  18. Python3.7 安装Airflow 报错tenacity.async import AsyncRetrying
  19. NOIP学习之函数与过程抽象:91.质数的和与积
  20. 对于分布式集群,应该思考什么问题?

热门文章

  1. macos docker挂载iso报failed to setup loop device: No such file or directory和mount: permission denied解决
  2. 优化移动端邮件营销效果办法
  3. Excel高级筛选操作图解
  4. 谷歌工程师薪资有多少 羡慕嫉妒恨
  5. 成都艾司博讯:拼多多营销账户无法提现是为什么?
  6. Android工程缺少SDK解决方案
  7. 【DL】第 3 章:高级卷积网络
  8. Cadence从原理图到PCB的具体步骤
  9. 林达华推荐的几本数学书
  10. 【研究方法】好的研究想法从哪里来--刘知远