详见: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. Windows 8.1更新变化

     在上个月微软公布了Windows 8.1更新(KB2919355),假设大家使用的是Windows 8.1的系统,而且启用了自己主动更新,那这个更新就会被自己主动安装.伴随着这个更新,微软同一时 ...

  2. InfoQ中文站特供稿件:Rust编程语言的核心部件

    本文为InfoQ中文站特供稿件.首发地址为: http://www.infoq.com/cn/articles/rust-core-components .如需转载.请与InfoQ中文站联系. 原文发 ...

  3. leetCode(26):Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  4. [读书笔记]《没人会告诉你的PPT真相》

    这本书分了三部分.第一部分偏重于基础技能,其中分为三部分,打印.放映.保存.第二部分是进阶,分为模板下载.模板修改.增加自定义页面等.第三部分是打造商业范的PPT,分为商业范的特征,具体技能体现(重复 ...

  5. java语法基础(二)

    流程控制语句 表达式语句 在表达式后面添加:就构成了表达式语句,简称“语句” 我们编写java代码,更多时候都是在书写表达式语句. int i;声明语句 i = 10;赋值语句 流程控制语句 流程控制 ...

  6. Linux文件锁【转】

    本文转载自:http://blog.csdn.net/dragon_li_chen/article/details/17147911 一.文件锁的分类: 翻阅参考资料,你会发现文件锁可以进行很多的分类 ...

  7. bzoj 3872 [ Poi 2014 ] Ant colony —— 二分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 从食蚁兽所在的边向叶节点推,会得到一个渐渐放大的取值区间,在叶子节点上二分有几群蚂蚁符 ...

  8. hdu 1480

    钥匙计数之二 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. bzoj4390

    树上差分 感觉挺巧妙的... 每次更新就是在u,v上+1,x是lca(u,v),在x和fa[x]上-1,那么每个点的权值就是子树和,正确性yy一下就行了 不过树状数组的常数真是小,改成前缀和才快了20 ...

  10. javascript复制内容到剪切板/网页上的复制按钮的实现

    javascript复制内容到剪切板/网页上的复制按钮的实现:DEMO如下 <!doctype html> <html> <head> <meta chars ...