93. Restore IP Addresses--back tracking -- 类比39 Combination Sum

93 给你 一串数字,让你求出可能的IP 地址。

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]

仔细分析一下这题和39题 几乎完全一样呀,39是给你 一个没有重复数组,可以重复使用数组里的数字之和来组成 target. 
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

93题本质: 因为IP 地址只能是1~3位, 假设有一个数组 nums = {1,2,3} 表示每次选择IP地址的长度 ,然后 从Nums里可以用重复数字,一共取4次,当取得数字的sum = input 字符串的长度 时,说明选择的长度为合法的。         然后验证该长度下IP 地址是否合法, 以下两种为 非法情况: 1. IP>255  2. IP长度大于1位,但前面有0, 比如 05, 003.

为了便于理解,以及和 39题对照,定义了nums 数组,这段code 只能排 67% , code 如下: 
class Solution {
    public List<String> restoreIpAddresses(String s) {

        List<String> result = new ArrayList<>();
        int[] nums = {1,2,3};
        dfs(new ArrayList<Integer>(), nums, result, 0,0, s);
        return result;
    }

    private void dfs(List<Integer> list, int[] nums, List<String> result, int depth, int sum, String s){
        if(depth == 4 && sum == s.length()){
            String ip = gen_ip_address(list, s);

            if(!ip.equals("-1")){
                result.add(ip);
                return;
            }
        }

        if(depth == 4 ||  sum > s.length()){
            return;
        }

        for(int i=0; i< nums.length; i++){
            sum += nums[i];
            list.add(nums[i]);
            dfs(list,nums,result,depth+1, sum, s);
            sum -= nums[i];
            list.remove(list.size()-1);
        }
    }

    private String gen_ip_address(List<Integer> list, String s){

        int start = 0;
        String res = "";
        for(int i=0; i<4; i++){
            String si = s.substring(start, start+list.get(i));
            start += list.get(i);
            if(Integer.valueOf(si) >255)
                return "-1";
            if(list.get(i)>1 && si.charAt(0) == ‘0‘)
                return "-1";
            if(i<3) res += si+".";
            else res+= si;
        }
        return res;
    }
}

去掉了 nums 并且用remain 代替sum, 可以排名95%:

class Solution {
    public List<String> restoreIpAddresses(String s) {

        List<String> result = new ArrayList<>();
        dfs(new ArrayList<Integer>(),  result, 0,s.length(), s);
        return result;
    }

    private void dfs(List<Integer> list, List<String> result, int depth, int remain, String s){
        if(depth == 4 && remain == 0){
            String ip = gen_ip_address(list, s);
            if(!ip.equals("-1")){
                result.add(ip);
                return;
            }
        }

        if(depth == 4 ||  remain <0){
            return;
        }
        for(int i=1; i<=3; i++){
            list.add(i);
            dfs(list,result,depth+1, remain-i, s);
            list.remove(list.size()-1);
        }
    }

    private String gen_ip_address(List<Integer> list, String s){
        int start = 0;
        String res = "";
        for(int i=0; i<4; i++){
            String si = s.substring(start, start+list.get(i));
            start += list.get(i);

            if(Integer.valueOf(si) >255)
                return "-1";
            if(list.get(i)>1 && si.charAt(0) == ‘0‘)
                return "-1";
            if(i<3) res += si+".";
            else res+= si;
        }
        return res;
    }
}
 

原文地址:https://www.cnblogs.com/keepAC/p/9944502.html

时间: 2024-11-08 19:31:12

93. Restore IP Addresses--back tracking -- 类比39 Combination Sum的相关文章

【LeetCode】93. Restore IP Addresses 【面试题】

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 mat

93. 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) 什么时候临时容器符合题意加入结

LeetCode: 93. 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) 给一串数字字符串,返回所有可能的

leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法

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) 思路:本题用递归实现,一个ip

LeetCode 93. Restore IP Addresses 20170705 部分之前做了没写的题目

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) 题目大意:给出一串数字组成的字符

【一天一道LeetCode】#93. Restore IP Addresses

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return [&qu

[leetcode]93. Restore IP Addresses还原IP地址

Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: "25525511135" Output: ["255.255.11.135", "255.255.111.35"] 题意 给定一个没加点的IP地址,返回所有可能的合法IP地址. 思路:DFS 代码 原文

【leetcode刷题笔记】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) 题解:深度优先搜索.用resul

【leetcode】Restore IP Addresses

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 mat