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

Need to combine two arrays into one, maintaining position of a certain character

本文关键字:数组 位置 字符 两个 合并 一个      更新时间:2023-09-26

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

1)    S ( J D )
2)    S J Z D

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

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

S (J Z D)

不过我不太确定该怎么做。到目前为止,我一直在玩什么:

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

对于加法的情况,我可以复制数组#1,但没有括号(所以数组#3)。将this数组与数组#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) 
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);
}
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(它包含了结果中所需的所有字符),然后将数组#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);