- 设计思想:
-视频讲解链接B站视频讲解
- 复杂度分析:
- 代码:
c++版本:
class Solution {
public:
/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
vector<string> split(string s,string spliter){
vector<string> ans;
int it;
while ((it = s.find(spliter)) && it != s.npos) {
ans.push_back(s.substr(0, it));
s = s.substr(it + 1);
}
ans.push_back(s);
return ans;
}
bool isIPv4(string IP){
///长度小于7开头或结尾为.都返回false
if (IP.size() < 7 || IP[0] == '.' || IP[IP.size() - 1] == '.') {
return false;
}
//按照.切分
vector<string> str = split(IP, ".");
//切割后的长度不够4返回false
if(str.size() != 4) return false;
for(int i = 0;i < str.size();i ++){
//开头第一个不能是0
if(str[i].size() == 0 || str[i][0] =='0' && str[i].size() > 1 || str[i].size() > 3){
return false;
}
int num = 0;//转换数字
for(int j = 0;j < str[i].size();j ++){
char c = str[i][j];
///必须是数字
if(c < '0' || c > '9'){
return false;
}
//变化数字
num = num * 10 + (c - '0');
//范围0-255,不能超过255
if(num > 255){
return false;
}
}
}
return true;
}
bool isIPv6(string IP){
//特殊情况
if (IP.size() < 15 || IP[0] == ':' || IP[IP.size() - 1] == ':') {
return false;
}
//按照.切分
vector<string> str = split(IP, ":");
//切割后的长度不够8返回false
if(str.size() != 8) return false;
for(int i = 0;i < str.size();i ++){
if(str[i].size() == 0 || str[i].size() > 4){
return false;
}
for(int j = 0;j < str[i].size();j ++){
char c = str[i][j];
///必须是数字或字母
if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9'))){
return false;
}
}
}
return true;
}
string solve(string IP) {
// write code here
if(isIPv4(IP)) return "IPv4";
if(isIPv6(IP)) return "IPv6";
return "Neither";
}
};
Java版本:
import java.util.*;
public class Solution {
/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
public boolean isIPv4(String IP){
//特殊情况
if (IP.length() < 7 || IP.charAt(0) == '.' || IP.charAt(IP.length() - 1) == '.') {
return false;
}
String []str = IP.split("\\.");
//如果字符串数组长度不够4返回false
if(str.length != 4) return false;
for(String s: str){
//开头第一个不能是0
if(s.length() == 0 || (s.charAt(0) == '0'&& s.length()>1) || s.length() > 3){
return false;
}
int num = 0;
for(int i = 0;i < s.length();i ++){
char c = s.charAt(i);
///必须是数字
if(c < '0'|| c > '9'){
return false;
}
num = num * 10 + (c - '0');
//范围0-255,不能超过255
if(num > 255){
return false;
}
}
}
return true;
}
public boolean isIPv6(String IP){
//特殊情况
if (IP.length() < 15 || IP.charAt(0) == ':' || IP.charAt(IP.length() - 1) == ':') {
return false;
}
String []str = IP.split(":");
//如果字符串数组长度不够8返回false
if(str.length != 8) return false;
for(String s: str){
//开头第一个不能是0
if(s.length() == 0 || s.length() > 4){
return false;
}
for(int i = 0;i < s.length();i ++){
char c = s.charAt(i);
if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9'))){
return false;
}
}
}
return true;
}
public String solve (String IP) {
// write code here
if(isIPv4(IP)) return "IPv4";
if(isIPv6(IP)) return "IPv6";
return "Neither";
}
}Python版本:
#
# 验证IP地址
# @param IP string字符串 一个IP地址字符串
# @return string字符串
#
class Solution:
def isIPv4(self,IP):
#长度小于7开头或结尾为.都返回false
if len(IP) < 7 or IP[0] == '.' or IP[len(IP) - 1] == '.':
return False
#按照.切分
strr = IP.split('.')
#切割后的长度不够4返回false
if len(strr) != 4: return False
for s in strr:
#开头第一个不能是0
if len(s) == 0 or (s[0] == '0' and len(s) > 1) or len(s) > 3:
return False
num = 0#转换数字
for j in s:
if j < '0' or j > '9':
return False
num = num * 10 + int(j)
if num > 255 : return False
return True
def isIPv6(self,IP):
#长度小于15开头或结尾为:都返回false
if len(IP) < 15 or IP[0] == ':' or IP[-1] == ':':
return False
#切割后的长度不够8返回false
strr = IP.split(':')
if len(strr) != 8: return False
for s in strr:
if len(s) == 0 or len(s) > 4:
return False
for c in s:
if not('0' <= c <= '9' or 'a' <= c.lower() <= 'f' or 'A' <= c.upper() <= 'F'):
return False
return True
def solve(self , IP ):
# write code here
if self.isIPv4(IP): return "IPv4"
if self.isIPv6(IP): return "IPv6"
return "Neither"JavaScript版本:
/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
function isIPv4(IP){
//特殊情况
if (IP.length < 7 || IP[0] == '.' || IP[IP.length- 1] == '.') {
return false;
}
let str = IP.split(".");
//如果字符串数组长度不够4返回false
if(str.length != 4) return false;
for(let i = 0; i < str.length; i++){
//开头第一个不能是0
if(str[i].length == 0 || (str[i][0] == '0'&& str[i].length>1) ||str[i].length > 3){
return false;
}
let num = 0;
for(let j = 0;j < str[i].length;j ++){
let c = str[i][j];
///必须是数字
if(c < '0'|| c > '9'){
return false;
}
num = num * 10 + Number(c);
//范围0-255,不能超过255
if(num > 255){
return false;
}
}
}
return true;
}
function isIPv6(IP){
//特殊情况
if (IP.length < 15 || IP[0] == ':' || IP[IP.length - 1] == ':') {
return false;
}
let s = IP.split(":");
//如果字符串数组长度不够8返回false
if(s.length != 8) return false;
for(let i = 0; i < s.length; i++){
//开头第一个不能是0
if(s[i].length == 0 || s[i].length > 4){
return false;
}
for(let j = 0;j < s[i].length;j ++){
let c = s[i][j];
if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9'))){
return false;
}
}
}
return true;
}
function solve( IP ) {
// write code here
if(isIPv4(IP)) return "IPv4";
if(isIPv6(IP)) return "IPv6";
return "Neither";
}
module.exports = {
solve : solve
};
京公网安备 11010502036488号