需要将两个数组合并为一个,保持某个字符的位置

时间:2013-08-07 15:31:11

标签: javascript algorithm

我有两个字符数组。它们看起来像这样:

1)    S ( J D )

2)    S J Z D

第二个数组总是与第一个数组不同。它没有任何括号,并且将具有+/- 1个字母字符,或者仅交换两个字符的位置。我基本上需要将两个数组合并在一起,这样我就有了第二个数组的字符,但保留了第一个数组的括号。 (如果你看下面的测试用例,它会更容易理解)

在上面的例子中,输出应为:

S (J Z D)

我不太确定如何做到这一点。到目前为止,我一直在玩弄什么:

您可以计算每个数组中的字母字符,看看您是在添加,减去还是交换。

对于添加的情况,我可以制作数组#1的副本,但没有括号(所以数组#3)。将此数组与数组#2进行比较,找出第一个区别。注意索引。然后迭代#1直到你达到那个索引(每个括号减去1)。然后将#2中的角色复制到数组中。

对于减法情况,执行与添加相同的操作,只有在找到差异时,才将其从列表#1中删除。

有人能想出更好的方法来解决这个问题吗?

测试用例:

Input

Array1: A (G F)
Array2: A G D F

Output

A (G D F) 


Input

Array1: A (G F)
Array2: A G F D

Output

A (G F) D 

Input

Array1: A (G F)
Array2: A D G F

Output

A D (G F) 


Input

Array1: A (G F)
Array2: A F

Output

Input

Array1: A (G F)
Array2: G F

Output

(G F) 

Input

Array1: A (G F)
Array2: A F G

Output

A (F G) 

3 个答案:

答案 0 :(得分:1)

function myFunction()
{
var a = ["S","(","J","D",")"];
var b = ["S","P"];
var c = new Array();

var i = 0;
var j = 0;
var k = 0;

while (i < a.length) {
    if (a[i] == '(' || a[i] == ')')
        c[k++] = a[i++];

    if (i < a.length && j < b.length) {
        // If character in a and b matches the add to the final result
        if (a[i] == b[j]) {
            c[k++] = a[i++];
            j++;
        } else {
                // If the character in a and b don't match then check if character in a exist in b. 
                // If yes then add the character in b else skip
                if (b.indexOf(a[i]) != -1)
                    c[k++] = b[j++];
                else
                    i++;
            }
        }
    }

    while (j < b.length)
        c[k++] = b[j++];

alert(c);
}

答案 1 :(得分:0)

package com.test;

import java.util.ArrayList;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
    Character[] a = {'S','(','J','D',')'};
    Character[] b = {'S','J','Z','D'};
    ArrayList<Character> c = new ArrayList<Character>();

    int i = 0; // Array a pointer
    int j = 0; // Array b pointer

        while (i < a.length) {
            // If it is an opening or closing bracket then add to the final result
            if (a[i] == '(' || a[i] == ')')
                c.add(a[i++]);

            if (i < a.length && j < b.length) {
            // If character in a and b matches the add to the final result
            if (a[i] == b[j]) {
                c.add(a[i++]);
                j++;
            } else {
                // If the character in a and b don't match then check if character in a exist in b. 
                // If yes then add the character present in b to final result else skip
                if (Arrays.asList(b).contains(a[i]))
                    c.add(b[j++]);
                else
                    i++;
            }
        }
    }

    while (j < b.length)
        c.add(b[j++]);

    System.out.println(c);
}

}

答案 2 :(得分:0)

  

有人能想出更好的方法来解决这个问题吗?

我认为copy数组#2(在结果中包含你想要的所有字符)然后insert数组#1中的括号进入副本会更容易。

var array1 = "S(JZD)".split(''),
    array2 = "SZD".split('');

var result = array2.slice();
for (var i=array1.length; i--; ) // iterate backwards to prevent index troubles
    if (/[()]/.test(array1[i]))
        result.splice(i, 0, array1[i]);
console.log(result);