猿问

在给定数组中查找键数组(模式)

我现在在这个问题上工作了 2 小时,但我的思绪停止了工作。而我无处可去。有人可以帮忙吗?


问题是将 Key 数组中的确切模式匹配到另一个数组。例如:


Key = {3, 8, 6}

Target = {3, 6, 8, 8, 6, 3, 8, 6, 2, 4}

这里的答案是找到这些的索引,这将是:


{5, 6, 7}


红颜莎娜
浏览 117回答 3
3回答

慕的地8271018

这段代码解决了问题:&nbsp; &nbsp; &nbsp; &nbsp; int[] key = new int[]{3, 8, 6};&nbsp; &nbsp; &nbsp; &nbsp; int[] target = new int[]{3, 6, 8, 8, 6, 3, 8, 6, 2, 4};&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < target.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = 0; j < key.length && (i + j) < target.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (target[i + j] != key[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == key.length && j != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("{");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = 0; j < key.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i + j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j != key.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(", ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

HUX布斯

&nbsp; HashMap<Integer, Integer> maps = new HashMap<>();&nbsp; &nbsp; IntStream.range(0, target.length).forEach(i -> {&nbsp; &nbsp; &nbsp; &nbsp; maps.put(target[i], i);&nbsp; &nbsp; });&nbsp; &nbsp; Arrays.stream(src).forEach(i -> {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(maps.get(i));&nbsp; &nbsp; });首先计算索引,它喜欢group by,但只选择最后一个。最后我们可以很容易地从这个Hash 中得到索引。

慕容3067478

您应该遍历“目标”数组中的所有元素,并在“键”数组中的当前匹配模式之后保留一些索引,我们将此索引称为“keyIndex”。每次“目标”数组中的元素等于“键”数组中“keyIndex”位置的元素时,您增加 keyIndex(当前匹配模式更大)并添加到一些数据结构(我选择列表) 来自“目标”数组的索引,其中元素相等。如果元素不相等,您应该重置“keyIdnex”(当前匹配模式的长度为零)并清除列表。我相信这应该对你有好处:&nbsp; &nbsp; &nbsp; &nbsp; public static List<Integer> findPattern(int[] key , int[] target){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<Integer> result = new ArrayList<Integer>(); //This list hold the indexes of the patter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int keyIndex = 0; //The index to follow after the "key" array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0 ; i < target.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(target[i] == key[keyIndex]){ //This "key" element is equal to the element from the "target"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(i); //Add the index in which the elements are equal.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyIndex++; //The currently match pattern is larger, increment "keyIndex"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(result.size() == key.length) //The all pattern is checked and match, return the list which store the indexes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{ //The pattern is not match anymore, reset all the data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null; //The pattern from "key" not found in "target" ,return null&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答