LeetCode 093 Restore IP Addresses

来源:互联网 发布:skycc营销软件下载 编辑:程序博客网 时间:2024/06/06 01:54
题目


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)


思路


1 能够感觉到这个问题可以DFS来解决。

2 输入是否可以非法?010. 这样的输出是否可以?这都是要问面试官的。

3 每一段数字可行的就是0-255。一旦超过255,那么后面的字符再和目前的字符串组成这段数字,必然都是超过255,都是不合法的。所以一旦检查到目前这段超过255。就表示这段到此为止了。

4 什么时候结束(合法)?一旦4段数字填满,并且原字符串也都检查完毕了。

5 010. 这类是非法的。怎么解决?考虑0开头,必然检查到这段第一个字符为‘0’,那么这时候这段数字只有可能是0。一定0的情况都检查完了,这段数字就不用再增添字符考虑其他情况了,即break跳出循环。


代码


public class Solution {    public List<String> restoreIpAddresses(String s) {        ArrayList<String> ans = new ArrayList<String>();        String temp = "";        int countnumber = 0;        int curindex = 0;                useme(s,ans,temp,countnumber,curindex);        return ans;            }        public void useme(String s, ArrayList<String> ans, String temp,int countnumber,int curindex ){        if(countnumber ==4 && curindex == s.length()){            temp = temp.substring(0,temp.length()-1);            ans.add(new String(temp));        }        if(countnumber <4 && curindex<s.length()){            int length = temp.length();            for(int i=curindex;i<s.length();i++){                if(i == curindex && s.charAt(i)=='0'){                    temp= temp+"0.";                    useme(s,ans,temp,countnumber+1,i+1);                    temp = temp.substring(0,length);                    break;                }                else{                    String test = s.substring(curindex,i+1);                    int curnumber =Integer.parseInt(test);                    if(curnumber > 255){                        break;                    }                    temp = temp +test+".";                    useme(s,ans,temp,countnumber+1,i+1);                    temp = temp.substring(0,length);                }            }        }    }}

总结


1 题目中的写法还可以简洁,比如curindex表示目前检查到哪个字符。其实可以截断/回溯传入源字符串的方式,来省去这个变量。

2 countnumber 表示目前已经完成几个数字段了。

3 有代码可以简洁的检查010 这种非法情况,不过感觉需要硬背一下:

int val = Integer.parseInt(s);    check(s.equals(String.valueOf(val))


******************************************************************************************************************************************************
15.4.14
public class Solution {       public  List<String> restoreIpAddresses(String s) {        List<String> ans = new ArrayList<String>();        StringBuffer temp = new StringBuffer();        StringBuffer sb = new StringBuffer(s);        int n = 0;        int startIndex = 0;        useme(sb,temp,ans,n,startIndex);        return ans;    }        public  void useme(StringBuffer sb, StringBuffer temp,    List<String> ans,int n,int startIndex){        if( startIndex ==sb.length()  && n == 4){            ans.add(temp.substring(0,temp.length()-1));            return;        }                if(n>4){        return;        }        for(int i =startIndex;i<sb.length();i++){            if(legal(sb,startIndex,i)){                temp.append(sb.substring(startIndex,i+1)).append(".");                useme(sb,temp,ans,n+1,i+1);                temp =temp.delete(temp.length()-(i-startIndex)-2,temp.length());            }        }    }        public  boolean legal(StringBuffer sb ,int start,int end){        if(end-start>3){        return false;        }        String cur= sb.substring(start,end+1);        int num = Integer.parseInt(cur);        String cur2 = Integer.toString(num);                if(!cur2.equals(cur)){            return false;        }        if(num>255){            return false;        }        return true;            }}

注意点:

1 StringBuffer的应用

2 数字如何判断合法

3 回退的写法



0 0
原创粉丝点击