码迷,mamicode.com
首页 > 其他好文 > 详细

Restore IP Addresses

时间:2015-03-10 22:59:02      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Restore IP Addresses

标签:

原文地址:http://www.cnblogs.com/sunshisonghit/p/4328480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!