using System; using System.Collections.Generic; using System.Linq; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 验证IP地址 * @param IP string字符串 一个IP地址字符串 * @return string字符串 */ public string solve (string IP) { // write code here if(IP.Contains('.')){ string[] nums = IP.Split('.'); if (nums.Length != 4) return "Neither"; foreach(string str in nums){ if(int.TryParse(str, out int num)){ if(0 < num && num < 256 && str[0] != '0') continue; else if(num == 0 && str.Length == 1) continue; } return "Neither"; } return "IPv4"; } else if(IP.Contains(':')){ string[] nums = IP.Split(':'); if (nums.Length != 8) return "Neither"; foreach(string str in nums){ if(str.Length <= 4 && str.Length > 0){ foreach(char c in str){ if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) continue; return "Neither"; } } else return "Neither"; } return "IPv6"; } return "Neither"; } }