• leetcode 336. 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"]

    Credits:
    Special thanks to @dietpepsi for adding this problem and creating all test cases.

    Subscribe to see which companies asked this question

    Hide Tags
     Hash Table String Trie
    Show Similar Problems
     
     
    转一发题解:
     

    题意:给出一个有n个单词的字典,问有多少不同的两个单词的组合使得新组合成的单词是回文串。

    思路:对于每个单词,从左到右遍历一遍,对于任意一个位置,如果它的左边是回文串且右边的逆序在字典中出现,那么就存在这么一种组合,同理,如果它的右边是回文串且左边的逆序在字典中出现,那么也存在这么一种组合。

    真是好题啊,涉及到好多知识点

    不过还有个小疑问,为什么用 map 就超时了,用 unordered_map 就过了

     

    STL::unordered_map之无序map

     http://blog.csdn.net/devourheavens/article/details/7539815

    哈希map是一种关联容器,通过键值和映射值存储元素。允许根据键值快速检索各个元素。

    在unordered_map中,键值一般用来唯一标识元素,而对应的值是一个对象关联到这个键的内容。键映射值的类型可能会有所不同。
    在内部unordered_map的元素以键值或映射的元素作任何特定的顺序排序,其存储位置取决于哈希值允许直接通过其键值为快速访问单个元素(具有恒定平均的平均时间复杂度)。
    unordered_map容器比map容器更快地通过键值访问他们的单个元素,虽然unordered_map一般都是比map通过其元素的一个子集范围迭代效率低
    哈希map允许使用操作运算符(运算符[])以其键值作为参数直接访问元素。

    Submission Details

    134 / 134 test cases passed.
    Status: 

    Accepted

    Runtime: 1804 ms
     1 class Solution {
     2 public:
     3     vector<vector<int>> palindromePairs(vector<string>& words) {
     4         vector<vector<int> > ans;
     5         unordered_map<string,int> mp;
     6         int n = words.size();
     7         int i,j;
     8         for(i = 0;i < n;i++){
     9             mp[ words[i] ] = i;
    10         }
    11         
    12         for(i = 0;i < n;i++){
    13             int l = words[i].length();
    14             if(l == 0){         //空串
    15                 for(int k = 0;k < n;k++)  
    16                     if(k != i && ispa(words[k])) ans.push_back(vector<int>{i,k}); 
    17                 continue;
    18             }  
    19             mp.erase(words[i]);    //本身是回文的情况,会造成错误
    20             for(j = 0;j < l;j ++){  
    21                 string subL = words[i].substr(0,j);  
    22                 string subR = words[i].substr(j,l);  
    23                 string revL,revR;  
    24                 revL.assign(subL.rbegin(),subL.rend());  
    25                 revR.assign(subR.rbegin(),subR.rend());  
    26                 //j左边是回文,并且j右边的逆序找到(包括j)
    27                 if( mp.find(revR) != mp.end() && ispa(subL)){ 
    28                     ans.push_back(vector<int>{mp[revR],i} );
    29                 }
    30                 //j右边是回文,并且j左边的逆序找到(包括j)
    31                 if( mp.find(revL) != mp.end() && ispa(subR)){
    32                     ans.push_back(vector<int>{i,mp[revL]} );
    33                 }
    34             }
    35             mp[ words[i] ] = i;
    36         }
    37         return ans;
    38     }
    39     bool ispa(string& s)
    40     {
    41         int i = 0;
    42         int l = s.length();
    43         int j = l - 1;
    44         while(i <= j){
    45             if(s[i] != s[j]) return false;
    46             i++;j--;
    47         }
    48         return true;
    49     }
    50 };

     C++STL之string

    http://blog.csdn.net/y990041769/article/details/8763366

    http://www.cnblogs.com/aicro/archive/2010/01/15/1642659.html

    a) =,assign()   //赋以新值

    s.assign(b,e);  用迭代器b到e范围内的元素替换s

    STL map常用操作简介

    http://www.cnblogs.com/TianFang/archive/2006/12/30/607859.html

    7。从map中删除元素

    移除某个map中某个条目用erase()

    该成员方法的定义如下

    1. iterator erase(iterator it); //通过一个条目对象删除
    2. iterator erase(iterator first, iterator last);        //删除一个范围
    3. size_type erase(const Key& key); //通过关键字删除

    clear()就相当于 enumMap.erase(enumMap.begin(), enumMap.end());

  • 相关阅读:
    Winform 异步更新listbox
    Object-C
    易学易懂
    C# login with cookie and fiddler2
    开源 侧滑 和 Tab滑动翻页 控件
    Mysql Java type mapping
    jQuery滑动导航菜单
    js判断是移动端还是pc端
    设为主页和加入收藏
    原生javascript效果:无缝滚动
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5478314.html
Copyright © 2020-2023  润新知