当前位置:网站首页>Niuke network: verify the IP address
Niuke network: verify the IP address
2022-06-21 17:20:00 【lsgoose】



There are two ways .
Catalog
1. Task extraction
No matter ipv4 still ipv6, Our task is to split the string first , Then judge whether each string is legal .
therefore , We first write a function that splits a string with a certain character .
For the split string , To judge whether ipv4:
1. The number of strings should be 4
2. Each string must be 0-255 Between
3. Longer than 1 The first bit of the string of cannot be 0
To judge whether ipv6:
1. The number of strings should be 8
2. The characters in each string are either numbers , Or a-f,A-F The characters in
3. The length of each string cannot exceed 4
The code is as follows :
class Solution {
public:
/**
* verification IP Address
* @param IP string character string One IP Address string
* @return string character string
*/
const string IPv6="IPv6";
const string IPv4="IPv4";
vector<string> split(string s, string spliter){
vector<string> res;
int i;
while((i=s.find(spliter))&&i!=s.npos){
res.push_back(s.substr(0,i));
s=s.substr(i+1);
}
res.push_back(s);
return res;
}
bool isIPv4(string &IP){
vector<string> s=split(IP, ".");
if(s.size()!=4){
return false;
}
for(int i=0;i<s.size();++i){
if(s[i].size()==0){
return false;
}
if(s[i].size()>3 || (s[i].size()>0 && s[i][0]=='0')){
return false;
}
for(int j=0;j<s[i].size();++j){
if(!isdigit(s[i][j])){
return false;
}
}
int num=stoi(s[i]);
if(num<0 || num>255){
return false;
}
}
return true;
}
bool isIPv6(string &IP){
vector<string> s=split(IP, ":");
if(s.size()!=8){
return false;
}
for(int i=0;i<s.size();++i){
if(s[i].size()==0 || s[i].size()>4){
return false;
}
for(int j=0;j<s[i].size();++j){
if(!(isdigit(s[i][j]) || (s[i][j] >= 'a' && s[i][j] <= 'f') || (s[i][j] >= 'A' && s[i][j] <= 'F')))
return false;
}
}
return true;
}
string solve(string IP) {
if(IP.size()==0){
return "Neither";
}else if(isIPv4(IP)){
return IPv4;
}else if(isIPv6(IP)){
return IPv6;
}else{
return "Neither";
}
}
};2. Regular expressions
The patterns here can be matched with regular expressions :
class Solution {
public:
/**
* verification IP Address
* @param IP string character string One IP Address string
* @return string character string
*/
const string IPv6="IPv6";
const string IPv4="IPv4";
vector<string> split(string s, string spliter){
vector<string> res;
int i;
while((i=s.find(spliter))&&i!=s.npos){
res.push_back(s.substr(0,i));
s=s.substr(i+1);
}
res.push_back(s);
return res;
}
bool isIPv4(string &IP){
vector<string> s=split(IP, ".");
if(s.size()!=4){
return false;
}
for(int i=0;i<s.size();++i){
if(s[i].size()==0){
return false;
}
if(s[i].size()>3 || (s[i].size()>0 && s[i][0]=='0')){
return false;
}
for(int j=0;j<s[i].size();++j){
if(!isdigit(s[i][j])){
return false;
}
}
int num=stoi(s[i]);
if(num<0 || num>255){
return false;
}
}
return true;
}
bool isIPv6(string &IP){
vector<string> s=split(IP, ":");
if(s.size()!=8){
return false;
}
for(int i=0;i<s.size();++i){
if(s[i].size()==0 || s[i].size()>4){
return false;
}
for(int j=0;j<s[i].size();++j){
if(!(isdigit(s[i][j]) || (s[i][j] >= 'a' && s[i][j] <= 'f') || (s[i][j] >= 'A' && s[i][j] <= 'F')))
return false;
}
}
return true;
}
string solve(string IP) {
if(IP.size()==0){
return "Neither";
}else if(isIPv4(IP)){
return IPv4;
}else if(isIPv6(IP)){
return IPv6;
}else{
return "Neither";
}
}
};边栏推荐
猜你喜欢

Reinforcement learning introductory project spinning up (1) installation

《网络是怎么样连接的》读书笔记 - FTTH

Overseas new things | software developer "dynaboard" seed round raised US $6.6 million to develop low code platform to connect design, products and developers

Pytest框架实现前后置的处理

【毕业N年系列】 毕业第四年

Why do you want to develop tea mall applet app?

Yaml file details

QT5知识:字符串列表QStringListModel

建立自己的网站(11)

Show you how to distinguish several kinds of parallelism
随机推荐
使用unittest框架生成测试报告
Generating test reports using the unittest framework
牛客网:验证IP地址
[SQLite] résoudre le jeton non enregistré: ''
从北京“润”到芝加哥,工程师宝玉“滋润”成长的秘诀
使用 Guzzle 中间件进行优雅的请求重试
为什么要做茶叶商城小程序app开发?
第13周总结博客(校历第15周)动态规划总结
牛客网:大数加法
[ROS2 基础] Navigation2 导航系统介绍
[1108. Invalidation de l'adresse IP]
7 tips for writing effective help documents
【观察】微软“云+端”全面创新,让混合云更简单、更灵活、更安全
Sequence traversal of binary tree
[theory] - interface test
Pytest--生成测试报告
Online JSON to yaml tool
Postman basic operations
Résolution des erreurs signalées par qtcreator
期货农产品开户怎么开?手续费是多少?