using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串一维数组 */ public List<string> restoreIpAddresses(string s) { // write code here if (string.IsNullOrWhiteSpace(s)) return new List<string>(); List<string> lsStr = new List<string>(); HS(0, s, lsStr, "", 0); return lsStr; } public void HS(int nCurIndex, string s, List<string> lsStr, string strR, int nT) { // write code here if (nCurIndex == s.Length && nT == 4) { lsStr.Add(strR.Substring(1)); return; } //n表示字符个数 for (int n = 1; n <= 3; n++) { if (nCurIndex + n > s.Length) return; string strPart = s.Substring(nCurIndex, n); if (strPart.StartsWith("0") && strPart.Length > 1) return; int nPart = int.Parse(strPart); if (nPart > 255 || nPart < 0) return; HS(nCurIndex + n, s, lsStr, strR + '.' + s.Substring(nCurIndex, n), nT + 1); } } }