import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串ArrayList */ public ArrayList<String> restoreIpAddresses (String s) { // write code here ArrayList<String> ret = new ArrayList<String>(); if (s.length() < 4) { return ret; } for (int i = 1; i < 4; i++) { for (int j = i + 1; j < i + 4 && j < s.length(); j++) { for (int k = j + 1; k < j + 4 && k < s.length(); k++) { String a1 = s.substring(0, i); String a2 = s.substring(i, j); String a3 = s.substring(j, k); String a4 = s.substring(k, s.length()); if (a4.length() <= 3) { int b1 = Integer.parseInt(a1); int b2 = Integer.parseInt(a2); int b3 = Integer.parseInt(a3); int b4 = Integer.parseInt(a4); if ((b1 == 0 && a1.length() != 1) || (b2 == 0 && a2.length() != 1) || (b3 == 0 && a3.length() != 1) || (b4 == 0 && a4.length() != 1)) { break; } if (b1 <= 255 && b2 <= 255 && b3 <= 255 && b4 <= 255) { if ((!(a1.charAt(0) == '0' && b1 != 0)) && (!(a2.charAt(0) == '0' && b2 != 0)) && (!(a3.charAt(0) == '0' && b3 != 0)) && (!(a4.charAt(0) == '0' && b4 != 0))){ String tmp = ""; tmp += a1 + "." + a2 + "." + a3 + "." + a4; ret.add(tmp); } } } } } } return ret; } }