您的位置:首页 > 其它

LeetCode 题解(Week 14):468. Validate IP Address

2017-06-05 15:57 363 查看

原题目

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.

IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots (“.”), e.g.,172.16.254.1;

Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.

IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (“:”). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).

However, we don’t replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.

Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.

Note: You may assume there is no extra space or special characters in the input string.

Example 1:

Input: “172.16.254.1”

Output: “IPv4”

Explanation: This is a valid IPv4 address, return “IPv4”.

Example 2:

Input: “2001:0db8:85a3:0:0:8A2E:0370:7334”

Output: “IPv6”

Explanation: This is a valid IPv6 address, return “IPv6”.

Example 3:

Input: “256.256.256.256”

Output: “Neither”

Explanation: This is neither a IPv4 address nor a IPv6 address.

中文大意

IPv4是通过点分十进制来表示的,而IPV6是通过16进制数来表示的,现给出一个字符串,判断这个字符串是否是有效的IPv4或者是有效的IPv6字符串。

题解:

class Solution {
public:
bool isNum(string str)
{
for(int i =0 ; i < str.size() ;i++)
if(!isdigit(str[i])) return false;

return true;
}

bool isValidHex(string str)
{
for(int i = 0 ; i < str.size(); i++)
{
if(isdigit(str[i])) continue;
else if(!isalpha(str[i]) || tolower(str[i])>'f') return false;
}

return true;
}

void split(string s, string delimiters, vector<string>& splitedString)
{
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = s.find_first_of( delimiters, current );
splitedString.push_back( s.substr(current, next - current) );
}
while (next != string::npos);
}

bool isValidIPv4(vector<string> ipv4)
{
if(ipv4.size()!=4) return false;
for(int i = 0 ;i < ipv4.size() ;i++)
{
if(ipv4[i].empty() || !isNum(ipv4[i])) return false;
int curr = atoi(ipv4[i].c_str());
if(curr < 0 || curr > 255) return false;
if(ipv4[i][0]=='0' && ipv4[i].size()>1) return false;
}
return true;
}

bool isValidIPv6(vector<string> ipv6)
{
if(ipv6.size()!=8) return false;
for(int i = 0 ;i < ipv6.size() ;i++)
{
if(ipv6[i].empty() || !isValidHex(ipv6[i]) ) return false;
int curr;
sscanf(ipv6[i].c_str(), "%x", &curr);  // 十六进制(字符串)转十进制数
if(curr < 0 || curr > 65535) return false;
if(ipv6[i].size()>4) return false;
}
return true;
}

string validIPAddress(string IP) {
string dot = ".";
string colon = ":";

vector<string> ipv4;
vector<string> ipv6;

split(IP,dot,ipv4);
split(IP,colon,ipv6);

if(isValidIPv4(ipv4)) return "IPv4";
else if(isValidIPv6(ipv6)) return "IPv6";
else return "Neither";
}
};


思路:

给出字符串之后,首先分别通过两个分割符”.”和”:”对字符进行分割,然后根据分割的结果进行判断。将分割后的子串通过一个vector存起来,这样的话,就得到的两个vector,分别记为ipv4和ipv6

满足以下条件的ipv4有效:

size为4,并且每一个元素都非空

ipv4中的每一个元素中的每一个字符都是数字

ipv4中的每一个元素转换为10进制数后都大于等于0,小于或等于255

如果ipv4中有元素是0开头的,那么它的size只能为0(不能有多余的0开头)

满足以下条件的ipv6有效

size为8,且每一个元素都非空

每个元素中的每个字符要么是数字,要么是a到f的字母

每一个元素转换为10进制数后都大于等于0或小于等于65535(216−1)

每一个元素的长度不能超过4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode