题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        console.log(getResult(line));
    }
})();
function getResult(data) {
    let map = new Map();
    data = data.replace(/[0-9]+/g, (value) => {
        map.set(value, value.length);
        return value.length;
    });
    data = data.replace(/[^0-9]+/g, "*");
    let arr = data.split("*");
    arr = arr.map((a) => {
        return (a = Math.abs(a));
    });

    let res = Math.max(...arr);
    let res1 = "";
    map.forEach((value, key) => {
        if (value == res) {
            res1 += key;
        }
    });
    return res1 + "," + res;
}

全部评论

相关推荐

01-26 19:51
门头沟学院 Java
isabener:怎么感觉像群发的呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务