93. 复原 IP 地址 -dfs

1.题目

2.思路

一看见这个就是dfs的写法,或者叫做回溯的思想。

dfs(int index, int cnt, String temp, String s);

index : 遍历字符串的索引,最大为s.length()

cnt:记录当前已经拥有切分整数个数,最大为4

temp:记录当前已经形成的字符串,记得加点.

s:代表原来的字符串


// 时间复杂度:3^4 * 20
// 对于第一个节点来说,构造一棵树,有三个节点,树的深度最大为4,所以此时时间复杂度为3^4
// 总共有字符串大小的长度的节点,也就是20个。
// 所以最终时间为 3^4 * 20
class Solution {
    List<String>ans = new ArrayList();
    int n ;
    public List<String> restoreIpAddresses(String s) {
        n = s.length();
        if(n <= 3)return ans;
        dfs(0, 0, "", s);
        return ans;
    }

// cnt -记录切分的整数个数
// indeex -记录遍历的索引的位置
    public void dfs(int index, int cnt, String temp, String s){
        if(index == n){
            if(cnt == 4){
                ans.add(temp);
            }
            return ;
        }
        if(cnt == 4) return ; //代表后面还有数字
        int preFlag = 1; //代表前面是否加.
        if(index == 0) preFlag = 0;
        // 可以选1,2,3位数字
        for(int i = 1; i <= 3; i++){
            String res = temp;
            if(preFlag == 1)res = res + "." ;
            if(index + i <= n){
                String s2 = s.substring(index, index + i);
                if(Double.parseDouble(s2) >=0 && Double.parseDouble(s2) <= 255 ){
                    if(i != 1 && s2.charAt(0) == '0'){
                        continue;
                    }
                    res += s2;
                    dfs(index + i, cnt + 1, res, s);
                       
                }
               
            }
           
        }
        return ;
    }
}

时间复杂度如何分析?  --3^4 * s.length()

dfs的时间复杂度分析基本一样的。--找树的子节点个数 和 树的深度,以及有多少颗这样的树!!

首先以第一个节点为例,可以产生3个子节点(也就是三个dfs(),对于当前元素,可以有三个不同的操作, 选择1位,选择2位,选择3位,因为数字范围是0-255,所以最大是3位,最小是1位。)对于第一个节点形成的树,深度大小为4,因为最多只能有四次这样的操作,因为IP地址整数个数为4,所以这个节点的时间复杂度为 3^4.

当第一个节点的树都遍历完后,那么下一个节点可能就是选择前2个数作为起点,再下一个起点就是选择前3个节点作为起点,按照答案的意思,可以选择前20个数作为起点,所以是有20颗这样的树。所以最终为3^4 * s.length()。实际过程中会比这个少很多,因为会有结束条件提前结束。

3.结果

4.感想

2.注意什么时候要回溯,什么时候不回溯。

这里没有回溯,是因为我单独取了一个遍历来保存结果,所以没有回溯。

2.注意学会分析时间复杂度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码实现复原IP地址: ```java import java.util.*; public class RestoreIPAddresses { public List<String> restoreIpAddresses(String s) { List<String> res = new ArrayList<>(); if (s == null || s.length() < 4 || s.length() > 12) { return res; } dfs(s, "", 0, res); return res; } private void dfs(String s, String path, int index, List<String> res) { if (index == s.length() && countDots(path) == 3) { res.add(path); return; } if (countDots(path) > 3) { return; } for (int i = index; i < s.length(); i++) { String substr = s.substring(index, i + 1); if (isValid(substr)) { if (index == 0) { dfs(s, substr, i + 1, res); } else { dfs(s, path + "." + substr, i + 1, res); } } } } private boolean isValid(String s) { if (s == null || s.length() == 0 || s.length() > 3) { return false; } if (s.charAt(0) == '0' && s.length() > 1) { return false; } int num = Integer.parseInt(s); return num >= 0 && num <= 255; } private int countDots(String s) { int count = 0; for (char c : s.toCharArray()) { if (c == '.') { count++; } } return count; } } ``` 具体实现中,使用深度优先搜索(DFS)来枚举所有可能的IP地址。在DFS的过程中,使用一个path字符串来记录当前已经确定的IP地址,以及一个index变量来记录当前正在处理的字符串的下标。如果当前的path中已经有了4个数字,且整个字符串都被处理完了,则说明找到了一个合法的IP地址,将其加入到结果集中。如果当前path中的数字个数已经超过了4个,则说明这不是一个合法的IP地址,直接返回。否则,从index开始枚举所有可能的子串,如果当前子串是合法的,则递归处理后面的字符串,并将当前子串加入到path中。注意,如果是第一个数字,则不需要加上"."。在递归结束后,需要将当前子串从path中删除,以便处理其他可能的子串。isValid函数用于判断当前子串是否是合法的IP地址片段,countDots函数用于计算当前path中包含的"."的个数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值