递归解排列组合问题

全排列递归求解

 1 public class Main {
 2 
 3     //思路:对每一个子递归的数组(除去第一个数的数组)进行全排列,并用除去的第一个数对所有子数组的全排列的每一项进行交换
 4     
 5     public static void swap(int[] a,int cursor,int i) {
 6         int temp=a[cursor];
 7         a[cursor]=a[i];
 8         a[i]=temp;
 9     }
10 
11     public static void digui(int[] a,int cursor,int end) {
12         if(cursor==end) {
13             for(int i=0;i<5;i++) {
14                 System.out.print(a[i]);
15             }
16             System.out.println();
17         }
18         else {
19             for(int i=cursor;i<=end;i++) {
20                 swap(a,cursor,i);
21                 digui(a,cursor+1,end);
22                 swap(a,cursor,i);
23             }
24         }
25     }
26     
27     public static void main(String[] args) {
28         int[] a = {1,2,3,4,5};
29         digui(a,0,4);
30     }
31 
32 }

 

全排列有重复元素情况时

public class Main {

    // 思路:有重复时加入方法,进行交换时若要交换的中间若有相同的数字,则停止本次交换意图
    //例如12335,先进行的第2个3和5的交换没有在中间发现3,则成功进行12353.在第一个3和5发生交换时发现了中间的3,则取消本次交换。
    //12533为在12353状态下第一个3和5进行的交换

    public static boolean swapaccepted(int[] array,int start,int end) {
        for(int i=start;i<end;i++) {
            if(array[i]==array[end]) {
                return false;
            }
        }
        return true;
    }
    
    public static void swap(int[] a, int cursor, int i) {
        if (a[cursor] != a[i]) {
            int temp = a[cursor];
            a[cursor] = a[i];
            a[i] = temp;
        }
    }

    public static void digui(int[] a, int cursor, int end) {
        if (cursor == end) {
            for (int i = 0; i < 5; i++) {
                System.out.print(a[i]);
            }
            System.out.println();
        } else {
            for (int i = cursor; i <= end; i++) {
                if(!swapaccepted(a,cursor,i)) {
                    continue;
                }
                swap(a, cursor, i);
                digui(a, cursor + 1, end);
                swap(a, cursor, i);
            }
        }
    }

    public static void main(String[] args) {
        int[] a = { 1, 2, 3, 3, 5 };
        digui(a, 0, 4);
    }

}

 

组合有几种取法

 1 //组合,m个球取出n个有几种取法
 2 //递归计数两部走:1.取到了某一个球,则再从剩下的m-1个里取n-1个    (隐含第二步)2.永远都没取到这个球,从剩下的m-1个里选n个
 3 public class Main {
 4 
 5     public static int digui(int m, int n) {
 6         if (n == m)
 7             return 1;
 8         if (n == 0)
 9             return 1;
10         else
11             return digui(m - 1, n - 1) + digui(m - 1, n);
12     }
13 
14     public static void main(String[] args) {
15         System.out.println(digui(5, 3));
16     }
17 
18 }

 

组合无重复需要列出所有情况

 1 //组合,列出m个球取出n个所有组合
 2 //组合类似于多for循环,在第一个数为什么的情况下找第二个数,在前两个数既定的情况下找下一个,注意回溯
 3 public class Main {
 4 
 5     public static void digui(int[] a, int cursor, int remain, String s,int num) {
 6 
 7         if (remain == 0)
 8             System.out.println(s);
 9         
10         else {
11             for (int i = cursor; i <= a.length-1; i++) {
12                 s += Integer.toString(a[i]);
13                 digui(a, i + 1, remain-1, s,num);
14                 s=s.substring(0, num-remain);
15             }
16         }
17 
18     }
19 
20     public static void main(String[] args) {
21         int[] a = { 1, 2, 3, 4, 5 };
22         digui(a, 0, 3, "",3);
23     }
24 
25 }

 

组合有重复情况列出所有

 1 //组合,列出m个球取出n个所有组合且球里有重复的
 2 //类似于无重复组合思路,将问题数组改为表示元素个数的数组,并在递归是进行判断是否超过了球的个数。类似于多循环间加入限定条件if
 3 public class Main {
 4 
 5     public static void digui(int[] b, int cursor, int remain, int[] c, int num) {
 6 
 7         if (remain == 0) {
 8             for (int i = 0; i < c.length; i++)
 9                 System.out.print(c[i]);//为了好理解只输出个数数组
10             System.out.println();
11         } else {
12             for (int i = cursor; i<b.length; i++) {
13                 c[i]++;
14                 if (c[i] >= b[i])
15                     digui(b, i + 1, remain - 1, c, num);
16                 else
17                     digui(b, i, remain - 1, c, num);
18                 c[i]--;
19             }
20         }
21 
22     }
23 
24     public static void main(String[] args) {
25         int[] a = { 1, 1, 2, 2, 2, 3 };
26         int[] b = { 2, 3, 1 };// 将原数组改写为个数数组
27         int[] c = new int[3];
28         digui(b, 0, 3, c, 3);
29     }
30 
31 }

 

转载于:https://www.cnblogs.com/superxinyu/p/8553483.html

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的释性差、计算资源消耗大等。研究人员正在不断探索新的方法来决这些问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值