336. Palindrome Pairs 寻找回文对
来源:互联网 发布:javascript和java的区别 编辑:程序博客网 时间:2024/06/11 23:49
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"]
就是遍历每个字符串,然后两两结合看是不是回文串
class Solution {public: bool isPali(string s1, string s2){ string s = s1+s2; for(int k = 0; k < s.size()/2; k++){ if(s[k] != s[s.size()-1-k]) return false; } return true; } vector<vector<int>> palindromePairs(vector<string>& words) { int len = words.size(); vector<vector<int>>res; if(len <= 0) return res; for(int i = 0; i < len; i++) for(int j = i+1; j < len; j++){ if(isPali(words[i], words[j])){ vector<int> vec; vec.push_back(i); vec.push_back(j); res.push_back(vec); } if(isPali(words[j], words[i])){ vector<int> vec; vec.push_back(j); vec.push_back(i); res.push_back(vec); } } return res; }};
2.hash table
对每个字符串,对其倒置,对每个子串进行原始words中的匹配查找,若子串能找到且剩下一部分是回文,则可以组成回文串,加入结果集中。这里对左右两边的子串都进行判断。
注意:
1. 在第一个判断中加入j<len,是为了防止二次判断。
如:abcd, dcba
当对abcd字符串时,先reserve,然后从左边第0个开始分割,则dcba能找到;当分割到最后一个时,得到的又是dcba,则此时不加入结果集;因为当在对第二个字符串dcba开始处理时,会重复到。
class Solution {public: bool isvalid(string s){ int i = 0, j = s.size()-1; while(i < j){ if(s[i] != s[j]) return false; i++; j--; } return true; } vector<vector<int>> palindromePairs(vector<string>& words) { vector<vector<int>>res; map<string, int>map_str; for(int i = 0; i < words.size(); i++) map_str[words[i]] = i; for(int j = 0; j < words.size(); j++){ reverse(words[j].begin(), words[j].end()); int len = words[j].size(); for(int k = 0; k <= len; k++){ string left = words[j].substr(0,k); string right = words[j].substr(k); if(map_str.count(left) && isvalid(right)&&(map_str[left] != j)&&(k < len)) res.push_back(vector<int>{map_str[left], j}); if(map_str.count(right) && isvalid(left)&&(map_str[right] != j)) res.push_back(vector<int>{j, map_str[right]}); } } return res; }};
3.用trie树
下次再补
- 336. Palindrome Pairs 寻找回文对
- LeetCode 336. Palindrome Pairs(回文对)
- leetcode 336. Palindrome Pairs 回文数字符串对
- leetcode : 336. Palindrome Pairs : 拼接回文的个数
- LeetCode 336 Palindrome Pairs(回文串)
- LeetCode 336. Palindrome Pairs
- LeetCode 336. Palindrome Pairs
- leetcode 336. Palindrome Pairs
- LeetCode 336. Palindrome Pairs
- leetcode.336. Palindrome Pairs
- 336. Palindrome Pairs
- 336. Palindrome Pairs
- [leetcode] 336. Palindrome Pairs
- 336. Palindrome Pairs
- LeetCode-336.Palindrome Pairs
- LeetCode 336. Palindrome Pairs
- 336. Palindrome Pairs
- 336. Palindrome Pairs
- Umbraco CMS 入住autoyu博客 Umbraco 我会喜欢你吗
- 用Docker封装一个web应用(Django)
- MyBatis Generator generatorConfig.xml的配置
- Android之PopupWindow 在指定位置上的显示
- 根据文字返回长度
- 336. Palindrome Pairs 寻找回文对
- 关于创建自定义控件的那些事(笔记)
- bucket sort
- 80端口被占用,杀死该进程的方法
- hdoj1025Constructing Roads In JGShining's Kingdom(LIS)
- 【Consul】CONSUL环境部署
- 线性表链式存储结构基本操作
- 小鸡腿U T3 成都2007
- protocol buffer数据转换