需要将两个数组合并为一个数组,以保持某个字符的位置
Need to combine two arrays into one, maintaining position of a certain character
我有两个字符数组。它们看起来像这样:
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);
相关文章:
- 如何从某个位置顺时针和逆时针循环数组
- 将javascript数组中的项移动到特定位置的有效方法
- 生成ACF标记位置的数组(元素列表后缺少])
- 基于javascript中位置的数组差异
- Javascript删除数组中某个位置之后的项
- 在数组中迭代时的JS循环位置
- 在两个位置显示数组中的随机单词
- javascript递归,按数组位置查找子级
- 检查数组位置是否真的未定义
- 检查值存在于数组位置
- 在数组位置获取并设置cookie值
- 如何访问成员模板中的数组位置
- 递增数组位置
- 返回最低数字的数组位置(非负数)
- 我如何使一个按钮改变Javascript数组位置
- 我如何保存字符串的每个字母在不同的数组位置?并合并回单个字符串
- 下划线-比较两个对象数组(位置)
- for循环中的Ajax不会返回数组位置的正确值,尽管闭包绑定了当前值位置的范围
- 根据对象在Firebase中的数组位置检索对象
- 当使用x作为索引号时.要在函数中显示数组位置,数组返回undefined