如: ab ac ad bc bd cb   abc abd bcd bca 从左往右 和从右往左 都需要
求大神给段代码最好再详解,
明天面试 

解决方案 »

  1.   


    public static void main(String[] args) {
        
            String arr[] = { "a", "b", "c", "d" };
            int all = 4;
            for (int i = 0; i < 1 << all; i++) {
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < all; j++) {
                    if ((i & (1 << j)) != 0) {
                        sb.append(arr[j]);
                    }
                }
                System.out.println(sb);
            }
        }
      

  2.   

       List<String> result = new ArrayList<>();
        @Test
        public void test() {
            String[] target = new String[]{"a", "b", "c", "d"};
            for (int i = 1; i <= target.length; i++)
                calc(i, "", target);
            System.out.println(Arrays.toString(result.toArray()));
        }    //递归
        public void calc(int length, String p, String... target) {
            for (String item : target)
                if (!p.contains(item) && (!((length == 1) && result.add(p + item))))
                    calc(length - 1, p + item, target);
        }
      

  3.   

    public class Abcd {
    private static final String[] str = { "a", "b", "c", "d" };
    private static String res = "";
    int len = str.length; public static void main(String[] args) {
    t2();
    t3();
    } private static void t2() {
    for (int i = 0; i < str.length; i++) {
    res = str[i];
    for (int j = 0; j < str.length; j++) {
    System.out.println(res + str[j]);
    }
    } } private static void t3() {
    for (int i = 0; i < str.length; i++) {
    res = str[i];
    for (int j = 0; j < str.length; j++) {
    // System.out.println(res + str[j]);
    for (int k = 0; k < str.length; k++) {
    System.out.println(res + str[j] + str[k]);
    } } } }}