详见: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

最新文章

  1. php如何防止图片盗用/盗链的两种方法(转)
  2. 基本的HTML标签
  3. 并查集——HDOJ-1232-畅通工程
  4. asp.net解决高并发的方案.
  5. Asp.Net中的session配置
  6. HTML5开发入门经典教程和案例合集(含视频教程)
  7. c++ 17介绍
  8. &lt;META http-equiv=Content-Type content=&quot;text/html; charset=gb2312&quot;&gt;
  9. linux安装配置solr
  10. 《javascript语言精粹》——第6章数组
  11. 2017-3-2 C#基础 集合
  12. HTTP文件下载JAVA后台的实现
  13. python实现 多叉树 寻找最短路径
  14. scrapy-redis 分布式爬虫
  15. 从头开始学Maven【仓库】
  16. ubuntu 1604安装docker-ce 记录
  17. zip文件解压工具类
  18. 运行VsCode缺少libxss.so.1
  19. luogu P1357 花园
  20. pycharm 运行py文件一直updating indexing

热门文章

  1. composer-安装插件包
  2. List&lt;Guid?&gt; a = new List&lt;Guid?&gt;();
  3. Saltstack运行cmd.run重新启动tomcat后出现日志乱码(15)
  4. Android之——AIDL深入
  5. Python爬虫开发【第1篇】【Scrapy shell】
  6. Hadoop 解除 “Name node is in safe mode”
  7. HDU1281 棋盘游戏 —— 二分图最大匹配 + 枚举
  8. JavaScript 在浏览器环境中的模块管理
  9. Tyvj:1729 文艺平衡树(saply练习)
  10. AutoIT: 下载文件函数