题解 | #验证IP地址#

验证IP地址

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

思路很简单,就是把字符串遍历,然后想一些特殊case就可
C语言版本

/**
 * 验证IP地址
 * @param IP string字符串 一个IP地址字符串
 * @return string字符串
 */

int IPv4(char* IP)
{
    int tmp_ipv4 = 0;
    int count = 0;
    while(*IP)
    {
        while(*IP != '.' && *IP != '\000' && *IP != ':')
        {
            char* IP_tmp = IP;
            IP_tmp++;
            if(*IP == '0' && *IP_tmp != '0')
            {
                if(*IP_tmp == '.')
                {
                    IP++;
                    continue;
                }
                return 0;
            }
            tmp_ipv4 = tmp_ipv4*10+((int)(*IP)-48);
            IP++;
        }
        IP++;
        if(tmp_ipv4>=0 && tmp_ipv4<=255)
        {
            count++;
            tmp_ipv4 = 0;
        }
        else return 0;
    }
    if(count==4) return 1;
    return 0;
}

int IPv6(char* IP)
{
    int count_ipv6 = 0;
    int count_6 = 0;
    while(*IP)
    {
        while(*IP != ':' && *IP != '\000' && *IP != '.')
        {
            if(*IP >='a' && *IP <= 'e' || *IP >='A' && *IP <= 'E' || *IP >='0' && *IP <= '9')
            {
                count_ipv6++;
                IP++;
            }
            else
            {
                return 0;
            }
        }
        count_6++;
        if(count_ipv6 == 4)
        {
            IP++;
            count_ipv6=0;
        }
        else if(count_ipv6 < 4 && count_ipv6>0 && *IP == ':') 
        {
            IP++;
            count_ipv6=0;
        }
        else return 0;
    }
    if(count_6==8) return 1;
    return 0;
}
char* solve(char* IP ) {
    // write code here
    int res_4 = IPv4(IP);
    if(res_4==1) return "IPv4";
    int res_6 = IPv6(IP);
    if(res_6==1) return "IPv6";
    return "Neither";
}

C++版本

class Solution {
public:
    /**
     * 验证IP地址
     * @param IP string字符串 一个IP地址字符串
     * @return string字符串
     */
int IPv4(string IP_N)
{
    int tmp_ipv4 = 0;
    int count = 0;
    char* IP = &IP_N[0];
    int lenth = 4;
    while(lenth)
    {
        while(*IP != '.' && *IP != ':' && *IP != '\000')
        {
            char* IP_tmp = IP;
            IP_tmp++;
            if(*IP == '0' && *IP_tmp != '0')
            {
                if(*IP_tmp == '.')
                {
                    IP++;
                    continue;
                }
                return 0;
            }
            tmp_ipv4 = tmp_ipv4*10+((int)(*IP)-48);
            IP++;
        }
        IP++;
        lenth--;
        if(tmp_ipv4>=0 && tmp_ipv4<=255)
        {
            count++;
            tmp_ipv4 = 0;
        }
        else return 0;
    }
    if(count==4) return 1;
    return 0;
}

int IPv6(string IP_N)
{
    int count_ipv6 = 0;
    int count_6 = 0;
    char* IP = &IP_N[0];
    int lenth = 8;
    while(lenth)
    {
        while(*IP != ':' && *IP != '\000' && *IP != '.')
        {
            if(*IP >='a' && *IP <= 'e' || *IP >='A' && *IP <= 'E' || *IP >='0' && *IP <= '9')
            {
                count_ipv6++;
                IP++;
            }
            else
            {
                return 0;
            }
        }
        count_6++;
        lenth--;
        if(count_ipv6 == 4)
        {
            IP++;
            count_ipv6=0;
        }
        else if(count_ipv6 < 4 && count_ipv6>0 && *IP == ':')
        {
            IP++;
            count_ipv6=0;
        }
        else return 0;
    }
    if(count_6==8) return 1;
    return 0;
}

    string solve(string IP) {
        // write code here
        int res_4 = IPv4(IP);
        if(res_4==1) return "IPv4";
        int res_6 = IPv6(IP);
        if(res_6==1) return "IPv6";
        return "Neither";
    }
};
牛客刷题记录 文章被收录于专栏

记录自己的刷题记录,刷过的题的解法

全部评论

相关推荐

11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务