Leetcode 93-- 递归

知识点:使用ArrayList();
注意这道题的陷阱:010 或者001 用0 开头的是不合法的。
针对这一点使用来回string int转换判断字符串长度是否改变


Leetcode 93-- 递归_第1张图片
图片.png
class Solution {
    public List restoreIpAddresses(String s) {
        List ans=new ArrayList();
        helper(s,0,"",ans);
        return ans;
    }
    public void helper(String left,int times, String out,Listans){
        if(times==4){
            if(left.isEmpty())
                ans.add(out);
            return;
        }
        for(int i=1;i<4;i++){
            if(left.length()255|| i != String.valueOf(val).length())
                continue;
            helper(left.substring(i),times+1,out+left.substring(0,i)+(times==3?"":"."),ans);
        }
    }
}

你可能感兴趣的:(Leetcode 93-- 递归)