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 ...
随机推荐
- 如何在公司Http代理后使用NuGet官方源
文章转自CSDN 霍力强的专栏 有些公司上网使用的是Http代理.默认情况下,VS是无法访问外部网络的.如果要使用NuGet,通常只能在局域网里架一个自己的NuGet服务器.但这种方法不论是packa ...
- Python解析excel文件并存入sqlite数据库
最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...
- sapconnector.dll download
Sapnoc30Demo_Yan.rar Sap30Server.rar SAPNCO_Sample_Code.zip sapcon3.0_X64.rar DbEntry.Net.4.1.Setup. ...
- 使用appassembler-maven-plugin插件生成启动脚本
appassembler-maven-plugin可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序. 首先pom引入相关依赖 <build> < ...
- bzoj 2119 股市的预测 —— 枚举关键点+后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2119 思路就是对于这个形如 ABA 的串,枚举 A 的长度,并按照长度分出几块,找到一些关键 ...
- CAN总线过载帧
过载帧 过载帧与主动错误帧具有相同的格式.但是,过载帧只能在帧间间隔产生,因此可通过这种方式区分过载帧和错误帧(错误帧是在帧传输时发出的).过载帧由两个字段组成,即过载标志和随后的过载定界符.过载标志 ...
- 事务之四:Spring事务--原理
一.Spring事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤进行: ...
- phantomjs 安装和试用
准备学习casperjs, 发现官网上说 it’s an extremely useful companion to PhantomJS, 所以决定下把它下来试试.下载安装(win7)没什么可说的, ...
- __CLASS__
<?php class base_class { function say_a() { echo "'a' - said the " . __CLASS__ . " ...
- MVC中Ajax post 和Ajax Get——提交对象Model
HTTP 请求:GET vs. POST两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST.GET - 从指定的资源请求数据POST - 向指定的资源提交要处理的数据GET 基本上 ...