/** ** @param STR String * @param length String length * @param index Current index * @param Remain Remaining number * @param path Path * @param res Result */ public static void dfs(String str, int length, int index, int remain, StringBuilder path, List<String> res) { if (remain == 0 && index == length) { res.add(path.toString()); return; } if (remain < 0 || index == length) { return; } for (int I = index+1; i <= index + 3; i++) { if (i > length) { break; } String temp = str.substring(index, i); if (isRight(temp)) { path.append(temp); if (remain > 1) { path.append("."); } // depth-first traversal DFS (STR, length, I, remain-1, path, res); if (remain > 1) { path.deleteCharAt(path.length() - 1); } path.delete(path.length() - temp.length(), path.length()); Public static Boolean isRight(String s) {int length = s.length(); public static Boolean isRight(String s) {int length = s.length(); int value = Integer.valueOf(s); return length == 1 || length == 2 && (10 <= value) && (value <= 99) || length == 3 && (100 <= value) && (value <= 255); }Copy the code