很抱歉,您没有提供需要改写的句子。请提供您希望改写的句子,我将为您改写为一个长尾词的。
- 内容介绍
- 文章标签
- 相关推荐
本文共计297个文字,预计阅读时间需要2分钟。
题目:给定一个包含单字的超长文本文件,任意指定两个不同的单字,找出这两个单字在该文件中最短的距离(间隔单数)。如果搜索过程中这两个单字在该文件中会重复出现,每次都寻找不同的单字。
题目:
有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?
示例:
输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
输出:1
代码实现:
class Solution { public int findClosest(String[] words, String word1, String word2) { int length = words.length; int ans = length; int index1 = -1, index2 = -1; for (int i = 0; i < length; i++) { String word = words[i]; if (word.equals(word1)) { index1 = i; } else if (word.equals(word2)) { index2 = i; } if (index1 >= 0 && index2 >= 0) { ans = Math.min(ans, Math.abs(index1 - index2)); } } return ans; }}本文共计297个文字,预计阅读时间需要2分钟。
题目:给定一个包含单字的超长文本文件,任意指定两个不同的单字,找出这两个单字在该文件中最短的距离(间隔单数)。如果搜索过程中这两个单字在该文件中会重复出现,每次都寻找不同的单字。
题目:
有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?
示例:
输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
输出:1
代码实现:
class Solution { public int findClosest(String[] words, String word1, String word2) { int length = words.length; int ans = length; int index1 = -1, index2 = -1; for (int i = 0; i < length; i++) { String word = words[i]; if (word.equals(word1)) { index1 = i; } else if (word.equals(word2)) { index2 = i; } if (index1 >= 0 && index2 >= 0) { ans = Math.min(ans, Math.abs(index1 - index2)); } } return ans; }}
