题解 | #验证IP地址#

验证IP地址

https://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880

class Solution {
  public:
    /**
     * 验证IP地址
     * @param IP string字符串 一个IP地址字符串
     * @return string字符串
     */
    string nth = "Neither";
    string v4 = "IPv4";
    string v6 = "IPv6";

    bool isLegal(string str, int mode) {
        bool leg = false;
        switch (mode) {
            case 4:
                if (str.size() > 1 && str[0] == '0')
                    leg = false;
                else if (stoi(str) > 255 || stoi(str) < 0)
                    leg = false;
                else {
                    for (auto i : str)
                        if (!(i >= '0' && i <= '9'))
                            return false;
                    leg = true;
                }
                break;
            case 6:
                if (str.size() > 4)
                    leg = false;
                else {
                    for (auto i : str)
                        if (!((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F')))
                            return false;
                    leg = true;
                }
                break;
            default:
                break;
        }
        return leg;
    }

    string cal4Or6(string s, vector<int>& ilist, int n, int calV4Or6) {
        if (ilist.size() != n)
            return nth;
        int index = 0;
        vector<string> strVc;
        for (int i = 0; i < ilist.size(); i++) {
            if (i < ilist.size() - 1)
                if ((ilist[i + 1] - ilist[i]) == 1)
                    return nth;
            strVc.push_back(s.substr(index, ilist[i] - index));
            index = ilist[i] + 1;
        }
        strVc.push_back(s.substr(index, ilist.size() - index));
        for (auto i : strVc) {
            if (!isLegal(i, calV4Or6))
                return nth;
        }
        return calV4Or6 == 4 ? v4 : v6;
    }

    string solve(string IP) {
        string s = nth;
        if (IP.length() < 7)
            return s;
        int i = 0;
        bool isv4 = false;
        bool isv6 = false;
        vector<int> ilist;
        while (i < IP.length()) {
            if (IP[i] == ':' || IP[i] == '.') {
                if (IP[i] == '.')
                    isv4 = true;
                else
                    isv6 = true;
                ilist.push_back(i);
            }
            i++;
        }
        if (isv4)
            s = cal4Or6(IP, ilist, 3, 4); // 3个.
        else if (isv6)
            s = cal4Or6(IP, ilist, 7, 6); // 7个:
        return s;
    }
};

全部评论

相关推荐

那一天的Java_Java起来:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务