LeetCode 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
题目标签:Array
题目给了我们一个words array, word1 和word2, 让我们找出word1 和word2 之间最短的距离。words array 里的word 可以重复出现。
基本思想是:当找到任何一个word 的时候,记录它的 index, 当两个word 都确定被找到的时候,用它们的index 相减来记录它们之间的距离
Java Solution:
Runtime beats 52.07%
完成日期:04/27/2017
关键词:Array
关键点:当找到任何一个word 时候,记录它的index;只有当两个word 都被找到的情况下,记录它们的距离
public class Solution
{
public int shortestDistance(String[] words, String word1, String word2)
{
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE; for(int i=0; i<words.length; i++)
{
if(words[i].equals(word1)) // if find word1, mark its position
p1 = i;
else if(words[i].equals(word2)) // if find word2, mark its position
p2 = i; if(p1 != -1 && p2 != -1) // if find word1 and word2, save the smaller distance
min = Math.min(min, Math.abs(p1 - p2));
} return min;
}
}
参考资料:
https://discuss.leetcode.com/topic/20668/ac-java-clean-solution
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 243. Shortest Word Distance (最短单词距离)$的更多相关文章
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [leetcode]243. Shortest Word Distance最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- 243. Shortest Word Distance 最短的单词index之差
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode#244] Shortest Word Distance II
Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...
随机推荐
- 在腾讯云上搭建WordPress博客
笔者一直很羡慕那些搭建了个人博客的大牛,在最近工作之余也尝试着搭建了自己的博客,历时1周,这篇文章就将踩过的坑记录下来,先看下成果,链接在此 1- 购买腾讯云主机 腾讯云官网,我选了79元/月的最便宜 ...
- 如何定制 Calico 网络 Policy - 每天5分钟玩转 Docker 容器技术(70)
Calico 默认的 policy 规则是:容器只能与同一个 calico 网络中的容器通信.本节讨论如何定制 policy. calico 能够让用户定义灵活的 policy 规则,精细化控制进出容 ...
- Python学习笔记006_异常_else_with
>>> # try-except语句 >>> >>> # try : >>> # 检测范围 >>> # exc ...
- Socket类 以及 ServerSocket类 讲解
Socket类 套接字是网络连接的端点,套接字使应用可以从网络中读取数据,可以向网络中写入数据.不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到相互通信的目的. 为了从一个应用程序向另 ...
- Dodobox一个基于所有平台的嵌入式操作系统(OS)
DodoBox是为广大应用开始者提供的一个跨平台应用发布平台.它提供了客户端和服务器端的SDK及详细的开发者帮助文件,帮助开发者创建.移植软件应用或游戏应用. DodoBox基于OpenGL技术,提供 ...
- 利用angularJs自定义指令(directive)实现在页面某一部分内滑块随着滚动条上下滑动
最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...
- 庞玉栋:浅谈seo优化对于网站建设的重要性
根据最近做SEO优化经验而写 写的也都是我的方法 大神勿喷 SEO:英文Search Engine Optimization缩写而来, 中文意译为搜索引擎优化 如果你连个网站都没有那就点这里:如何拥 ...
- GCD之死锁体会
1.先看下几句代码 1 2 3 4 5 6 7 dispatch_queue_t serialqueue=dispatch_queue_create("serialqueue", ...
- Angularjs –– Expressions(表达式)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Angular的表达式 Angular的表达式和JavaScript代码很像,不过通常A ...
- 深入理解计算机系统chapter7
链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...