当前位置:网站首页>2022.5.27-----leetcode. Interview 17.11
2022.5.27-----leetcode. Interview 17.11
2022-06-10 04:34:00 【Lu 727】
public int findClosest(String[] words, String word1, String word2) {
List<Integer>[] l=new ArrayList[2];// Store target word location
l[0]=new ArrayList<>();
l[1]=new ArrayList<>();
for(int i=0;i<words.length;i++){
if(words[i].equals(word1))
l[0].add(i);
if(words[i].equals(word2))
l[1].add(i);
}
int min=100000;
int a=0,b=0;
// Double pointer traversal , Move the left pointer to the right each time , Reduce distance
while(a<l[0].size()&&b<l[1].size()){
if(l[0].get(a)>l[1].get(b)){
min=Math.min(min,l[0].get(a)-l[1].get(b));
b++;
}else if(l[0].get(a)<l[1].get(b)){
min=Math.min(min,l[1].get(b)-l[0].get(a));
a++;
}else return 0;
}
return min;
}边栏推荐
- midway的使用教程
- [understanding of opportunity -22]: Guiguzi - the art of closing Tibet. Collect your talents in time to protect yourself in the workplace, business and business activities
- - Oui. Net C # Foundation (7): Interface - Comment les gens interagissent avec les chats
- Execution strategy of application software efficiency test
- Log management of MySQL database
- [depth first search] maximum product: arrangement
- Eight part essay ceiling! (PDF HD download)
- Golang learning 4: closures and defers
- Acl2022 | the introduction of comparative learning to add negative samples to the generation process enables the model to effectively learn knowledge at different levels
- 信息学奥赛一本通 1274:【例9.18】合并石子 | 洛谷 P1775 石子合并(弱化版)
猜你喜欢

Crack the five myths of programmers, and new programmer 004 is officially launched!

FastApi-16-页面美化-1

Distributed data object: HyperTerminal 'global variable'

5 minutes to learn how to set up local pypi source

Fastapi-14-file upload-2

Why do SQL statements use indexes but still slow queries?

S系列·删除文件夹的几种姿势
![[graduation project 2] intelligent range hood system based on stm32](/img/b0/2ab9f22c939198f3cd5fcf11d87998.png)
[graduation project 2] intelligent range hood system based on stm32

Using kubekey to build kubernetes/kubesphere environment
![[joint search set] sympodial plants (number of connected blocks)](/img/47/0dd7c64568a2176640dcab835228c6.png)
[joint search set] sympodial plants (number of connected blocks)
随机推荐
CSDN audio and video technology developers' online survey
Golang learning 6: file operation in
TCP (sliding window, flow control)
Exemple de démarrage JDBC
Huawei, this is too strong
Basic methods of stack and related problems
[in depth study of 4g/5g/6g topic -30]: 5g NR startup process 5.1 - connection between the base station and the core network (ng setup request, ng setup response) and message details.
Two modes of epoll and epoll reactor
golang学习之六:中的文件操作
Metersphere | a super easy-to-use open source testing platform
[depth first search] toy snake: maze problem
Perform tests
Who ate IO?
Webcodecs解析GIF图
Manage global variables (2)
Application of PhD debate 𞓜 self supervised learning in Recommendation System
Cross in tensorflow_ entropy
QT cannot connect to local MySQL
[in depth study of 4g/5g/6g topic -31]: 5g NR startup process 5.2 - ue attach process (registration request/accept/complete)
MindSpore【初学入门】教程在线运行时报错