输入: "123"输出: [["1","2","3"],["12","3"],["1","23"]]输入: "12345"输出: [["1","23","45"],["12","3","45"],["12","34","5"],["1","2","3","45"],["1","2","34","5"],["1","23","4","5"],["12","3","4","5"],["1","2","3","4","5"]]public class Solution { /* * @param : a string to be split * @return: all possible split string array */ public List<List<String>> splitString(String s) { List<List<String>> result = new ArrayList<>(); dfs(s, 0, new ArrayList<>(), result); return result; } private void dfs(String s, int index, List<String> current, List<List<String>> result) { if (index == s.length()) { result.add(new ArrayList<>(current)); return; } // 分割1个字符 current.add(String.valueOf(s.charAt(index))); dfs(s, index + 1, current, result); current.remove(current.size() - 1); // 分割2个字符 if (index < s.length() - 1) { current.add(s.substring(index, index + 2)); dfs(s, index + 2, current, result); current.remove(current.size() - 1); } }}[leetcode/lintcode 题解] Google 面试题:分割字符串
原文:https://www.cnblogs.com/lintcode/p/13919499.html