我想得到消耗对的自然数所有增量或减量序列

时间:2015-10-02 18:45:27

标签: java algorithm

假设我有一个数组2,9,0,6,1,2,3,6,12,0,3,9 所以第一对应该是1,2,3,因为它是固定的增量。 第二对应该是6,12 第三对应该是0,3,9

int [] arr = {2,9,0,6,1,2,3,6};

            System.out.println(">>>>>>>"+array(arr));

            //to get the max sequence.
            int sequenceLength = 3;
            Map<String, Integer> map = new HashMap<String, Integer>();
            int count;
            String str1, str2;
            for (int i = 0; i <= arr.length - sequenceLength; i++) {
                str1 = "";
                count = 0;
                for (int a = i; a < i + sequenceLength; a++) {
                        str1 += "" + arr[a];
                    }
                if (map.get(str1) != null) {
                    continue;
                } else {
                    map.put(str1, count);
                }

                for (int ii = i; ii <= arr.length - sequenceLength; ii++) {
                    str2 = "";
                    for (int a = ii; a < ii + sequenceLength; a++) {
                        str2 += "" + arr[a];
                    }
                    if (str1.equals(str2)) {
                        count++;
                        map.put(str1, count);
                    }
                }
            }   

1 个答案:

答案 0 :(得分:0)

PSEUDO CODE:

incrementArray = new Array<pair>
increasingSequences = new Array<pair> //size of N
increasingSequenceCount = 0    //You start with 0 incrementing arrays
for i -> N
    value = array[i];
    for j -> incrementArray.length
        if (incremtentArray[j] == value + incrementArray[j])
            addValueToIS(value, j) //adds to array j of increasingSequences
        else
           incrementArray.close(j) //closes increasingSequence array
    incrementArray.push(array[i], increasingSequenceCount++);

最后,increaseSequences将包含X个大小为&gt;的数组。这些是你的序列