468 Validate IP Address 验证IP地址
2024-09-08 05:19:20
详见:https://leetcode.com/problems/validate-ip-address/description/
Java实现:
class Solution {
public String validIPAddress(String IP) {
if (isIpV4(IP)) {
return "IPv4";
} else if (isIpV6(IP)) {
return "IPv6";
} else {
return "Neither";
}
}
private boolean isIpV4(String ip) {
if (ip == null || ip.isEmpty()) {
return false;
}
String[] splits = ip.split("\\.", -1);
if (splits.length != 4){
return false;
}
for (int i = 0; i < 4; i++) {
try {
int val = Integer.parseInt(splits[i], 10);
if (val < 0 || val > 255){
return false;
}
} catch (Exception e) {
return false;
}
if (splits[i].charAt(0) == '-' || splits[i].charAt(0) == '+'){
return false;
}
if (splits[i].charAt(0) == '0' && splits[i].length() > 1){
return false;
}
}
return true;
}
private boolean isIpV6(String ip) {
if (ip == null || ip.isEmpty()) {
return false;
}
String[] splits = ip.split(":", - 1);
if (splits.length != 8){
return false;
}
for (int i = 0; i < 8; i++) {
try {
int val = Integer.parseInt(splits[i], 16);
if (val < 0 || val > 65535){
return false;
}
} catch (Exception e) {
return false;
}
if (splits[i].charAt(0) == '-' || splits[i].charAt(0) == '+'){
return false;
}
if (splits[i].length() > 4){
return false;
}
}
return true;
}
}
C++实现:
class Solution {
public:
string validIPAddress(string IP)
{ if(IsIPv4(IP))
{
return "IPv4";
}
else if(IsIPv6(IP))
{
return "IPv6";
}
else
{
return "Neither";
}
} private:
bool IsIPv4(string IP)
{
string temp="";
int count=0;
int count1=0;
for(int i=0;i<=IP.length();i++)
{
if(IP[i]!='.'&&IP[i]!='\0')
{
if(IP[i]<'0'||IP[i]>'9')
{
return false;
}
temp=temp+IP[i];
count1++;
if(count1>=4)
{
return false;
}
}
else
{
if(temp!=""&&stoi(temp)<256&&stoi(temp)>=0)
{
if(temp[0]=='0'&&temp.length()>1)
{
return false;
}
count++;
if(count==4)
{
return i==IP.length();
}
}
else
{
return false;
}
temp="";
count1=0;
}
}
return false;
}
bool IsIPv6(string IP)
{
string temp="";
int count=0;
int count1=0;
for(int i=0;i<=IP.length();i++)
{
if((IP[i]>='0'&&IP[i]<='9')||(IP[i]>='A'&&IP[i]<='F')||(IP[i]>='a'&&IP[i]<='f'))
{
count1++;
if(count1>4)
{
return false;
}
}
else if(IP[i]==':'||IP[i]=='\0')
{
if(count1==0)
{
return false;
}
count++;
count1=0;
if(count==8)
{
return i==IP.length();
}
}
else
{
return false;
} }
return false;
} };
参考:https://blog.csdn.net/starstar1992/article/details/54925098
最新文章
- php如何防止图片盗用/盗链的两种方法(转)
- 基本的HTML标签
- 并查集——HDOJ-1232-畅通工程
- asp.net解决高并发的方案.
- Asp.Net中的session配置
- HTML5开发入门经典教程和案例合集(含视频教程)
- c++ 17介绍
- <;META http-equiv=Content-Type content=";text/html; charset=gb2312";>;
- linux安装配置solr
- 《javascript语言精粹》——第6章数组
- 2017-3-2 C#基础 集合
- HTTP文件下载JAVA后台的实现
- python实现 多叉树 寻找最短路径
- scrapy-redis 分布式爬虫
- 从头开始学Maven【仓库】
- ubuntu 1604安装docker-ce 记录
- zip文件解压工具类
- 运行VsCode缺少libxss.so.1
- luogu P1357 花园
- pycharm 运行py文件一直updating indexing
热门文章
- composer-安装插件包
- List<;Guid?>; a = new List<;Guid?>;();
- Saltstack运行cmd.run重新启动tomcat后出现日志乱码(15)
- Android之——AIDL深入
- Python爬虫开发【第1篇】【Scrapy shell】
- Hadoop 解除 “Name node is in safe mode”
- HDU1281 棋盘游戏 —— 二分图最大匹配 + 枚举
- JavaScript 在浏览器环境中的模块管理
- Tyvj:1729 文艺平衡树(saply练习)
- AutoIT: 下载文件函数