基于另一个充满“<”和“>”符号的数组排列数组
Arrange an array based on another array filled with “<” and “>” symbols
我有两个数组。一个大小为n
的数组由<
和>
符号填充。第二个数组的大小为n+1
,具有整数。我必须以这样的方式排列第二个数组,使它能够满足第一个数组的条件。请注意,第一个数组不能修改。
conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];
可能的输出:
[9, 1, 11, 12, 13, 2]
我有个想法:
1)对数据使用内置的排序方法。这满足conditionArray = ['<', '<', '<' ...]
。
2)遍历conditionArray
,对于每个>
,取最后一个元素(最大的一个),并将其插入当前索引中,移动每一个其他元素。否则,<
已经满足,可以跳过。
在您的示例中,数组将经历以下中间状态:
12, 9, 1, 11, 13, 2 //start
1, 2, 9, 11, 12, 13 // initial sort
13, 1, 2, 8, 11, 12 // index 0 of condition is a > so insert the last element at index 0
13, 1, 2, 8, 11, 12 // < is NOP
13, 1, 2, 8, 11, 12 // < is NOP
13, 1, 2, 8, 11, 12 // < is NOP
13, 1, 2, 8, 12, 11 // index 4 of condition is a > so insert the last element at index 4
一个非常基本的实现:
conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];
numArray.sort( function(a, b) {return a -b ; /*lazy number comparison*/ });
console.log(numArray);
var size = numArray.length;
conditionArray.forEach( function(comparison, index) {
//ignore '<'
if (comparison == '>')
{
var finalElement = numArray[size - 1];
numArray.splice(index, 0, finalElement); //insert the element
}
console.log(numArray);
});
numArray = numArray.slice(0, size); //remove all the extra stuff at the end
console.log(numArray);
这段代码大量使用插入。它可以很好地用于链接列表,因为插入是一个常数时间操作,但它不能很好地扩展到数组。如果插入的计算量很大(就像数组的典型情况一样),则可以在辅助数组中一次只插入一个元素。
conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];
numArray.sort( function(a, b) {return a - b; /*lazy number comparison*/ });
console.log(numArray);
var size = numArray.length;
var newArray = [];
var numInsertedFromBack = 0;
conditionArray.forEach( function(comparison, index) {
if (comparison == '>')
{
var biggestElementNotAlreadyInserted = numArray[size - 1 - numInsertedFromBack];
newArray.push(biggestElementNotAlreadyInserted);
numInsertedFromBack += 1;
}
else {
//just insert the next element
//since we inserted stuff out of turn, we need
//to account for each skip, and backtrack that many times
var smallestElementNotAlreadyInserted = numArray[index - numInsertedFromBack];
newArray.push(smallestElementNotAlreadyInserted);
}
console.log(newArray);
});
//need to manually add the straggling final element
//since the comparisons are of array.length - 1
newArray.push(numArray[size - 1 - numInsertedFromBack]);
console.log(newArray);
conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];
isComplete = false;
console.log(numArray);
while (isComplete == false) {
isComplete = true;
for (i = 0; i < conditionArray.length; i++) {
if ((conditionArray[i] == ">" && numArray[i] < numArray[i+1]) || (conditionArray[i] == "<" && numArray[i] > numArray[i+1])){
isComplete = false;
temp = numArray[i];
numArray[i] = numArray[i+1];
numArray[i+1] = temp;
}
}
}
document.write(numArray);
<!DOCTYPE html>
<html lan="en">
<meta charset="utf-8">
<body>
</body>
</html>
相关文章:
- AngulsrJS $filter orderby 一个带有 @ 符号的数组键
- 如何检查符号是否在范围(或数组)中
- 在javascript中将字节数组转换为有符号的int64
- Javascript 正则表达式来匹配我的数组中的表情符号
- 为什么JSLint告诉我“;使用数组文字符号[]&”;
- 如何操作对象's结构转换成'点符号'-样式数组
- 序列化无符号整数(ulong)数组时,Json.NET崩溃
- 带字符串拆分的JS Lint数组文字符号
- 无符号整数数组到base64的快速转换
- 获取字符串中两个符号之间的字符串,并将它们推送到数组中
- 多维数组[键]符号在jQuery ajax()上失败
- 基于另一个充满“<”和“>”符号的数组排列数组
- Javascript:通过数组符号字符串访问对象(数组)
- 循环使用带有数字和符号的字符串&推入数组
- 二维数组映射在点自由符号
- 如何在javascript中将字节数组转换为有符号的大整数
- 为什么javascript中数组符号前面的加号会返回零
- JavaScript-使用数组符号将函数添加到原型中
- 对象上的Javascript数组符号
- "|"有什么用?(管道)符号在JS数组中