Any special point of having a String array in this problem? I mean int array would do the same


  • 0
    V

    Any special point of having a String array in this problem? I mean int array would do the same

    This is my code

    public int shortestDistance(String[] words, String word1, String word2) {
            int pointerWord1 = Integer.MAX_VALUE / 2, pointerWord2 = Integer.MIN_VALUE / 2, min = Integer.MAX_VALUE;
            for (int i = 0; i < words.length; i++) {
                if (words[i].equals(word1)) pointerWord1 = i;
                else if (words[i].equals(word2)) pointerWord2 = i;
                min = Math.min(min, Math.abs(pointerWord1 - pointerWord2));
            }
            return min;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.