我现在正在解决这个问题 2 小时,我的大脑停止工作了。我无处可去。有人可以帮忙吗?
问题是将 Key 数组的确切模式匹配到另一个数组。 例如:
Key = {3, 8, 6}
Target = {3, 6, 8, 8, 6, 3, 8, 6, 2, 4}
这里的答案是找到这些索引的索引,即:
{5, 6, 7}
这段代码解决了问题:
int[] key = new int[]{3, 8, 6};
int[] target = new int[]{3, 6, 8, 8, 6, 3, 8, 6, 2, 4};
for (int i = 0; i < target.length; i++) {
int j = 0;
for (j = 0; j < key.length && (i + j) < target.length; j++) {
if (target[i + j] != key[j]) {
break;
}
}
if (j == key.length && j != 0) {
System.out.print("{");
for (j = 0; j < key.length; j++) {
System.out.print(i + j);
if (j != key.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
}
HashMap<Integer, Integer> maps = new HashMap<>();
IntStream.range(0, target.length).forEach(i -> {
maps.put(target[i], i);
});
Arrays.stream(src).forEach(i -> {
System.out.println(maps.get(i));
});
首先计算索引,它喜欢分组依据,但只选择最后。 最后,我们可以轻松地从这个哈希中获取索引。
char[] key = {3, 8, 6};
char[] target = {3, 6, 8, 8, 6, 3, 8, 6, 2, 4};
String keyStr = new String( key );
String targetStr = new String( target );
int start = targetStr.indexOf( keyStr );
int[] resultArr = new int[key.length];
int x = 0;
for(int i = start; i< start + key.length; i++)
{
resultArr[ x] = i;
x++;
}
System.out.println( Arrays.toString( resultArr ));
如果要多次匹配,请使用:
char[] key = {3, 8, 6};
char[] target = {3, 6, 8, 8, 6, 3, 8, 6, 2, 4, 3, 8, 6};
String keyStr = new String( key );
String targetStr = new String( target );
Pattern pattern = Pattern.compile( keyStr );
Matcher matcher = pattern.matcher( targetStr );
// Check all occurrences
while( matcher.find() )
{
System.out.print( "Start index: " + matcher.start() );
System.out.print( " End index: " + matcher.end() );
int start = matcher.start();
int[] resultArr = new int[key.length];
int x = 0;
for( int i = start; i < start + key.length; i++ )
{
resultArr[x] = i;
x++;
}
System.out.println( Arrays.toString( resultArr ) );
}
您可以使用键数组的长度创建目标数组的子数组,并将每个子数组与键进行比较:
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class OBJ {
public static void main(String[] args){
int[] key = {3, 8, 6};
int[] target = {3, 6, 8, 8, 6, 3, 8, 6, 2, 4};
for(int i = 0; i < target.length - key.length; i++){
int[] temp = Arrays.copyOfRange(target, i, i+key.length);
if(Arrays.equals(key,temp)){
String indices = IntStream.range(i, i+key.length).mapToObj(e->String.valueOf(e)).collect(Collectors.joining(","));
System.out.println(indices);
}
}
}
}
您应该遍历"目标"数组中的所有元素,并在"key"数组的当前匹配模式之后保留一些索引,我们将此索引称为"keyIndex"。每次"目标"数组中的元素等于"key"数组中"keyIndex"位置的元素时,您都会递增keyIndex(当前匹配模式更大(,并将元素相等的"目标"数组中的索引添加到某些数据结构(我选择列表(中。如果元素不相等,则应重置"keyIdnex"(当前匹配模式的长度为零(并清除列表。
我相信这对你有好处:
public static List<Integer> findPattern(int[] key , int[] target){
List<Integer> result = new ArrayList<Integer>(); //This list hold the indexes of the patter
int keyIndex = 0; //The index to follow after the "key" array
for(int i = 0 ; i < target.length; i++){
if(target[i] == key[keyIndex]){ //This "key" element is equal to the element from the "target"
result.add(i); //Add the index in which the elements are equal.
keyIndex++; //The currently match pattern is larger, increment "keyIndex"
if(result.size() == key.length) //The all pattern is checked and match, return the list which store the indexes
return result;
}else{ //The pattern is not match anymore, reset all the data
keyIndex = 0;
i--;
result.clear();
}
}
return null; //The pattern from "key" not found in "target" ,return null
}
试试这个,
public static void main(String args[]) {
int[] a = { 1, 5, 7, 3, 6, 10, 9, 8, 3, 6, 7, 10, 9, 8 };
int[] key = { 3, 6, 10 };
List<Integer> pos = new ArrayList<Integer>();
for (int i = 0; i <= a.length - key.length; i++) {
pos = getPosition(i, a, key);
if (pos != null)
System.err.println(pos);
}
}
private static List<Integer> getPosition(int i, int[] a, int[] key) {
int count = 0;
List<Integer> p = new ArrayList<Integer>();
for (int j = 0; i < a.length && j < key.length; i++, j++) {
if (a[i] == key[j]) {
count++;
p.add(i);
}
}
return count == key.length ? p : null;
}