243 Shortest Word Distance – Easy
Problem:
Thoughts:
Solutions:
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
Integer i1 = null;
Integer i2 = null;
int min = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i ++) {
if (words[i].equals(word1)) {
i1 = i;
}
if (words[i].equals(word2)) {
i2 = i;
}
if (i1 != null && i2 != null) {
min = Math.min(min, Math.abs(i1 - i2));
}
}
return min;
}
}Last updated