93. Restore IP Addresses(js)

93. Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
题意:给定一个数字字符串,求出所有组成合法的ip地址
代码如下:
/**
 * @param {string} s
 * @return {string[]}
 */
var restoreIpAddresses = function(s) {
      let res=[]
        let item =''
        if(s.length<4 || s.length>12){
            return res;
        }
        dfs(s,0,item,res);
        return res;
    }
var dfs=function( s, start, item, res){
        if(start==3 && isValid(s) ){
            res.push(item+s);
            return;
        }
        for(let i=0;i<3 && i<s.length-1;i++){
            let substr=s.substring(0,i+1);
            if(isValid(substr)){
                dfs(s.substring(i+1,s.length),start+1,item+substr+'.',res);
            }
        }
        
    }
var isValid=function(s){
        if(s.charAt(0)=='0'){
            return s==='0';
        }
        if(parseInt(s)<=255 && parseInt(s)>0){
            return true;
        }
        else{
            return false;
        }
    };

 

posted @ 2019-04-14 21:54  mingL  阅读(123)  评论(0编辑  收藏  举报