[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离

简介:

18.5 You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution? 

LeetCode上的原题,请参见我之前的博客Shortest Word DistanceShortest Word Distance II和 Shortest Word Distance III

解法一:

// Call One Time
int shortest_dist(vector<string> words, string word1, string word2) {
    int p1 = -1, p2 = -1, res = INT_MAX;
    for (int i = 0; i < words.size(); ++i) {
        if (words[i] == word1) p1 = i;
        if (words[i] == word2) p2 = i;
        if (p1 != -1 && p2 != -1) res = min(res, abs(p1 - p2));
    }
    return res;
}

解法二:

// Call Many Times
int shortest_dist(vector<string> words, string word1, string word2) {
    unordered_map<string, vector<int>> m;
    int i = 0, j = 0, res = INT_MAX;
    for (int i = 0; i < words.size(); ++i) {
        m[words[i]].push_back(i);
    }
    while (i < m[word1].size() && j < m[word2].size()) {
        res = min(res, abs(m[word1][i] - m[word2][j]));
        m[word1][i] < m[word2][j] ? ++i : ++j;
    }
    return res;
}

解法三:

// word1, word2 may be same
int shortest_dist(vector<string> words, string word1, string word2) {
    int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
    for (int i = 0; i < words.size(); ++i) {
        if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
        if (words[i] == word2) p2 = i;
        res = min(res, abs(p1 - p2));
    }
    return res;
}

本文转自博客园Grandyang的博客,原文链接:两单词间的最短距离[CareerCup] 18.5 Shortest Distance between Two Words ,如需转载请自行联系原博主。

相关文章
|
API 调度 语音技术
基于Qt的简易语音助手设计与实现
基于Qt的简易语音助手设计与实现
805 2
|
7月前
|
人工智能 Java 编译器
Java:面向对象
本文介绍了Java编程中的核心概念,包括包的命名规范与自动导入机制、构造方法的特点与使用、`this`和`super`关键字的作用、继承的基本规则、访问权限的设置、封装的意义、多态的实现原理以及`static`关键字的用法。通过详细解析每个知识点,并结合代码示例,帮助读者深入理解Java面向对象编程的核心思想与实践技巧。内容适合初学者及进阶开发者学习参考。
167 0
|
前端开发
【前端性能优化方法与实战】
【前端性能优化方法与实战】
|
消息中间件 Java 中间件
一个空格引发的“救火之旅”:记一次SOFA RPC的排查过程
一个空格引发的“救火之旅”:记一次SOFA RPC的排查过程
一个空格引发的“救火之旅”:记一次SOFA RPC的排查过程
|
测试技术 调度
性能测试(9)——线程组详解
线程数:虚拟用户数 Ramp-Up时间(秒):启动全部虚拟用户数所需要的时间 循环次数:指定次数或勾选永远。使用调度器时,必须勾选永远 延迟创建线程直到需要:测试开始的时候,所有线程就被创建完。勾选了此选项,那么线程只会在合适的需要用到的时候创建 调度器:勾选后,调度器配置才能使用;
340 0
|
Java 大数据 Linux
大数据基本开发工具的构建工具的Gradle
在大数据开发过程中,构建工具是必不可少的。Gradle是一种基于Apache Maven和Apache Ant的自动化构建工具,广泛应用于Java、Scala和Kotlin等编程语言的项目构建。本文将介绍Gradle的特点、安装和使用方法。
196 0
|
程序员 Python
牛客题霸在线编程Python题库——Python入门到实践40招(四)(五)(六)循环语句 条件语句 元组
牛客题霸在线编程Python题库——Python入门到实践40招(四)(五)(六)循环语句 条件语句 元组
|
人工智能 算法 Ubuntu
MobileNet实战:tensorflow2.X版本,MobileNetV1图像分类任务(大数据集)
本例提取了植物幼苗数据集中的部分数据做数据集,数据集共有12种类别,今天我和大家一起实现tensorflow2.X版本图像分类任务,分类的模型使用MobileNetV1。本文实现的算法有一下几个特点: 1、自定义了图片加载方式,更加灵活高效,不用将图片一次性加载到内存中,节省内存,适合大规模数据集。 2、加载模型的预训练权重,训练时间更短。 3、数据增强选用albumentations。
266 0
MobileNet实战:tensorflow2.X版本,MobileNetV1图像分类任务(大数据集)
AppleWatch开发入门五——菜单控件的使用
AppleWatch开发入门五——菜单控件的使用
303 0
AppleWatch开发入门五——菜单控件的使用