我已经在这上面阅读了一些其他堆栈溢出线程:
public static int[] intersection (int [] x, int numELementsInX, int [] y, int numElementsInY) {
Run Code Online (Sandbox Code Playgroud)
我试图检查两个数组以及它们的元素数(numElementsInX和numElementsInY),并返回一个包含数组x和y的公共值的新数组.他们的交集.
Example,if x is{1,3,5,7,9}and y is{9,3,9,4} then
intersection(x, 5, y, 4} should return {3, 9} or {9, 3}
Run Code Online (Sandbox Code Playgroud)
我读过我需要使用LCS算法.谁能给我一个如何做到这一点的例子?数组中的数组和值都被初始化并在另一个方法中生成,然后传递到交集中.
任何帮助/澄清表示赞赏.
编辑代码
for (int i=0; i<numElementsInX; i++){
for (int j=0; j<numElementsInY; j++){
if (x[j]==x[i]) { //how to push to new array?;
}
else{
}
}
}
Run Code Online (Sandbox Code Playgroud) 有没有有效的方法在Go中获得两个切片的交集?
我想避免嵌套for循环解决方案slice1 := []string{"foo", "bar","hello"}
slice2 := []string{"foo", "bar"}
intersection(slice1, slice2)
=> ["foo", "bar"]
Run Code Online (Sandbox Code Playgroud)
字符串的顺序无关紧要