93.复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
示例:
输入: "25525511135"
输出: ["255.255.11.135", "255.255.111.35"]
def restoreIpAddresses(s): def valid(segment): return int(segment) <= 255 if segment[0] != '0' else len(segment) == 1 def update_output(curr_pos): segment = s[curr_pos + 1: n] if valid(segment): segments.append(segment) output.append(segments) segments.pop() def backtrack(prev_pos = -1, dots = 3): for curr_pos in range(prev_pos+1, min(n-1, prev_pos+4)): segment = s[prev_pos + 1:curr_pos + 1] if valid(segment): segments.append(segment) if dots - 1 == 0: update_output(curr_pos) else: backtrack(curr_pos, dots - 1) segments.pop() n = len(s) output, segments = [], [] backtrack() return output
class Solution { int n; String s; LinkedList<String> segments = new LinkedList<String>(); ArrayList<String> output = new ArrayList<String>(); public boolean valid(String segment){ int m = segment.length(); if (m > 3) return false; return (segment.charAt(0) != '0') ? (Integer.valueOf(segment) <= 255) : (m == 1); } public void update_output(int curr_pos) { String segment = s.substring(curr_pos + 1, n); if (valid(segment)){ segments.add(segment); output.add(String.join(".", segments)); segments.removeLast(); } } public void backtrack(int prev_pos, int dots){ int max_pos = Math.min(n-1, prev_pos+4); for (int curr_pos = prev_pos + 1; curr_pos < max_pos; curr_pos++){ String segment = s.substring(prev_pos + 1, curr_pos + 1); if (valid(segment)){ segments.add(segment); if (dots - 1 == 0) update_output(curr_pos); else backtrack(curr_pos, dots - 1); segments.removeLast(); } } } public List<String> restoreIpAddresses(String s) { int prev_pos = -1; int dots = 3; n = s.length(); this.s = s; backtrack(prev_pos, dots); return output; } }
468.验证IP地址
思路一:正则
思路二:分治
import re chunk_IPv4 = r'([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' pattern_IPv4 = re.compile(r'^(' + chunk_IPv4 + r'.){3}' + chunk_IPv4 + r'$') chunk_IPv6 = r'([0-9a-fA-F]{1,4})' pattern_IPv6 = re.compile(r'^(' + chunk_IPv6 + r':){7}' + chunk_IPv6 + r'$') def validIPAddress(IP: str) -> str: if '.' in IP: return "IPv4" if pattern_IPv4.match(IP) else "Neither" if ':' in IP: return "IPv6" if pattern_IPv6.match(IP) else "Neither" return "Neither"
import java.util.regex.Pattern; class Solution { String chunkIPv4 = "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; Pattern patternIPv4 = Pattern.compile("^(" + chunkIPv4 + "\.){3}" + chunkIPv4 + "$"); String chunkIPv6 = "([0-9a-fA-F]{1,4})"; Pattern patternIPv6 = Pattern.compile("^(" + chunkIPv6 + "\:){7}" + chunkIPv6 + "$"); public String validIPAddress(String IP) { if (IP.contains(".")){ return (patternIPv4.matcher(IP).matches()) ? "IPv4" : "Neither"; } else if (IP.contains(":")){ return (patternIPv6.matcher(IP).matches()) ? "IPv6" : "Neither"; } return "Neither"; } }
1108.IP地址无效化
给你一个有效的 IPv4地址 address
,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 "[.]"
代替了每个 "."
。
def defangIPaddr(address: str) -> str: res = '' for i in address: if i == '.': res += '[.]' else: res += i return res
//
def defangIPaddr(address: str) -> str:
return address.replace('.','[.]')
class Solution { public String defangIPaddr(String address) { StringBuilder sb = new StringBuilder(); String[] splits = address.split("\."); for (String split: splits){ sb.append(split + "[.]"); } return sb.toString().substring(0, sb.length() - 3); } }