From ef4025102fc719e359150611b600442def1d46ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Fri, 27 May 2022 10:02:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2017.11:=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/FindClosestLcci.java | 62 +++++++++++++++++++ .../editor/cn/doc/content/FindClosestLcci.md | 14 +++++ 2 files changed, 76 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FindClosestLcci.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/FindClosestLcci.md diff --git a/src/main/java/leetcode/editor/cn/FindClosestLcci.java b/src/main/java/leetcode/editor/cn/FindClosestLcci.java new file mode 100644 index 0000000..607ce94 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FindClosestLcci.java @@ -0,0 +1,62 @@ +//

有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?

+// +//

示例:

+// +//
+//输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
+//输出:1
+// +//

提示:

+// +// +//
Related Topics
  • 数组
  • 字符串

  • 👍 50
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +// 面试题 17.11:单词距离 +public class FindClosestLcci { + public static void main(String[] args) { + Solution solution = new FindClosestLcci().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int findClosest(String[] words, String word1, String word2) { + List list1 = new ArrayList<>(); + List list2 = new ArrayList<>(); + for (int i = 0; i < words.length; i++) { + if (word1.equals(words[i])) { + list1.add(i); + } + if (word2.equals(words[i])) { + list2.add(i); + } + } + int index1 = 0; + int index2 = 0; + int min = words.length; + while (index1 < list1.size() && index2 < list2.size()) { + int num1 = list1.get(index1); + int num2 = list2.get(index2); + if (num1 < num2) { + min = Math.min(min, num2 - num1); + index1++; + } else { + min = Math.min(min, num1 - num2); + index2++; + } + if (min == 1) { + return 1; + } + } + return min; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/FindClosestLcci.md b/src/main/java/leetcode/editor/cn/doc/content/FindClosestLcci.md new file mode 100644 index 0000000..743aea5 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/FindClosestLcci.md @@ -0,0 +1,14 @@ +

    有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?

    + +

    示例:

    + +
    +输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
    +输出:1
    + +

    提示:

    + +
      +
    • words.length <= 100000
    • +
    +
    Related Topics
  • 数组
  • 字符串

  • 👍 50
  • 👎 0
  • \ No newline at end of file