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

468 Validate IP Address 验证IP地址的更多相关文章

  1. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  2. 华东师大OJ:IP Address【IP地址转换】

    /*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...

  3. [Leetcode] restore ip address 存储IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [LintCode] Restore IP Address 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. IP池验证IP是否可用 及scrapy使用 ip池

    简单验证 import requests url = "http://www.baidu.com/"proxies = {"http": "http: ...

  6. Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)

    <Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...

  7. VIP - virtual IP address

    virtual IP address (虚拟 IP 地址)1.是集群的ip地址,一个vip对应多个机器2.与群集关联的唯一 IP 地址 see wiki: A virtual IP address ( ...

  8. Java实现 LeetCode 468 验证IP地址

    468. 验证IP地址 编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址. IPv4 地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为 0 - 255, 用(&qu ...

  9. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. ES文件浏览器 WIFI 查看电脑文件怎么弄

    1 开启来宾账户   2 右击要共享的文件夹,添加Guest共享(如果只是要查看共享的资源,权限级别为读取即可)   3 共享之后,网络路径就是"\\"+你的计算机名+" ...

  2. 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件

    1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...

  3. 汝佳大神的紫书上写错了?uva10048

    算法竞赛入门经典第二版的365页例题11-5噪音.应该是"之和"换成"取最大值","取最小值"还是取最小值 假设我错了,请大家务必指点小弟 ...

  4. HDU4763 Theme Section 【KMP】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. Oracle学习(18)【DBA向】:分布式数据库

    分布式数据库 什么是分布数据库? l数据物理上被存放在网络的多个节点上,逻辑上是一个总体. 分布式数据库的独立性 l分布数据的独立性指用户不必关心数据怎样切割和存储,仅仅需关心他须要什么数据. Ora ...

  6. Android碎纸机效果

    1.总体思想 活用padding和margin 2.实现过程 public class PopupShredderView extends FrameLayout{ public PopupShred ...

  7. ASP.NET MVC中为DropDownListFor设置选中项的方法

    在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中.本篇只整理思路,不涉及完整代码. □ 思路 往前台视图传的类型是List< ...

  8. unix2dos/dos2unix

    dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文件是以\r\n作为断行标志的 ...

  9. Form content types

    Forms in HTML documents https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 17.13.4 Form con ...

  10. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...