在给定的数组中找到一个键数组(一种模式)

时间:2018-09-10 06:39:47

标签: java

我正在处理此问题2个小时,而我的思想却停止了工作。我什么也没有。有人可以帮忙吗?

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

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

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

{5, 6, 7}

6 个答案:

答案 0 :(得分:0)

此代码解决了问题:

        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("}");
            }

        }

答案 1 :(得分:0)

尝试一下

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;
}

答案 2 :(得分:0)

    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));
    });

首先计算索引,它喜欢分组依据,但只能选择最后一个。最后,我们可以轻松地从此哈希中获取索引。

答案 3 :(得分:0)

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 ) );
    }

答案 4 :(得分:0)

您应该遍历“ target”数组中的所有元素,并保留一些索引以跟随“ key”数组中当前匹配的模式,将其称为“ keyIndex”。每当“目标”数组中的元素等于“键”数组中“ 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
        }

答案 5 :(得分:0)

您可以使用键数组的长度创建目标数组的子数组,并将每个子数组与键进行比较:

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);
            }
        }
    }
}