93. 复原 IP 地址

原题链接:

93. 复原 IP 地址

https://leetcode.cn/problems/restore-ip-addresses/description/

完成情况:

在这里插入图片描述

解题思路:

This Java solution is designed to generate all possible valid IP addresses from a given string s. The code utilizes a depth-first search (DFS) strategy to explore all potential segmentations of the input string into four parts, each representing an octet of an IP address. Let’s break down the implementation and the key components of this solution.

Key Components and Logic

  1. Class Members:

    • List<String> result: A list to store the resulting valid IP addresses.
    • static final int SEGCOUNT = 4: A constant to represent the number of segments in an IP address.
    • int[] SEGMENTS = new int[SEGCOUNT]: An array to store the current segments of the IP address being formed.
  2. Main Method (restoreIpAddresses):

    • Takes the input string s.
    • Initiates the DFS with the starting segment number (segNums = 0) and starting index (segIndex = 0).
    • Returns the list of valid IP addresses.
  3. DFS Method (dfs_restoreIpAddresses):

    • Base Case: If the number of segments equals SEGCOUNT and the end of the string is reached, it forms a valid IP address.
    • Edge Case: If the end of the string is reached before forming 4 segments, it returns early.
    • Handling Leading Zeroes: If a segment starts with ‘0’, only ‘0’ is considered as a valid segment.
    • General Case: It forms segments by considering each substring from the current index to the next positions, ensuring each segment is within the valid range (0-255).

Detailed Breakdown

Main Method
public List<String> restoreIpAddresses(String s) {
    dfs_restoreIpAddresses(s, 0, 0);
    return result;
}
  • Calls the dfs_restoreIpAddresses method to start the DFS traversal.
  • Returns the result list containing all valid IP addresses.
DFS Method
private void dfs_restoreIpAddresses(String s, int segNums, int segIndex) {
    // Base case: If four segments are formed
    if (segNums == SEGCOUNT) {
        if (segIndex == s.length()) {
            StringBuilder ipAddress = new StringBuilder();
            for (int i = 0; i < SEGCOUNT; ++i) {
                ipAddress.append(SEGMENTS[i]);
                if (i < SEGCOUNT - 1) {
                    ipAddress.append('.');
                }
            }
            result.add(ipAddress.toString());
        }
        return;
    }
    
    // Edge case: If the string is exhausted before forming 4 segments
    if (segIndex == s.length()) {
        return;
    }

    // Handling leading zeroes
    if (s.charAt(segIndex) == '0') {
        SEGMENTS[segNums] = 0;
        dfs_restoreIpAddresses(s, segNums + 1, segIndex + 1);
        return;
    }

    // Form segments and proceed with DFS
    int addr = 0;
    for (int segNext = segIndex; segNext < s.length(); segNext++) {
        addr = addr * 10 + (s.charAt(segNext) - '0');
        if (addr > 0 && addr <= 255) {
            SEGMENTS[segNums] = addr;
            dfs_restoreIpAddresses(s, segNums + 1, segNext + 1);
        } else {
            return;
        }
    }
}

Key Points

  • Base Case Handling: Ensures that exactly 4 segments are formed and the entire string is consumed.
  • Leading Zeroes: Special case handling where ‘0’ can only be a segment by itself.
  • Valid Range Check: Each potential segment is validated to be within the range [0, 255].
  • DFS Traversal: Recursively explores all possible segmentations by adjusting segment numbers and string indices.

This algorithm ensures that all valid combinations are checked systematically and invalid cases are pruned early, leading to efficient generation of valid IP addresses from the given string.

参考代码:

_93复原IP地址_回溯

package leetcode板块;

import java.util.ArrayList;
import java.util.List;

public class _93复原IP地址_回溯 {
    List<String> result = new ArrayList<String>();
    static final int SEGCOUNT = 4;  //必须要保证有四个部分
    int [] SEGMENTS = new int[SEGCOUNT];  // 构建一个数组用做记录每次的IP

    /**
     * 复原所有可能的IP情况
     * @param s
     * @return
     */
    public List<String> restoreIpAddresses(String s) {
        dfs_restoreIpAddresses(s,0,0);
        return result;
    }

