• [LeetCode] Substring with Concatenation of All Words


    方法一:暴力搜索

    针对S中每个字符开始搜索一次长度为wordLen*len的字符串,是否包含L中的所有单词。
    这个很好统计,Map就可以直接搞定。思路很好想,同时也很费时。

    超时

    class Solution {
        map<string, int> m_map;
        public:
            void initMap(vector<string> &L)
            {
                m_map.clear();
                for(size_t i = 0; i <L.size() ; i++)
                {
                    if(m_map.find(L[i]) == m_map.end())
                        m_map[L[i]] = 1;
                    else
                        m_map [L[i]]++;
                }
            }
    
            void printMap()
            {
                map<string,int>::iterator it;
                for(it = m_map.begin(); it != m_map.end(); it++)
                {
                    cout << it->first << "	-->	" << it->second << endl;
                }
                cout << endl;
            }
    
            vector<int> findSubstring(string S, vector<string> &L)
            {
                vector<int> result;
                if(L.size() == 0)
                    return result;
    
                size_t wordLen = L[0].size();
                size_t wordNum = L.size();
                size_t wordsLen = wordLen * wordNum;
    
                if(S.size() < wordsLen)
                    return result;
    
                //cout << "wordLen 	" << wordLen << endl;
                //cout << "wordNum 	" << wordNum<< endl;
                //cout << "wordsLen	" << wordsLen << endl;
    
                initMap(L);
                //printMap();
    
                for(size_t i = 0; i <= S.size() - wordsLen; i++)
                {
                    size_t j = i;
                    for( /**/; j < (i + wordsLen); j += wordLen)
                    {
                        //cout << "j	" << j << endl;
                        //printMap();
                        string tmp = S.substr(j, wordLen);
                        if(m_map.find(tmp) != m_map.end() && m_map[tmp] > 0)
                        {
                            m_map[tmp]--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    //cout << "==j	" << j << endl;
                    if(j >= (i+wordsLen))
                    {
                        result.push_back(i);
                    }
                    initMap(L);
                }
                return result;
    
            }
    };

     方法二:

      把上面的map直接改成unordered_map就AC了,我去,为此直接查找了一番map和unordered_map的区别

      map 是基于红黑树,查找时间为logN, 而unordered_map是基于hash的,查找时间理论为O(1)

      连接 http://zrj.me/archives/1248 解析unordered_map

      https://msdn.microsoft.com/zh-cn/library/bb982522.aspx

    此模板类描述用于控制 std::pair<const Key, Ty> 类型的变长元素序列的对象。 序列由哈希函数弱排序,哈希函数将此序列分区到称为存储桶的有序序列集中。 在每个存储桶中,比较函数将确定任一元素对是否具有等效顺序。 每个元素存储两个对象,包括一个排序键和一个值。 序列以允许查找、插入和移除任意元素的方式表示,并包含与序列中的元素数量无关的多个操作(常量时间),至少在所有存储桶长度大致相等时如此。 在最坏情况下,当所有元素位于一个存储桶中时,操作数量与序列中的元素数量成比例(线性时间)。 此外,插入元素不会使迭代器失效,移除元素仅会使指向已移除元素的迭代器失效。

    template<class Key,
        class Ty,
        class Hash = std::hash<Key>,
        class Pred = std::equal_to<Key>,
        class Alloc = std::allocator<std::pair<const Key, Ty> > >
        class unordered_map;

     另外,unordered_map是C++11引入的,要想使用unordered_map, 需要加上 g++ main.cpp  -std=c++11 选项。

  • 相关阅读:
    Application
    Intent
    C#Winform实现自动更新
    Activity的四种启动模式
    小白学Python——用 百度翻译API 实现 翻译功能
    小白学Python——用 百度AI 实现 OCR 文字识别
    小白学Python——Matplotlib 学习(3) 函数图形
    小白学Python——Matplotlib 学习(2):pyplot 画图
    小白学Python——Matplotlib 学习(1)
    小白学Python(20)—— Turtle 海龟绘图
  • 原文地址:https://www.cnblogs.com/diegodu/p/4282041.html
Copyright © 2020-2023  润新知