[LeetCode]46. 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)

Subscribe to see which companies asked this question

解法1:暴力破解法。三层循环确定前面三个数字,最后剩下的所有作为第四个数字。实际每层循环最多执行三遍。循环中使用作为正确IP地址的条件进行剪枝。需要注意考虑各种边界条件。

class Solution {
public:
    vector<string> restoreIpAddresses(string s) {
        int n = s.size();
        string addr = "";
        vector<string> res;
        for (int i = 0; i < n - 3 && i < 3; ++i)
        {
            string fir(s.begin(), s.begin() + i + 1);
            if ((fir[0] != ‘0‘ || fir.size() == 1) && stoi(fir) <= 255)
            {
                for (int j = i + 1; j < n - 2 && j < i + 4; ++j)
                {
                    string sec(s.begin() + i + 1, s.begin() + j + 1);
                    if ((sec[0] != ‘0‘ || sec.size() == 1) && stoi(sec) <= 255)
                    {
                        for (int k = j + 1; k < n - 1 && k < j + 4; ++k)
                        {
                            string trd(s.begin() + j + 1, s.begin() + k + 1);
                            if ((trd[0] != ‘0‘ || trd.size() == 1) && stoi(trd) <= 255)
                            {
                                string fth(s.begin() + k + 1, s.end());
                                if ((fth[0] != ‘0‘ || fth.size() == 1) && fth.size() < 4 && stoi(fth) <= 255)
                                {
                                    addr += fir + ‘.‘ + sec + ‘.‘ + trd + ‘.‘ + fth;
                                    res.push_back(addr);
                                    addr.clear();
                                }
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
};
时间: 2024-08-09 06:21:25

[LeetCode]46. Restore IP Addresses复原IP地址的相关文章

[LeetCode] 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地址,IP地

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还原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 -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[LeetCode]Restore IP Addresses

题目:Restore IP Addresses 将数字串转换成合法的IP地址,返回所有可能的情况. 思路: 移动3个地址分隔符".",将地址换分成四份,检查是否合法: 注意不能有0开头的地址段(它是非法的),且不用将开头的0去掉. 中间的点从第一个点的后面开始向尾部移动,当第一个点与第二个点的距离大于3时,第一个点向后移动一步,第二个点还原到当前第一个点的后面: 当第一个点移动到最后一个点的前2个位置,则,最后一个点向前移动一步,第一个点和第二个点还原: 最后一个点到串尾距离大于3则退

【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

Restore IP Addresses leetcode java

题目: 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】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

LeetCode: Restore IP Addresses [093]

[题目] 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) [题意] 给定一个字