我编写了一个程序,从第二个字符串中删除第一个字符串中存在的字符。复杂度为BigO(n ^ 2)。是否可以进一步降低复杂度?
public class Tmp {
public static void main(String[] args) {
String s = "halloween";
String s1 = "halcyon";
char[] ss = s.toCharArray();
char[] ss1 = s1.toCharArray();
for(int i=0;i<ss.length;i++){
for(int j=0;j<ss1.length;j++){
if(ss1[j] == ss[i]){
ss1[j] = 'x'; //Replace the common char with x
}
}
}
System.out.println(Arrays.toString(ss1));
}
}
输出
[x, x, x, c, y, x, x]
总时间复杂度= O(N)
在这里,您需要额外的空间来存储MAP。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句