2
头图

Recovery IP address

Title description: Given a string containing only numbers to represent an IP address, return all valid IP addresses that may be obtained from s. You can return the answers in any order.

A valid IP address consists of exactly four integers (each integer is between 0 and 255, and cannot contain leading 0), and the integers are separated by'.'.

For example: "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/restore-ip-addresses/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Backtracking algorithm
The IP address is divided into 4 segments, the value of each segment is recorded, and then the processing starts from the first segment, recursively judging whether the subsequent IP addresses meet the conditions.
import java.util.ArrayList;
import java.util.List;

public class LeetCode_093 {
    // IP地址一共有4段
    private static final int SEG_COUNT = 4;
    // IP地址4段的值分别是多少
    private static int[] segments;
    // 结果集
    private static List<String> result = new ArrayList<>();

    public static List<String> restoreIpAddresses(String s) {
        segments = new int[SEG_COUNT];
        dfs(s, 0, 0);
        return result;
    }

    /**
     * 回溯算法
     *
     * @param s        原始字符串
     * @param segId    当前是第几段IP地址
     * @param segStart 当前第segId段IP地址的起始位置
     */
    private static void dfs(String s, int segId, int segStart) {
        // 如果找到了4段IP地址并且遍历完了字符串,则说明当前结果是一种符合条件的IP地址
        if (segId == SEG_COUNT) {
            if (segStart == s.length()) {
                StringBuffer ipAddr = new StringBuffer();
                for (int i = 0; i < SEG_COUNT; ++i) {
                    ipAddr.append(segments[i]);
                    if (i != SEG_COUNT - 1) {
                        ipAddr.append('.');
                    }
                }
                result.add(ipAddr.toString());
            }
            return;
        }

        // 如果还没有找到4段IP地址,字符串就已经遍历完了,则提前退出
        if (segStart == s.length()) {
            return;
        }

        // 由于不能有前导0,如果当前数字为0,那么这一段IP地址只能是0
        if (s.charAt(segStart) == '0') {
            segments[segId] = 0;
            dfs(s, segId + 1, segStart + 1);
        }

        // 一般情况下,每一种可能都要考虑
        int addr = 0;
        for (int segEnd = segStart; segEnd < s.length(); ++segEnd) {
            addr = addr * 10 + (s.charAt(segEnd) - '0');
            if (addr > 0 && addr <= 0xFF) {
                segments[segId] = addr;
                dfs(s, segId + 1, segEnd + 1);
            } else {
                break;
            }
        }
    }

    public static void main(String[] args) {
        for (String restoreIpAddress : restoreIpAddresses("101023")) {
            System.out.println(restoreIpAddress);
        }
    }
}
[Daily Message] People cannot let themselves eat too much. When you are hungry, you want to fight with others, so I often warn myself not to eat too much, and must go out to fight.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!