有重复元素的全排列问题

package algorithms;

/**
 * Created by sprint on 16-10-19.
 */
public class FullArray {

    /**
     *  思路:此程序是在全排列的基础上修改的。
     *      1.掌握全排列后
     *      2.添加条件:如果在arrays[i]前面含有 arrays[j] == arrays[i] 则不进行排列,否则进行排列。
     */
    
    public static void permutation(int[] arrays, int left, int right) {
        if (left == right) {
            for (int key : arrays) {
                System.out.print(key);
            }
            System.out.println();
        } else {
            for (int i = left; i <= right; i++) {
                if (!isExit(arrays, left, i)) {
                    swap(arrays, left, i);
                    permutation(arrays, left+1, right);
                    swap(arrays, left, i);
                }
            }
        }
    }

    public static void swap(int[] arrays, int leftValue, int rightValue) {
        int tempValue = arrays[leftValue];
        arrays[leftValue] = arrays[rightValue];
        arrays[rightValue] = tempValue;
    }

    public static boolean isExit(int[] arrays, int left, int index) {
        int key = arrays[index];
        for (int i = left; i < index; i++) {
            if (key == arrays[i]) {
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        int[] arrays = {1, 1, 2, 2};
        permutation(arrays, 0, arrays.length-1);
    }
}



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值