示例:
WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList(""," quick"," brown"," fox", "快速&#34));
断言(finder.distance(" fox","")== 3);
断言(finder.distance("快速","狐狸")== 1);
我有以下解决方案,似乎是O(n),但我不确定是否有更好的解决方案。有没有人有任何想法?
String targetString = "fox";
String targetString2 = "the";
double minDistance = Double.POSITIVE_INFINITY;
for(int x = 0; x < strings.length; x++){
if(strings[x].equals(targetString)){
for(int y = x; y < strings.length; y++){
if(strings[y].equals(targetString2))
if(minDistance > (y - x))
minDistance = y - x;
}
for(int y = x; y >=0; y--){
if(strings[y].equals(targetString2))
if(minDistance > (x - y))
minDistance = x - y;
}
}
}
答案 0 :(得分:14)
您的解决方案是O(N^2)
,因为您在查找每个单词时遍历整个列表。
首先,您找到第一个单词,然后再遍历整个列表以找到第二个单词。
你可以做的是使用两个变量来跟踪每个单词的位置,并通过list =&gt;计算单次通过的距离O(N)
。
int index1 = -1;
int index2 = -1;
int minDistance = Integer.MAX_VALUE;
int tempDistance = 0;
for (int x = 0; x < strings.length; x++) {
if (strings[x].equals(targetString)) {
index1 = x;
}
if (strings[x].equals(targetString2)) {
index2 = x;
}
if (index1 != -1 && index2 != -1) { // both words have to be found
tempDistance = (int) Math.abs(index2 - index1);
if (tempDistance < minDistance) {
minDistance = tempDistance;
}
}
}
System.out.println("Distance:\t" + minDistance);
答案 1 :(得分:3)
Value:
1
Value2:
2
Vlaue3:
4
Value4:
7
答案 2 :(得分:0)
import java.util.*;
public class WordDistance {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String s1=in.nextLine();
String s2=in.nextLine();
int index1=-1;
int index2=-1;
boolean flag1=false;
boolean flag2=false;
int distance=Integer.MAX_VALUE;
int answer=Integer.MAX_VALUE;
String[] sarray=s.split(" ");
for(int i=0;i<sarray.length;i++)
{
if(!s1.equals(s2))
{
flag1=false; flag2=false;
}
if(s1.equals(sarray[i]) && flag1==false)
{
index1=i;
flag1=true;
}
else
if(s2.equals(sarray[i]) && flag2==false)
{
index2=i;
flag2=true;
}
if(index1!=-1 && index2!=-1)
{
distance=Math.abs(index1-index2);
flag1=false; flag2=false;
}
if(distance<answer)
{
answer=distance;
}
}
if(answer==Integer.MAX_VALUE)
{
System.out.print("Words not found");
}
else
{
System.out.print(answer);
}
}
}
//**Test Case 1**: the quick the brown quick brown the frog (frog brown) **O/P 2**
//**Test Case 2**: the quick the brown quick brown the frog (brown brown) **O/P 2**
//**Test Case 3**: the brown qucik frog quick the (the quick) **O/P 1**
答案 3 :(得分:0)
找到给定段落中两个单词之间最小距离的更好,更简单的解决方案。
String[] strArray = {"the","quick","brown","fox","quick"};
String str1 = "quick";
String str2 = "fox";
int i,startIndex=0,minDistnace=100;
for( i=0;i<strArray.length;i++){
if(strArray[i].equals(str1)||strArray[i].equals(str2)){
startIndex = i; //get the first occurence of either word
break;
}
}
for(;i<strArray.length;i++){
if(strArray[i].equals(str1)||strArray[i].equals(str2)){
//compare every word from that first occurence
// if words not same and distance less than minimun distance then update
if(!strArray[i].equals(strArray[startIndex]) && (i-startIndex)<minDistance){
minDistance = i-startIndex;
startIndex =i;
}
else{
startIndex =i;
}
}
}
System.out.println(minDistance);
时间复杂度:O(n)
空间复杂性:O(1)
答案 4 :(得分:0)
只需1次迭代。 非常简单的解决方案 假设:str1和str2不为null。 str1不等于str2。 strs不包含null;
private static int findShortestDistance(String str1, String str2, String[] strs) {
int distance = Integer.MAX_VALUE;
String temp = null;
int index = 0;
for(int i=0; i<strs.length; ++i) {
if(str1.equals(strs[i]) || str2.equals(strs[i])) {
if(temp != null && !strs[i].equals(temp)) {
distance = Math.min(distance, i - index);
}
temp = strs[i];
index = i;
}
}
return distance;
}