Restore IP Addresses 还原IP地址@LeetCode

DFS算法的练习,现在逐渐对这类题有感觉了!

在parseInt时用到的一个小技巧是:

int val = Integer.parseInt(s);
if(!String.valueOf(val).equals(s)){	// 防止解析成00.01.010.0类似的情况
	return false;
}




package Level3;

import java.util.ArrayList;


/**
 * 
 * Restore IP Addresses
 * 
 * 
 * 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)
 *
 */
public class S93 {

	public static void main(String[] args) {
		System.out.println(restoreIpAddresses("25525511135"));
	}

	public static ArrayList<String> restoreIpAddresses(String s) {
		ArrayList<String> al = new ArrayList<String>();
		if(s.length() > 12){		// 及时过滤
			return al;
		}
		StringBuilder sb = new StringBuilder();
		dfs(s, al, sb, 0);
		return al;
    }
	
	public static boolean isValid(String s){
		if(s.length()==0 || s.length()>3){
			return false;
		}
		int val = Integer.parseInt(s);
		if(!String.valueOf(val).equals(s)){		// 防止解析成00.01.010.0类似的情况
			return false;
		}
		if(val<0 || val>255){
			return false;
		}
		return true;
	}
	
	public static void dfs(String s, ArrayList<String> al, StringBuilder sb, int level){
		if(level == 3){		// 第3次特别判断,必须取剩下的全部字符串
			if(isValid(s)){
				sb.append(s);
				al.add(sb.toString());		// 最后一次不加.
				sb.delete(sb.length()-s.length(), sb.length());	// 撤销动作
				return;
			}
		}
		
		for(int i=0; i<s.length()-1; i++){
			String substr = s.substring(0, i+1);
			if(isValid(substr)){
				sb.append(substr).append(".");		// 执行动作
				dfs(s.substring(i+1), al, sb, level+1);		
				sb.delete(sb.length()-1-substr.length(), sb.length());	// 撤销动作
			}
		}
	}
}


更加清晰的思路:

public class Solution {
    public List<String> restoreIpAddresses(String s) {
        List<String> ret = new ArrayList<String>();
        List<String> list = new ArrayList<String>();
        rec(ret, list, s);
        return ret;
    }
    
    public void rec(List<String> ret, List<String> list, String s) {
        if(list.size() > 4) {       // not possible
            return;
        }
        if(list.size() == 4 && s.length() == 0) {
            String concat = "";
            for(int i=0; i<list.size(); i++) {
                concat += list.get(i);
                if(i != list.size()-1) {
                    concat += ".";
                }
            }
            ret.add(concat);
            return;
        }
        
        for(int i=1; i<=3; i++) {
            if(i > s.length()) {
                continue;
            }
            String substr = s.substring(0, i);
            if(Integer.parseInt(substr) > 255) {        // ignore >255
                continue;
            }
            if(substr.length() > 1 && substr.charAt(0) == '0') {    // ignore sth like "01"
                continue;
            }
            list.add(substr);
            rec(ret, list, s.substring(i));
            list.remove(list.size()-1);
        }
    }
}




你可能感兴趣的:(Restore IP Addresses 还原IP地址@LeetCode)