题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
numberStrTest(line)
});
// 检查是不是数字字符串
function numberStrTest(str: string) {
// 保存数字字符串及其长度
const arr = []
let max = 1
// 1、循环遍历每一个字串
for(let i = 0; i < str.length; i++){
for(let j = str.length - 1; j > i; j--){
const tmpStr = str.slice(i,j+1)
// 2、判断字串是不是数字字串且长度比max大
if(/^[0-9]{1,}$/.test(tmpStr) && tmpStr.length >= max){
arr.push(tmpStr)
max = tmpStr.length
}
}
}
// 按照题目需求拼接输出
console.log(`${arr.filter((item)=>item.length == max).join('')},${max}`)
}
