我有以下功能,我想将其翻译成JS(我还是JS的新手,所以有一些困难):
它需要N个节点的完整图表,并枚举所有唯一的匹配对象。
/**
*
* @param nodes The nodes still to be added to our edge list.
* @param edges The current edge list. This is mutated, so always return a clone!
*/
public static <N> List<Map<N,N>> enumerateEdges(List<N> nodes,Map<N,N> edges){
if(nodes.isEmpty()) // No more nodes to create edges from, so return our current edge list in a new list.
return Collections.singletonList(new HashMap<>(edges));
N start = nodes.get(0); //The start node of our next pair.
List<Map<N,N>> acc = new LinkedList<>(); //The accumulation of the EdgeLists
for(int i = 1; i<nodes.size(); ++i){
N end = nodes.get(i); //The end node of our pair
edges.put(start,end); //Add this pair to our edge list
List<N> unused = new ArrayList<>(nodes); // The nodes not used in our edge list.
unused.remove(i);
unused.remove(0);
acc.addAll(enumerateEdges(unused,edges));
edges.remove(start); //Remove this pair from our edge list.
}
return acc;
}
跟:
List<Map<Integer,Integer>> results = enumerateEdges(Arrays.asList(0,1,2,3),new HashMap<>());
我目前对此的尝试并不奏效。它在执行console.log()
时输出空数组。
function enumerateEdges(nodes, edges) {
if (nodes.length == 0) return [];
let start = nodes[0];
let acc = [];
for(let i = 1; i < nodes.length; i++) {
let end = nodes[i];
edges = [ {start,end} ];
let unused = nodes.slice(0);
unused.splice(i,1);
unused.splice(0,1);
acc.push.apply(acc, enumerateEdges(unused,edges));
edges.splice(0, 1);
}
return acc;
}
用以下方式调用:
let nodes = [1,2,3,4];
let edges = [];
enumerateEdges(nodes, edges);
有没有人有任何想法?非常感谢。
答案 0 :(得分:2)
主要问题是:
nodes.length == 0
时)不应返回空数组,而是 edges 数组edges = [ {start,end} ]
完全覆盖之前edges
中的内容。你需要push
对它。此外edges.splice(0, 1)
删除第一个元素,但在原始代码中,它必须删除 start 键入的元素,这实际上是边缘中的最后一个元素列表。 请注意,JavaScript有一个Map
构造函数可用于edges
,因此它与Java代码的工作方式非常相似。但在这种情况下,我发现它使用过度杀伤:数组将正常工作。我在第二个片段中使用Map添加了版本。
修改:我还建议将条件length == 0
更改为length < 2
,这样当你传递一个奇数时就不会遇到麻烦节点。
function enumerateEdges(nodes, edges) {
if (nodes.length < 2) return [...edges]; // return copy
let start = nodes[0];
let acc = [];
for(let i = 1; i < nodes.length; i++) {
let end = nodes[i];
edges.push({start, end}); // don't overwrite, but push
let unused = nodes.slice(0);
unused.splice(i,1);
unused.splice(0,1);
// The spread operator will put each of the array elements as separate arguments
// ... so no more need for the mysterious apply:
acc.push(...enumerateEdges(unused, edges));
edges.pop(); // in practice it is always the last element to be removed
}
return acc;
}
let nodes = [1,2,3,4];
let edges = [];
let result = enumerateEdges(nodes, edges);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
为了好玩,以下是它的高度浓缩版本:
function enumerateEdges(nodes, edges) {
return nodes.length < 2 ? edges
: nodes.reduce( (acc, end, i) => (i<2 ? [] : acc).concat(
enumerateEdges(nodes.slice(1, i).concat(nodes.slice(i+1)),
[...edges, {start:nodes[0], end}])
) );
}
let nodes = [1,2,3,4];
let edges = [];
let result = enumerateEdges(nodes, edges);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
function enumerateEdges(nodes, edges) {
if (nodes.length < 2) return [...edges]; // return copy
let start = nodes[0];
let acc = [];
for(let i = 1; i < nodes.length; i++) {
let end = nodes[i];
edges.set(start, end); // <-- Map method to add
let unused = nodes.slice(0);
unused.splice(i,1);
unused.splice(0,1);
// The spread operator will put each of the array elements as separate arguments
// ... so no more need for the mysterious apply:
acc.push(...enumerateEdges(unused, edges));
edges.delete(start); // <-- Map method to remove
}
return acc;
}
let nodes = [1,2,3,4];
let edges = new Map(); // <-- use Map
let result = enumerateEdges(nodes, edges);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
请注意,边缘现在作为简单的对数组输出,例如[1, 2]
,而不是{start: 1, end: 2}
。这当然可以改变,但我这样做了 - 它是地图转换为数组的默认方式。