当前位置:网站首页>468. verifying the IP address
468. verifying the IP address
2022-06-12 06:00:00 【Taoist scholar】
link
subject
Given a string queryIP. If it works IPv4 Address , return "IPv4" ; If it works IPv6 Address , return "IPv6" ; If not of the above type IP Address , return "Neither" .
Effective IPv4 Address yes “x1.x2.x3.x4” Formal IP Address . among 0 <= xi <= 255 And xi Can not contain Leading zero . for example : “192.168.1.1” 、 “192.168.1.0” To be effective IPv4 Address , “192.168.01.1” Is invalid IPv4 Address ; “192.168.1.00” 、 “[email protected]” Is invalid IPv4 Address .
A valid IPv6 Address Is a format of “x1:x2:x3:x4:x5:x6:x7:x8” Of IP Address , among :
1 <= xi.length <= 4
xi It's a Hexadecimal string , It can contain numbers 、 Small letters ( 'a' To 'f' ) And capital letters ( 'A' To 'F' ).
stay xi Leading zeros are allowed in .
for example "2001:0db8:85a3:0000:0000:8a2e:0370:7334" and "2001:db8:85a3:0:0:8A2E:0370:7334" It works IPv6 Address , and "2001:0db8:85a3::8A2E:037j:7334" and "02001:0db8:85a3:0000:0000:8a2e:0370:7334" It's invalid IPv6 Address .
Example
Example 1:
Input :queryIP = "172.16.254.1"
Output :"IPv4"
explain : Effective IPv4 Address , return "IPv4"Example 2:
Input :queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output :"IPv6"
explain : Effective IPv6 Address , return "IPv6"Example 3:
Input :queryIP = "256.256.256.256"
Output :"Neither"
explain : neither IPv4 Address , Not yet IPv6 Address
explain
queryIP Only by English letters , Numbers , character '.' and ':' form .
Ideas
The idea of the topic itself is not difficult , Is to process strings , But it's complicated , Extra care is needed . Here are a few things to pay attention to :
1. about IPv4 Cannot contain a preamble 0 The problem of , image 192.0.0.1 Like this 0 It can exist .
2. For a correct IPv4, At last, there was an extra . Or a right one IPv6 There is one more on the last side : The situation of
C++ Code
class Solution {
public:
bool checkIPv4(string s){
int index=0, N=0;
while(index<s.size())
{
string temp=""; // Create a new empty string to store each number
if( !('0'<=s[index] && s[index]<='9')) return false; // Encounter non number return false
else if (s[index]=='0' && !(index==s.size()-1 || s[index+1]=='.')) return false; // When the number is 0, And the back is not . Or not the last character
else // Encountered the first number
{
temp+=s[index];// First, add the first number to the string
index++; // Then judge the next character
while(index<s.size() && '0'<=s[index] && s[index]<='9') // If it's a number
{
temp+=s[index]; index++;
}
if(index!=s.size() && s[index]!='.') return false; // It is not a legal character or . false
N++; // Number of segments ++
int num=stoi(temp); // Convert the previous string to a number
if(num<0 || num>255) return false;
if(N==4 && s[index]=='.') return false;
}
index++;
}
return N==4; // Finally, judge whether it is 4 paragraph
}
bool checkIPv6(string s){
int index=0, N=0;
while(index<s.size())
{
string temp=""; // Create a new empty string to store each number
if(! ( ('0'<=s[index] && s[index]<='9') || ('a'<=s[index] && s[index]<='f') || ('A'<=s[index] && s[index]<='F'))) return false;// Is not a specified legal character
else
{
temp+=s[index];// First, add the first number to the string
index++; // Then judge the next character
while(('0'<=s[index] && s[index]<='9') || ('a'<=s[index] && s[index]<='f') || ('A'<=s[index] && s[index]<='F')) // If it is a legal character
{
temp+=s[index]; index++;
}
if(index!=s.size() && s[index]!=':') return false; // It is not a legal character or : false
N++;
if(!(temp.size()>=1 && temp.size()<=4)) return false;
if(N==8 && s[index]==':') return false; // Prevent a correct IPv6 There is an extra after the address :
}
index++;
}
return N==8;
}
string validIPAddress(string queryIP) {
if(queryIP.size()<7 || queryIP.size()>39) return "Neither";
if(checkIPv4(queryIP)==true) return "IPv4";
if(checkIPv6(queryIP)==true) return "IPv6";
return "Neither";
}
};边栏推荐
- Understanding of distributed transactions
- 肝了一个月的 DDD,一文带你掌握
- 三年磨一剑:蚂蚁金服的研发效能洞察实践
- Glossary of Chinese and English terms for pressure sensors
- Leetcode-1260. 2D mesh migration
- What is the lszrz protocol used at ordinary times? Talk about xmodem/ymodem/zmodem
- Json-c common APIs
- 为什么联合索引是最左匹配原则?
- flex/fixed上中下(移动端)
- Individual application for ov type SSL certificate
猜你喜欢

IO stream introduction

A month's worth of DDD will help you master it

GRP development: four communication modes of GRP

Conversion of Halcon 3D depth map to 3D image

Introduction to Internet Protocol

March 22, 2021

Introduction to redis high availability

Leetcode-1043. Separate arrays for maximum sum

Who is more fierce in network acceleration? New king reappeared in CDN field

Redis transaction
随机推荐
Leetcode-717. 1-bit and 2-bit characters (O (1) solution)
Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]
User login [next]
CCF noi2022 quota allocation scheme
[C language basics] macro definition usage
Select gb28181, RTSP or RTMP for data push?
[Speech] 如何根据不同国家客制化ring back tone
March 22, 2021
Leetcode 第 80 場雙周賽題解
[machine learning] first day of introduction
yolov5
How to split a row of data into multiple rows in Informix database
Jpg format and XML format files are separated into different folders
How much Ma is the driving current of SIM card signal? Is it adjustable?
China's elastic belt market trend report, technical dynamic innovation and market forecast
Memory model, reference and function supplement of program
Who is more fierce in network acceleration? New king reappeared in CDN field
IO to IO multiplexing from traditional network
[grpc development] go language builds simple server and client
Divide a folder image into training set and test set