题解 | #数字字符串转化成IP地址#
数字字符串转化成IP地址
https://www.nowcoder.com/practice/ce73540d47374dbe85b3125f57727e1e
class Solution: def restoreIpAddresses(self , s: str) -> List[str]: n = len(s) ans = [] if not s: return ans def diedai(t,i,k): if i >= n: ans.append(t) if k == 3: if int(s[i]) > 0 and int(s[i:]) <256: diedai(t+s[i:],n,k+1) elif int(s[i]) == 0 and i == n-1: diedai(t+s[i:],n,k+1) if k < 4: if int(s[i]) >= 0 and i < n-1: diedai(t+s[i]+'.',i+1,k+1) if int(s[i]) > 0 and i < n-2: diedai(t+s[i:i+2]+'.',i+2,k+1) if 100 <= int(s[i:i+3]) <256 and i < n-3: diedai(t+s[i:i+3]+'.',i+3,k+1) diedai('',0,0) return ans