繁体   English   中英

Leetcode - 去除 IP 地址

[英]Leetcode - Defanging IP address

我正在使用 C++ 对 Leetcode 进行IP defanging 挑战 以下是我对解决方案的尝试:

class Solution {
public:
    string defangIPaddr(string address) {
        for(int i = 0; i < address.size(); i++){
            if(address[i] == '.'){
                address.replace(i, 1, "[.]");
            }
        }
        return address;
    }
};

在这里,代码不能用"[.]"替换句点,而是返回 "Time Limit Exceeded"。

我尝试用"[]"替换它,它工作正常,对于所有其他可能的字符串组合也是如此。 我试过 escaping,但即使这样也行不通。

"[.]"是一些无法读取的特殊字符串吗? 我知道有一个解决方案可以迭代地连接字符串,但这不应该也可以吗?

当循环遇到第一个. , 它取代了. [.] ,从而增加字符串的size 但是循环没有调整i以考虑新插入的[ ,因此下一次迭代将看到相同的. 在那个新的[之后,一次又一次地执行相同的替换,一次又一次,无休止。 这就是您的解决方案超时的原因。

您需要确保将i推进到替换文本之外,例如:

class Solution {
public:
    string defangIPaddr(string address) {
        for(string::size_type i = 0; i < address.size(); ++i){
            if (address[i] == '.'){
                address.replace(i, 1, "[.]");
                i += 2; // <-- skip to ']', the subsequent ++i will then skip past it
            }
        }
        return address;
    }
};

现场演示

或者,使用while循环而不是for循环,然后您可以更轻松地决定每次增加多少i

class Solution {
public:
    string defangIPaddr(string address) {
        string::size_type i = 0;
        while (i < address.size()){
            if (address[i] == '.'){
                address.replace(i, 1, "[.]");
                i += 3;
            }
            else {
                ++i;
            }
        }
        return address;
    }
};

现场演示

就个人而言,我会在循环中使用std::string::find() ,例如:

class Solution {
public:
    string defangIPaddr(string address) {
        string::size_type i = 0;
        while ((i = address.find('.', i)) != string::npos){
            address.replace(i, 1, "[.]");
            i += 3;
        }
        return address;
    }
};

现场演示

让我们看一个示例输入bo.y i为 2 时, replace将字符串变为bo[.]y 然后,当i为 3 时,它会看到 pesky . 再次,并replace将其变为bo[[.]]y 这种情况一直发生,直到超时。

修复? 调用replace后增加i:

class Solution {
public:
    string defangIPaddr(string address) {
        for(int i = 0; i < address.size(); i++){
            if(address[i] == '.'){
                address.replace(i, 1, "[.]");
                i++;
            }
        }
        return address;
    }
};

正如雷米的回答中所解释的那样,您的问题在于循环计数器永远不会超过“新'.' 增加字符串长度时的字符。

尽管该答案本身是完美的,但我发现在任何字符串替换和/或替换操作中,避免尝试“内联”工作通常要简单得多 相反,制作副本通常要容易得多,必要时更换复制的部分:

class Solution {
public:
    string defangIPaddr(string address)
    {
        string answer{ "" }; // Local string to build the answer - start off empty.
        answer.reserve(address.size() * 2); // Reserve sufficient space to avoid multiple reallocation.
        for (size_t i = 0; i < address.size(); i++) {
            if (address[i] == '.') {
                answer += "[.]"; // For a dot, replace with the "[.]" string ...
            }
            else {
                answer += address[i]; // ... otherwsie, just copy the character.
            }
        }
        answer.shrink_to_fit(); // Free any unneeded memory.
        return answer; // We return BY VALUE, so a copy is made of the LOCAL "answer".
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM