我想显示数组中未显示的数字范围,该数组包含1到128之间的数字。
例如数组[87,89,90,91,92,93,94,95,96,97,99]
我要打印1-86、88、98、100-128
我编写了一个仅在第一个未使用的数字和最后一个未使用的数字中间没有数字时起作用的函数
function PrintPorts(ports) {
var portString = "";
var open = true;
let index = 1
for (; index < 129; index++) {
for (let j = 0; j < ports.length; j++) {
if (index == ports[j]) {
open = false;
break;
} else
open = true;
}
if (open) {
portString += index;
break;
}
}
for (; index < 129; index++) {
for (let j = 0; j < ports.length; j++) {
if (index == ports[j]) {
open = false;
break;
} else
open = true;
}
if (!open) {
portString += "-" + (index - 1) + ",";
}
}
if (index == 129 && open) portString += "-" + (index - 1);
return portString;
}
console.log(PrintPorts([87,89,90,91,92,93,94,95,96,97,99]));
这是示例数组的结果1-86,-88,-89,-90,-91,-92,-93,-94,-95,-96,-98,-128
当我需要1-86、88、98、100-128
感谢您的帮助
答案 0 :(得分:2)
首先,使用includes()
方法来测试数组是否包含元素来简化代码。
然后,使用嵌套循环进行操作。外循环寻找范围的起点。找到它时,内部循环会查找范围的末尾。测试开始和结束是否相同,以决定输出一个数字还是两个数字,它们之间用-
。
生成逗号分隔的字符串的最佳方法是将结果放入数组中,然后在末尾使用join()
对其进行串联。
function PrintPorts(ports) {
var openPorts = [];
for (let index = 1; index < 129; index++) {
if (!ports.includes(index)) {
let startPort = index;
for (index = startPort + 1; index < 129; index++) {
if (ports.includes(index)) {
break;
}
}
let endPort = index - 1;
openPorts.push(startPort == endPort ? startPort : `${startPort}-${endPort}`);
}
}
return openPorts.join(",");
}
console.log(PrintPorts([87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99]));
console.log(PrintPorts([1, 2, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 120, 128]));
答案 1 :(得分:0)
function PrintPorts(ports) {
let portString = "";
let start = 1;
let stop = 128;
ports.sort((a,b) => a-b)
for (let i in ports) {
if (start == ports[i] - 1){
portString += start + ","
}
else if(start < ports[i] - 1){
portString += start + "-" + String(ports[i]-1) + ",";
}
start = ports[i] + 1;
}
if (start < stop) {
portString += start + "-" + String(stop);
}
else if (start == stop) {
portString += stop;
}
else
portString = portString.substring(0, portString.length-1);
return portString;
}
console.log(PrintPorts([87,89,90,91,92,93,94,95,96,97,99]));
console.log(PrintPorts([1,3,87,89,90,91,92,93,94,95,96,97,99,128]));
console.log(PrintPorts([95,89,91,92,93,94,,87,96,97,90,99]));
答案 2 :(得分:0)
在数组的开头添加0,在数组的末尾添加129,对元素进行迭代,如果相邻元素之间的差为2,则打印之间的内容,如果大于2,则打印范围。
答案 3 :(得分:0)
如果未对数组进行排序,请先对数组进行排序,然后遍历数组一次,并通过将先前值与当前值进行比较来找到缺失的范围。
以下解决方案还包含缺少范围数组的注释行。
您可以找到有效的代码笔here。
console.log(PrintPorts([87,89,90,91,92,93,94,95,96,97,99]))
function PrintPorts(ports) {
const sorted = ports.sort(function (p1, p2) {return p1-p2} );
const missingRanges = [];
let missingRangesStr = ''
let previous;
sorted.forEach(function(p, index) {
if(index == 0 && p > 1) {
missingRangesStr += (1 + ' - ' + (p - 1)) + ', ';
// missingRanges.push([1, p - 1]);
previous = p;
return;
}
if(index === (sorted.length - 1) && p !== 128) {
// missingRanges.push([p + 1, 128]);
missingRangesStr += (p + 1 + ' - ' + 128) + ', ';
previous = p;
return;
}
if(previous && (previous + 1) !== p) {
if((previous + 2) === p) {
missingRangesStr += (previous + 1) + ', ';
// missingRanges.push([previous + 1]);
} else {
missingRangesStr += (previous + 1) + '-' + (p - 1) + ', ' ;
// missingRanges.push([previous + 1, p - 1]);
}
}
previous = p;
});
return missingRangesStr.replace(/(,\s)$/, '');
}