Question:
Given a list of string, like (a, b, c), return a list: (*, *, *), (a, *, *), (*, b, *), (*, *, c), (a, b, *), (a, * , c), (* , b, c), (a, b, c).
// NP public List<List<String>> generate(List<String> str) { // Validations... List<String> cur = new ArrayList<>(str.size()); Lists.fill(cur, "*"); List<List<String>> result = new ArrayList<>(); help(str, 0, cur, result); return result; } private void help(List<String> str, int start, List<String> cur, List<List<String>> result) { result.add(new ArrayList<String>(cur)); if (start >= str.size()) return; for (int i = start ; i < str.size() ; i ++) { cur.set(i, str.get(i)); help(str, i + 1, cur, result); cur.set(i, "*"); } }
时间: 2024-11-02 01:19:32