Restore IP Addresses

Restore IP Addresses

问题:

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

思路:

  dfs + 回溯模板

我的代码1:

isValid中要catch到 00 0 010是不合法的数据

public class Solution {

    public List<String> restoreIpAddresses(String s) {

        if(s == null || s.length() < 4 || s.length() > 12)    return list;

        helper(s, 0, "");

        return list;

    }

    private List<String> list = new ArrayList<String>();

    public void helper(String s, int count, String res)

    {

        if(count == 4 && s.equals(""))

        {

            list.add(res.substring(1));

            return;

        }

        for(int i = 1; i <= s.length() && i <= 3; i++)

        {

            String ip = s.substring(0,i);

            if(validIp(ip))

            {

                helper(s.substring(i), count + 1, res + "." + ip);

            }

        }

    }

    public boolean validIp(String ip)

    {

        if(ip.length() > 1 && ip.charAt(0) == '0') return false;

        int value = Integer.parseInt(ip);

        if(value >= 0 && value <= 255)  return true;

        return false;

    }

}
View Code

代码1中 满足要求的代码里面 应该加入if(count == 4 && !s.equals("")) return;早一些弹栈,免得浪费时间

我的代码2:

public class Solution {

    public List<String> restoreIpAddresses(String s) {

        if(s == null || s.length() < 4 || s.length() > 12)    return list;

        helper(s, 0, "");

        return list;

    }

    private List<String> list = new ArrayList<String>();

    public void helper(String s, int count, String res)

    {

        if(count == 4)

        {

            if(!s.equals("")) return;

            list.add(res.substring(1));

            return;

        }

        for(int i = 1; i <= s.length() && i <= 3; i++)

        {

            String ip = s.substring(0,i);

            if(validIp(ip))

            {

                helper(s.substring(i), count + 1, res + "." + ip);

            }

        }

    }

    public boolean validIp(String ip)

    {

        if(ip.length() > 1 && ip.charAt(0) == '0') return false;

        int value = Integer.parseInt(ip);

        if(value >= 0 && value <= 255)  return true;

        return false;

    }

}
View Code

代码2中 isValid代码太丑了,一点也不简洁,后面两行可以合并的

我的代码3

public class Solution {

    public List<String> restoreIpAddresses(String s) {

        if(s == null || s.length() < 4 || s.length() > 12)    return list;

        helper(s, 0, "");

        return list;

    }

    private List<String> list = new ArrayList<String>();

    public void helper(String s, int count, String res)

    {

        if(count == 4)

        {

            if(!s.equals("")) return;

            list.add(res.substring(1));

            return;

        }

        for(int i = 1; i <= s.length() && i <= 3; i++)

        {

            String ip = s.substring(0,i);

            if(validIp(ip))

            {

                helper(s.substring(i), count + 1, res + "." + ip);

            }

        }

    }

    public boolean validIp(String ip)

    {

        if(ip.length() > 1 && ip.charAt(0) == '0') return false;

        int value = Integer.parseInt(ip);

        return value >= 0 && value <= 255;

    }

}
View Code

 

你可能感兴趣的:(store)