zoukankan      html  css  js  c++  java
  • LeetCode "Shortest Word Distance"

    A variation of linear scan..

    typedef pair<int, bool> Rec;
    class Solution {
    public:
        int shortestDistance(vector<string>& words, string word1, string word2)     {
            vector<Rec> recs;
    
            int ret = INT_MAX;
            for (int i = 0; i < words.size(); i++)
            {
                bool bFound = false;
                if (words[i] == word1){
                    bFound = true;
                    recs.push_back(Rec(i, true));
                }
                if (words[i] == word2){
                    bFound = true;
                    recs.push_back(Rec(i, false));
                }
                if (bFound)
                {
                    size_t len = recs.size();
                    if (recs.size() > 1)
                    {
                        if (recs[len - 1].second != recs[len - 2].second)
                        {
                            int cur = recs[len - 1].first - recs[len - 2].first;
                            ret = std::min(cur, ret);
                        }
                    }
                }
            }
            return ret;
        }
    };
  • 相关阅读:
    $dp$模板
    字符串基础
    基础算法

    图论
    山中无甲子,寒尽不知年
    模板集合(持续更新)
    数学基础——同余
    9.19 考试总结
    1-5-17:菲波那契数列
  • 原文地址:https://www.cnblogs.com/tonix/p/4745336.html
Copyright © 2011-2022 走看看