[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.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1
题意:
给定一列单词和俩单词,求这俩单词在一列中的最短距离。
Solution1: Two Pointers
扫words,若当前指针i 对应元素等于word1, 则将 i 赋给 a
若当前指针i 对应元素等于word2, 则将 i 赋给 b
同时更新result
code
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int result = Integer.MAX_VALUE;
int a = -1; // word1Idx
int b = -1; // word2Idx
for(int i = 0; i< words.length; i++){
if(words[i].equals(word1)){
a = i;
}else if(words[i].equals(word2)){
b = i;
}
if(a!=-1 && b !=-1){
result = Math.min(result, Math.abs(a-b));
}
}
return result;
}
}
[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] 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 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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 ...
随机推荐
- Excel转datatable
如果想支持 .xls,.xlsx 两种格式 则必须安装一个exe文件,下载路径https://www.microsoft.com/zh-CN/download/details.aspx?id=1325 ...
- Vue 中使用 viewerjs
安装 viewerjs npm install viewerjs --save 创建一个 Viewer.vue 组件 <template> <div id="index&q ...
- [转]C#程序性能优化
C#程序性能优化 1.显式注册的EvenHandler要显式注销以避免内存泄漏 将一个成员方法注册到某个对象的事件会造成后者持有前者的引用.在事件注销之前,前者不会被垃圾回收. private v ...
- 读取 exe dll 自定义config 文件
ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = GPARAM._configF ...
- springboot+mockito 异常解决方案
系统启动的异常日志如下 javax.net.ssl.* java.lang.IllegalStateException: Failed to load ApplicationContext at or ...
- Django项目的创建
一. Django介绍 Python的WEB框架有Django.Tornado.Flask 等多种, Django相较与其他WEB框架其优势为: 大而全, 框架本身集成了ORM.模型绑定,.模板引擎, ...
- c# 字典
字典 在System.Collections.Generic下, 对应HashTable,添加了泛型的特性,性能更高跟安全 在内存中是散列排布的,存储也是键值对 Dictionary<键的数据类 ...
- Deployment Descriptor Web.xml
Deployment Descriptor部署描述符: - 部署描述符是要部署到Web容器或EJB容器的Web应用程序或EJB应用程序的配置文件. - 部署描述符应包含EJB应用程序中所有企业bean ...
- PHP中empty、isset和is_null的具体区别?
1.isset()用来检测一个变量是否已声明; 2.empty()用来检测一个变量是否为空如果有如下情况返回真值: 1)空字符串 2)false 3)空数组 4)NULL 5)0 6)0.0 7)un ...
- 关于报错:There is already 'xxxController' bean method的解决方法
报这个错的原因是因为你controller里的@RequestMapping中的路径有重复! 如: