题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
// 这道题主要是要考虑特殊输入,主要挑选不满足的情况
// 解题思路:判断数组长度,元素长度,还有是不是以0开头同时长度大于1,还有判断值是否在0-255
// 只输入3段:1.2.3,
// 或者是以0或符号开头的,01.2.3,-1.2.3
void (async function () {
// Write your code here
let result = "YES";
while ((line = await readline())) {
const arr = line.split(".");
if (arr.length != 4) result = "NO";
for (let item of arr) {
if (
item.length == 0 ||
(item.startsWith("0") && item.length > 1) ||
!/^[0-9]/.test(item)||
parseInt(item) < 0 ||
parseInt(item) > 255
) {
result = "NO";
break;
}
}
}
console.log(result);
})();
查看17道真题和解析