LeetCode "468. Validate IP Address"
it is all about corner-cases...
class Solution(object):
def validIP4(self, IP):
def validNum4(s):
try:
# leading zero, negative
if (s[] == '' and len(s) > ) or s[] == '-':
return False
# value range
v = int(s)
if not (v >= and v <= ):
return False
except ValueError:
return False
return True arr = IP.split(".")
for s in arr:
if len(s) == :
return False
if not validNum4(s):
return False
return True def validIP6(self, IP):
def validNum6(s):
s = s.lower()
for c in s:
if not ((c >= '' and c <= '') or (c >= 'a' and c <= 'f')):
return False
return True arr = IP.split(":")
for s in arr:
if len(s) == or len(s) > :
return False if not validNum6(s):
return False
return True def validIPAddress(self, IP):
"""
:type IP: str
:rtype: str
"""
bIs4 = len(IP.split(".")) ==
if bIs4:
if self.validIP4(IP):
return "IPv4"
bIs6 = len(IP.split(":")) ==
if bIs6:
if self.validIP6(IP):
return "IPv6"
return "Neither"
LeetCode "468. Validate IP Address"的更多相关文章
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- Java实现 LeetCode 468 验证IP地址
468. 验证IP地址 编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址. IPv4 地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为 0 - 255, 用(&qu ...
- [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 ...
- Leetcode: Validate IP Address
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- [Swift]LeetCode468. 验证IP地址 | Validate IP Address
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither ...
- Java Regex match IP address
Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-ex ...
- LeetCode 1108. Defanging an IP Address (IP 地址无效化)
题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
随机推荐
- htop基本使用
一.什么是htop? top是所有类unix系统的必备工具,能直观方便的查看到系统负载.内存及进程等信息. 而htop具有top工具的全部功能且还新增了一些额外的功能和使用体验改进.与top相比,其具 ...
- CSS 中 Font-Family 中英文对照表
在 CSS 中,我们经常会使用 Font-Family 属性来定义字体.其中,中文字体如果直接使用中文名称,很有可能在非中文的系统环境下造成字体异常.所以通常使用字体的英文名称定义 Font-Fami ...
- dwr和spring的整合
1.dwr在spring配置文件的配置: <!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace --> <dwr:configuration /& ...
- Python 基礎 - 集合的使用
集合是一個無序的,不重複的數據組合,主要的作用如下 去重,把一個列表變成集合,就會自動去重了. 關係測試,測試二組數據之前的交集.差集.聯集等關係. 接下來我們來實作看看什麼是去重 #!/usr/bi ...
- php实现的笛卡儿积
之前在网上找到一个大牛写的版本(网址已经记不得了..),如下 function Descartes1() { //Cartesian product of arrays or strings or s ...
- PHP 输入流 php://input
在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组.所以,这里主要探讨php输入流php://input 对一php://in ...
- JavaScript 运动框架
<script> window.onload=function (){ var oDiv=document.getElementById("div1"); oDiv.o ...
- HTML5上传图片到ASP.NET.MVC
@{ ViewBag.Title = "Home Page";} <!DOCTYPE HTML PUBLIC><html><head> < ...
- cocoapods:安装/更新Ruby环境教程
简介 有时候在安装cocoapods时会产生如下错误 ERROR: Error installing cocoapods: activesupport requires Ruby version &g ...
- 推荐系统学习--cb+cf 初见
对于推荐系统的推出有两个条件:1.信息过载 ,2用户没有明确的需求 推荐系统算法中常见的有基于内容推荐,协同过滤推荐,协同过滤还可以分为基于人的协同过滤,基于内容协同过滤:社会推荐等 如何理解这些推荐 ...