    /**
     *
     * @param s     字符串
     * @param segNums   ip四个区间的当前数量
     * @param segIndex  当前所用到的长度索引位置
     */
    private void dfs_restoreIpAddresses(String s, int segNums, int segIndex) {
        // 终止条件,且是找到了四个地址
        if (segNums == SEGCOUNT){
            //查看当前选择下,是否能够满足题意,,,,
                //如果当前长度全部使用完毕,则说明满足条件,对于数值的判断就不放在这里进行
            if (segIndex  == s.length()){
                StringBuilder ipAddress = new StringBuilder();
                for (int i = 0;i<SEGCOUNT;++i){
                    ipAddress.append(SEGMENTS[i]);
                    if (i < SEGCOUNT-1){
                        //添加  .
                        ipAddress.append('.');
                    }
                }
                result.add(ipAddress.toString());
            }
            return;
        }
        //如果是没找到4个地址,但是长度用完了,则直接return
        if (segIndex == s.length()){
            return;
        }

        // TODO ----------------  开始进行一般化处理-----------------
        //  1.特殊情况   以 0 开头,就没有其他的方案可供选择,只能0单独作为一个IP地址
        if (s.charAt(segIndex) == '0'){
            SEGMENTS[segNums] = 0;
            dfs_restoreIpAddresses(s,segNums+1,segIndex+1);
        }

        // 其他情况的话则要进行分类讨论
        int addr = 0;
        //  需要考虑后面的是否也能够满足条件,不会对别人造成无法满足条件的情况
        for (int segNext = segIndex;segNext < s.length();segNext++){
            addr = addr * 10 + (s.charAt(segNext) - '0');
            if (addr > 0 && addr <= 255){
                SEGMENTS[segNums] = addr;
                dfs_restoreIpAddresses(s,segNums+1,segNext+1);
            }else {
                return;
            }
        }
    }
}

错误经验吸取

  • 20
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的解法: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 12 int isValid(char* s, int start, int end) { if (start > end) { return 0; } if (s[start] == '0' && start != end) { return 0; } int num = 0; for (int i = start; i <= end; i++) { if (s[i] < '0' || s[i] > '9') { return 0; } num = num * 10 + (s[i] - '0'); if (num > 255) { return 0; } } return 1; } void backtrack(char* s, int len, int start, int depth, char* path, char** res, int* returnSize) { if (depth == 4) { if (start == len) { path[strlen(path) - 1] = '\0'; // 去掉最后一个点 res[*returnSize] = (char*) malloc(sizeof(char) * (strlen(path) + 1)); strcpy(res[*returnSize], path); (*returnSize)++; } return; } for (int i = start; i < start + 3 && i < len; i++) { if (isValid(s, start, i)) { char* temp = (char*) malloc(sizeof(char) * (MAX_LEN + 2)); sprintf(temp, "%s%d.", path, atoi(s + start)); backtrack(s, len, i + 1, depth + 1, temp, res, returnSize); free(temp); } } } char** restoreIpAddresses(char* s, int* returnSize) { int len = strlen(s); char** res = (char**) malloc(sizeof(char*) * 5000); *returnSize = 0; if (len < 4 || len > 12) { return res; } char* path = (char*) malloc(sizeof(char) * (MAX_LEN + 2)); backtrack(s, len, 0, 0, path, res, returnSize); free(path); return res; } int main() { int returnSize; char* s = "25525511135"; char** res = restoreIpAddresses(s, &returnSize); for (int i = 0; i < returnSize; i++) { printf("%s\n", res[i]); free(res[i]); } free(res); return 0; } ``` 解题思路和 Python 版本相同,这里就不再赘述。需要注意的是,在 C 语言中,字符串长度是不包括结尾的空字符 `\0` 的,因此每个字符串的申请空间要比 Python 版本多加 1。同时,由于 C 语言不支持字符串加法,所以要使用 `sprintf` 函数将数字转换为字符串后再拼接。最后,记得释放申请的内存。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值