码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 力扣(LeetCode)243. 最短单词距离(2022.08.31)


    请设计一个类,使该类的构造函数能够接收一个字符串数组。然后再实现一个方法,该方法能够分别接收两个单词,并返回列表中这两个单词之间的最短距离。

    实现 WordDistanc 类:

    WordDistance(String[] wordsDict) 用字符串数组 wordsDict 初始化对象。
    int shortest(String word1, String word2) 返回数组 worddict 中 word1 和 word2 之间的最短距离。

    示例 1:

    输入:
    [“WordDistance”, “shortest”, “shortest”]
    [[[“practice”, “makes”, “perfect”, “coding”, “makes”]], [“coding”, “practice”], [“makes”, “coding”]]
    输出:
    [null, 3, 1]

    解释:

    WordDistance wordDistance = new WordDistance([“practice”, “makes”, “perfect”, “coding”, “makes”]);
    wordDistance.shortest(“coding”, “practice”); // 返回 3
    wordDistance.shortest(“makes”, “coding”); // 返回 1

    注意:

    1 <= wordsDict.length <= 3 * 104
    1 <= wordsDict[i].length <= 10
    wordsDict[i] 由小写英文字母组成
    word1 和 word2 在数组 wordsDict 中
    word1 != word2
    shortest 操作次数不大于 5000

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/shortest-word-distance-ii

    方法一:哈希表+双指针

    C++提交内容:

    class WordDistance {
    public:
        WordDistance(vector<string>& wordsDict) {
            int length = wordsDict.size();
            for (int i = 0; i < length; i++) {
                string word = wordsDict[i];
                indicesMap[word].emplace_back(i);
            }
        }
        
        int shortest(string word1, string word2) {
            vector<int> indices1 = indicesMap[word1];
            vector<int> indices2 = indicesMap[word2];
            int size1 = indices1.size(), size2 = indices2.size();
            int pos1 = 0, pos2 = 0;
            int ans = INT_MAX;
            while (pos1 < size1 && pos2 < size2) {
                int index1 = indices1[pos1], index2 = indices2[pos2];
                ans = min(ans, abs(index1 - index2));
                if (index1 < index2) {
                    pos1++;
                } else {
                    pos2++;
                }
            }
            return ans;
        }
    private:
        unordered_map<string, vector<int>> indicesMap;
    };
    
    /**
     * Your WordDistance object will be instantiated and called as such:
     * WordDistance* obj = new WordDistance(wordsDict);
     * int param_1 = obj->shortest(word1,word2);
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    【ChatGPT】人工智能的下一个前沿
    艾美捷热转移稳定性检测试剂盒解决方案
    看懂领导的这三个想法,能让领导刮目相看
    郑州大学图书馆许少辉《乡村振兴战略下传统村落文化旅游设计》中文文献——2023学生开学季辉少许
    Idea中查看SpringSecurity各Filter信息
    JWT生成与解析/JWT令牌前端存储
    linux中利用fork复制进程,printf隐藏的缓冲区,写时拷贝技术,进程的逻辑地址与物理地址
    Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
    卷积神经网络 异常检测,卷积神经网络故障检测
    c++类和对象(二)六个默认成员函数。
  • 原文地址:https://blog.csdn.net/ChaoYue_miku/article/details/126653427
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号