Simple 5 line JS/ES6 Solution


  • 1
    D
    restoreIP = (str, res = []) => {
      if (str.length == 0 && res.length == 4) return console.log(res)
      for (let i = 1; i <= Math.min(3, str.length); i++) 
        if (parseInt(str.slice(0, i)) < 256)  
          restoreIP(str.slice(i), res.concat(str.slice(0,i)))
    }
    

    Note: the use of Array.prototype.slice() creates a new copy of the array, so there is implicit backtracking


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.