publicstaticvoidf(String[] strs, int i, ArrayList<String> ans){ if (i == strs.length){ StringBuilderpath=newStringBuilder(); for (String str : ans){ path.append(str); } } else { for (intj= i; j < strs.length; j++){ swap(strs, i, j); f(strs, i + 1, ans); swap(strs, i, j); } } }
贪心策略
1 2 3 4 5 6 7 8
publicstaticvoidway2(String[] args) { Arrays.sort(strs, (a, b) -> (a+b).compareTo(b+a)); StringBuilderpath=newStringBuilder(); for (String str : strs) { path.append(str); } return path.toString(); }