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

For example:
Given ​​`​"25525511135"​`​,

return ​`​["255.255.11.135", "255.255.111.35"]​`​. (Order does not matter)

DFS

【复杂度】

【思路】

【注意】

【代码】

`public class Solution {    public List<String> restoreIpAddresses(String s) {        //require        List<String> ans=new LinkedList<>();        List<String> ip=new LinkedList<>();        //invariant        dfs(ip,s,0,ans);        //ensure        return ans;    }    private void dfs(List<String> ip,String s,int start,List<String> ans){        //base case        if(ip.size()==4&&start==s.length()){            ans.add(ip.get(0)+"."+ip.get(1)+"."+ip.get(2)+"."+ip.get(3));            return;        }        int remain=4-ip.size();        if(remain>(s.length()-start)||remain*3<(s.length()-start))return;   //剪枝        int num=0;        for(int i=start;i<start+3;i++){            if(i+1<=s.length()){                num=num*10+s.charAt(i)-'0';                if(num>=0&&num<=255){                    ip.add(s.substring(start,i+1));                    dfs(ip,s,i+1,ans);                    ip.remove(ip.size()-1);                }                if(num==0)break;//不允许前缀为0，但允许单个0            }        }    }}`