[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 ...
随机推荐
- redis使用get key中文变成十六进制编码
redis-cli 后面加上 --raw 解决中文显示问题 redis-cli -h 127.0.0.1 -p 端口 -a 密码 --raw 不带 --raw 参数: redis-cli -h &g ...
- excel 表格粘贴到word 显示不完整
左上角,十字点,右键,取消固定宽度即可:
- 添加一个pv到vg后,误删新加入的pv,报 pv * not found or rejected by a filter
问题如下 将某一pv加入vg vgextend cl /dev/xvdb1 然后进入fdisk将xvdb1分区删掉,重新创建pv 使用lvdisplay报警告 [root@localhost ~]# ...
- java 获取键盘输入常用的两种方法
java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...
- DevExpress GridView 整理(转)
DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...
- git初始化本地项目及关联github远程库
一.初始化本地项目 idea中在项目文件夹下执行:git init . 二.在github官网上创建一个库 三.执行如下命令关联远程库: git remote add origin 你创建的git远程 ...
- 黄聪:通过 itms:services://? 在线安装ipa ,跨过appstore
1.需要一个html文件,引导下载用户在线安装ipa <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&quo ...
- Docker容器常用命令
容器是镜像的一个运行实例.两者不同的是,镜像是静态的只读文件,而容器带有运行时需要的可写文件层. 一.创建容器 1.新建容器 docker create:新建一个容器 create命令命令支持的选项十 ...
- 小程序客服下发消息禁止后 session from 还有用吗?
文章概要 1. 小程序下发政策调整分析 2. session from 数据还传到底三方了没? 1. 小程序下发政策调整分析 小程序客服功能下发策略调整 ...
- 9-安装redis
1.在linux上安装C语言环境 yum install gcc-c++ 2.解压源码包 tar -xvf /opt/soft/redis-3.0.0.tar -C /opt/app/ 3.编译源码( ...