[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 ...
随机推荐
- 使用deb 打包开发的postgres extension 另外一种方法
已经写过一个deb 包打包的方法,我们同时也可以使用dpkg-deb 命令 安装依赖工具包 推荐安装全点的 sudo apt-get install build-essential autoconf ...
- IETF和W3C的区别
国际互联网协会ISOC和万维网联盟W3C是互联网领域内两大国际协会组织,ISOC旗下IETF机构与W3C是互联网行业内两大标准组织.不可否认「IETF和W3C都是优秀的组织,它们从事着一种使事情按程序 ...
- day50 django第一天 自定义框架
主要内容: 1.http协议 2.web框架 3.Django 1.http协议 1.1 http协议的简介 超文本传输协议(英文:Hyper Text Transfer Protocol,HTTP) ...
- centos查看系统版本信息
1.查看版本文件名称 ll /etc/*centos* 2.显示系统版本号 cat /etc/centos-release
- 利用STM32CubeMX来生成USB_HID_Mouse工程【添加ADC】(2)【非dma和中断方式】
上回讲到怎么采集一路的adc的数据,这次我们来采集两路的数据. 现在直接修改原先的代码 /* Private variables ----------------------------------- ...
- “永恒之蓝”(Wannacry)蠕虫全球肆虐 安装补丁的方法
“永恒之蓝”利用0day漏洞 ,通过445端口(文件共享)在内网进行蠕虫式感染传播,没有安装安全软件或及时更新系统补丁的其他内网用户就极有可能被动感染,所以目前感染用户主要集中在企业.高校等内网环境下 ...
- 【亲测显式等待】Selenium:元素等待的4种方法
Selenium:元素等待的4种方法 1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用. 2.隐式等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉We ...
- Python 快速排序 算法
基本的快排算法,二分法 #!/usr/bin/env python # encoding: utf-8 l1=[1,4,2,6,3] def path_sort(l,start_index,end_i ...
- python3之time、datetime、random
UTC:协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 中国属于东八区,领先世界时间8小时 time模块 time.ti ...
- suricata HTTP关键字
http request http request请求包括请求行.请求头.空行和内容.一个普通的request请求如下: http response http response应答包括应答行,头部,空 ...