题解 | #参数解析# JS 版解法-使用数组、字符串方法
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
fun74(line.split(' '));
});
function fun74(ipt) {
let i = 0, res = [];
while (i < ipt.length) {
// 如果当前字符串包含双引号(也就是当前字符串以双引号开头)
if (ipt[i].includes('"')) {
// 如果当前字符串的最后一个字符也是双引号,那就直接把双引号里边的内容 push 进最终结果中,continue
if (ipt[i].charAt(ipt[i].length - 1) === '"') {
res.push(ipt[i].substring(1, ipt[i].length - 1));
i++;
continue;
} else {
// 如果当前字符串不是以双引号结尾
// 那就定义一个 tempRes
let tempRes = [];
// push 进当前字符串除了双引号外的内容
tempRes.push(ipt[i].substring(1));
i++;
// 一直 push,直到遇到包含双引号的字符串(即与之相匹配的那个双引号所在的字符串)
while (i < ipt.length && !ipt[i].includes('"')) {
tempRes.push(ipt[i]);
i++;
}
// 把这个包含双引号的字符串的除了双引号的部分 push 进 tempRes 中
tempRes.push(ipt[i].substring(0, ipt[i].length - 1));
res.push(tempRes.join(' ')); // 添加进最终的 res
}
} else {
// 如果不含双引号,那就直接 push 进 res 中
res.push(ipt[i]);
}
i++;
}
// 最后要在 res 的首端添加参数的数量,然后依次输出即可
res.unshift(res.length);
for (const e of res) {
console.log(e);
}
}
#笔试##华为机试##编程猫##算法#我的编程算法题 文章被收录于专栏
这是我在牛客上做的算法题的个人题解收录
