Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

243. Shortest Word Distance #110

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

public class Solution { public int shortestDistance(String[] words, String word1, String word2) { int distance = words.length; int index1 = -1, index2 = -1; for(int i = 0; i < words.length; i++) { if(words[i].equals(word1)) { index1 = i; if(index2 != -1) distance = Math.min(distance, index1-index2); } if(words[i].equals(word2)) { index2 = i; if(index1 != -1) distance = Math.min(distance, index2-index1); } } return distance; } }

Shawngbk commented 7 years ago

Linkedin