gpt4 book ai didi

java - 如何在时间复杂度方面改进算法?

转载 作者:搜寻专家 更新时间:2023-11-01 03:57:09 25 4
gpt4 key购买 nike

我正在尝试测量我的算法的时间复杂度:

public boolean rotateAndCompare(int[] fst, int[] snd) {
int len = fst.length;
for (int k = 0; k < len; k++) {
for (int j = 0; j < len; j++) {
if (fst[(k + j) % len] != snd[j]) {
break;
}
if (j == len - 1) {
return true;
}
}
}
return false;
}

我假设它具有 O(n*n) 复杂性,因为我们遍历一个数组,然后遍历另一个数组。我对吗?如果是这样,我该如何改进它?

最佳答案

如果我理解正确,你的算法会决定 snd 的第一个 fst.length 整数是否等于 fst,可能是旋转的。它假定 snd.length >= fst.length。如果这不是您的意思,请在问题中说明。

但假设这就是您的真正意思,您可以使用像 KMP 这样的字符串匹配算法在 O(n) 中解决这个问题。 .换句话说,您需要查看是否可以找到 snd 作为 fst + fst 的子数组,这是一个经典问题。

这是 Java 中的示例实现:

import java.util.Arrays;

public class Main {
public static class KMP {
private final int F[];
private final int[] needle;

public KMP(int[] needle) {
this.needle = needle;
this.F = new int[needle.length + 1];

F[0] = 0;
F[1] = 0;
int i = 1, j = 0;
while (i < needle.length) {
if (needle[i] == needle[j])
F[++i] = ++j;
else if (j == 0)
F[++i] = 0;
else
j = F[j];
}
}

public int find(int[] haystack) {
int i = 0, j = 0;
int n = haystack.length, m = needle.length;

while (i - j <= n - m) {
while (j < m) {
if (needle[j] == haystack[i]) {
i++;
j++;
} else break;
}
if (j == m) return i;
else if (j == 0) i++;
j = F[j];
}
return -1;
}
}

public static boolean rotateAndCompare(int[] fst, int[] snd) {
int[] fst2 = new int[fst.length * 2];
System.arraycopy(fst, 0, fst2, 0, fst.length);
System.arraycopy(fst, 0, fst2, fst.length, fst.length);

int[] snd2 = Arrays.copyOf(snd, fst.length);
return new KMP(snd2).find(fst2) >= 0;
}

public static void main(String[] args) {
System.out.println(rotateAndCompare(new int[]{1, 2, 3}, new int[]{3, 1, 2, 4}));
System.out.println(rotateAndCompare(new int[]{1, 2, 2}, new int[]{3, 1, 2, 4}));
}
}

关于java - 如何在时间复杂度方面改进算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com