243.最短单词距离

时游小于 1 分钟LeetCode

243.最短单词距离

/*
 * @lc app=leetcode.cn id=228 lang=typescript
 *
 * [243] 最短单词距离
 */

// @lc code=start
function shortestDistance(
	wordsDict: string[],
	word1: string,
	word2: string
): number {
	let firstIndex: number[] = [];
	let secondIndex: number[] = [];
	wordsDict.map((el, index) => {
		if (el == word1) firstIndex.push(index);
		if (el == word2) secondIndex.push(index);
	});

	let nums: number[] = [];
	firstIndex.map(el => {
		secondIndex.map(cl => {
			nums.push(Math.abs(el - cl));
		});
	});

	return Math.min(...nums);
}

// @lc code=end

上次编辑于:
贡献者: 15327360835
Loading...