243. Shortest Word Distance 最短的单词index之差
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道用什么数据结构指定index
[一句话思路]:
指定index只需要设成-1然后遍历就行
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 头一次见:必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
字符串判断相等用的是equals,你没记错
[关键模板化代码]:
[其他解法]:
[Follow Up]:
244. Shortest Word Distance II 多次调用:hashmap多次取
3:可重复:用数学
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
//cc
if (words == null || words.length == 0) {
return 0;
}
//ini
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE;
//for loop, renew p1 p2, compare
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
p1 = i;
}
if (words[i].equals(word2)) {
p2 = i;
}
if (p1 != -1 && p2 != -1) {
min = Math.min(min, Math.abs(p2 - p1));
}
}
//return
return min;
}
}
243. Shortest Word Distance 最短的单词index之差的更多相关文章
- [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 ...
- LeetCode 243. 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
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...
- 【LeetCode】243. Shortest Word Distance 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LC] 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 ...
随机推荐
- (八)java运算符
算数运算符 + - * / % ++ -- class Ysf { public static void main(String[] args) { System.out.println(5/2);/ ...
- python学习之aop装饰模式
实际开发过程当中可能要对某些方法或者流程做出改进,添加监控,添加日志记录等所以我们要去改动已有的代码,自己的或者别人的,但改动后测试不周会引发不可控的异常,aop 模式解决了这类问题引发重复代码大量积 ...
- 【转】IUSR和IIS_IUSRS
转自:http://blog.chinaunix.net/uid-20344928-id-3306130.html 概述 在早期的IIS版本中,随着IIS的安装,系统会创建一个IUSR_Mac ...
- LeetCode Split Concatenated Strings
原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/ 题目: Given a list of st ...
- c#模拟键盘输入
System.Windows.Forms.SendKeys.SendWait("j");
- windows中文编码报错 com.google.gson.JsonIOException: java.nio.charset.MalformedInputException: Input length = 1
昨天碰到一个问题:同一个请求页面,页面经过匹配后调用http的post协议接口,部署在linux环境的没问题,本地Eclipse启动的tomcat也没问题,直接启动本地tomcat却报错了: 18:4 ...
- 解决Maven提示:Could not read settings.xml, assuming default values
本文转载自:http://blog.csdn.net/hqocshheqing/article/details/47702049 最近在学习Maven 时总是出现 Could not read se ...
- 网络编程Socket之wireshark使用
这里只对wireshark进行简单的使用介绍.里面的报表,IO图形分析等以后再谈.... 这里不提供下载地址了,不是什么稀有资源,我使用的wireshark是在百度软件中心下载的. 第一步:选择需要 ...
- codeforces 985E Pencils and Boxes(dp+思维)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- WIN10运行软件,窗口不显示(移动到屏幕外无法复原)的解决办法 Lebal:bug10解决方案
双显示器切换回单显示器的时候,可能会遇到窗口移动到屏幕外不显示的情况 像这样虽然有缩略图但是点击无反应,并且平铺窗口也不管用,这个时候单击该窗口,Alt+space 执行最小化以及最大化操作即可复原