93. Restore IP Addresses

Posted 鸵鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了93. Restore IP Addresses相关的知识,希望对你有一定的参考价值。

class Solution {
    public List<String> restoreIpAddresses(String s) {
        List<String> res=new ArrayList<String>();
        restoreIpAddresses("", 0, 0, s, res);
        return res;
    }
    private void restoreIpAddresses(String ip, int idx, int blocks, String s, List<String> res){
        if(blocks==4&&idx==s.length())
        {
            res.add(ip);
            return;
        }
        if(blocks>4||idx==s.length())
            return;
        if(blocks>0)
            ip+=".";
        if(idx<s.length()&&s.charAt(idx)>=‘0‘&&s.charAt(idx)<=‘9‘)
            restoreIpAddresses(ip+s.charAt(idx), idx+1, blocks+1, s, res);
        if(idx+1<s.length())
        {
            int num=Integer.parseInt(s.substring(idx,idx+2));
            if(9<num&&num<100)
                restoreIpAddresses(ip+s.substring(idx,idx+2), idx+2, blocks+1, s, res);
        }
        if(idx+2<s.length())
        {
            int num=Integer.parseInt(s.substring(idx,idx+3));
            if(99<num&&num<256)
                restoreIpAddresses(ip+s.substring(idx,idx+3), idx+3, blocks+1, s, res);
        }
    }
}

 

以上是关于93. Restore IP Addresses的主要内容,如果未能解决你的问题,请参考以下文章

93. Restore IP Addresses

93. Restore IP Addresses

93. Restore IP Addresses

93. Restore IP Addresses

93. Restore IP Addresses

Leetcode 93. Restore IP Addresses