SOURCE

/**
 * @param {string} s
 * @return {string[]}
 */
var restoreIpAddresses = function (s) {
    const res = [];
    const dfs = (canStartIndex, selectArr) => {
        // 巧用 join ,可以不考虑加 '.' 的情况
        const unCheckedRes = selectArr.join('.');
        if (selectArr.length === 4 && unCheckedRes.length === s.length + 3) {
            res.push(unCheckedRes);
        }

        for (let singleLen = 1; singleLen <= 3; singleLen++) {
            const nextStartIndex = canStartIndex + singleLen;
            // 假设下标越界了,直接返回
            if (nextStartIndex > s.length) {
                return;
            }
            const str = s.substring(canStartIndex, nextStartIndex);
            // 假设当前截断的数超过255 或 02、033 这样的情况直接返回
            if (Number(str) > 255 || (str.length > 1 && str[0] === '0')) {
                return;
            }
            dfs(nextStartIndex, [...selectArr, str])
        }
    };
    dfs(0, []);
    return res;
}
console.log('res', restoreIpAddresses('101023'))
console 命令行工具 X clear

                    
>
console