题目
题目链接:
https://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880
https://www.lintcode.com/problem/1222/description
思路
直接模拟,注意IPv4,ipv6的条件
Java代码
import java.util.*;public class Solution {/*** 验证IP地址* @param IP string字符串 一个IP地址字符串* @return string字符串*/public String solve (String IP) {String[] arr = IP.split("\\.");String[] arr1 = IP.split(":");if(arr.length ==4){if(checkIPV4(arr)) return "IPv4";}else if(arr1.length==8){if(IP.endsWith(":") && !arr1[arr1.length-1].equals("0")) return "Neither";if(checkIPV6(arr1)) return "IPv6";}return "Neither";}public static boolean checkIPV4(String[] arr){for (String s : arr) {for(int i=0;i<s.length();i++) {int c = s.charAt(i);if(!(c>='0' && c<='9')) return false;}if(Integer.parseInt(s) >255) return false;if(s.startsWith("0")) return false;}return true;}public static boolean checkIPV6(String[] arr){for (String s : arr) {if(s.length() ==0) return false;if(s.length()>4) return false;for(int i=0;i<s.length();i++){int c = s.charAt(i);if(c>'f' && c<='z' || c>'F' && c<='Z') return false;}}return true;}
}
Go代码
package mainimport ("strconv""strings"
)/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** 验证IP地址* @param IP string字符串 一个IP地址字符串* @return string字符串*/
func solve(IP string) string {// write code herearr := strings.Split(IP, ".")arr1 := strings.Split(IP, ":")if len(arr) == 4 {if checkIPV4(IP, arr) {return "IPv4"}}if len(arr1) == 8 {if checkIPV6(IP, arr1) {return "IPv6"}}return "Neither"
}func checkIPV4(ip string, arr []string) bool {if ip[len(ip)-1] == '.' {return false}for _, s := range arr {if len(s) > 1 && s[0] == '0' {return false}for j := 0; j < len(s); j++ {if !(s[j] >= '0' && s[j] <= '9') {return false}}num, _ := strconv.Atoi(s)if num > 255 {return false}}return true
}
func checkIPV6(ip string, arr []string) bool {sepcnt := 0for i := 0; i < len(ip); i++ {if ip[i] == ':' {sepcnt++}}if sepcnt > 7 {return false}for _, s := range arr {if len(s) == 0 || len(s) > 4 {return false}for i := 0; i < len(s); i++ {c := s[i]if c > 'f' && c <= 'z' || c > 'F' && c <= 'Z' {return false}}}return true
}
PHP代码
<?php/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** 验证IP地址* @param IP string字符串 一个IP地址字符串* @return string字符串*/
function solve( $IP ) {$arr = explode(".",$IP);$arr1 = explode(":",$IP);if(count($arr) ==4) {if(checkIPv4($IP,$arr)){return "IPv4";}}if(count($arr1) ==8){if(checkIPv6($IP,$arr1)){return "IPv6";}}return "Neither";
}function checkIPv4($IP,$arr){$len = strlen($IP);if($IP[$len-1] == '.') return false;foreach ($arr as $s){$len1= strlen($s);if($len1 >1 && $s[0] =='0') return false;for($j=0;$j<$len1;$j++){if(!($s[$j]>='0' && $s[$j]<='9'))return false;}if(intval($s) > 255) return false;}return true;
}function checkIPv6($IP,$arr){$sepcnt =0;$n = strlen($IP);for($i=0;$i<$n;$i++){if($IP[$i] ==':')$sepcnt++;}if($sepcnt >7) return false;foreach ($arr as $s){$len1 =strlen($s);if($len1 ==0 || $len1 > 4) return false;for($j=0;$j<$len1;$j++){if($s[$j] >'f' && $s[$j]<='z'|| $s[$j]>'F' && $s[$j]<='Z') return false;}}return true;
} $arr = explode(".",$IP);$arr1 = explode(":",$IP);if(count($arr) ==4) {if(checkIPv4($IP,$arr)){return "IPv4";}}if(count($arr1) ==8){if(checkIPv6($IP,$arr1)){return "IPv6";}}return "Neither";
}function checkIPv4($IP,$arr){$len = strlen($IP);if($IP[$len-1] == '.') return false;foreach ($arr as $s){$len1= strlen($s);if($len1 >1 && $s[0] =='0') return false;for($j=0;$j<$len1;$j++){if(!($s[$j]>='0' && $s[$j]<='9'))return false;}if(intval($s) > 255) return false;}return true;
}function checkIPv6($IP,$arr){$sepcnt =0;$n = strlen($IP);for($i=0;$i<$n;$i++){if($IP[$i] ==':')$sepcnt++;}if($sepcnt >7) return false;foreach ($arr as $s){$len1 =strlen($s);if($len1 ==0 || $len1 > 4) return false;for($j=0;$j<$len1;$j++){if($s[$j] >'f' && $s[$j]<='z'|| $s[$j]>'F' && $s[$j]<='Z') return false;}}return true;
}