zoukankan      html  css  js  c++  java
  • 字符串的全排列

     1 import org.junit.Test;
     2 
     3 public class AllSort {
     4 
     5     public void permutation(char[] buf, int start, int end) {
     6         if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
     7             for (int i = 0; i <= end; i++) {
     8                 System.out.print(buf[i]);
     9             }
    10             System.out.println();
    11         } else {// 多个字母全排列
    12             for (int i = start; i <= end; i++) {
    13                 char temp = buf[start];// 交换数组第一个元素与后续的元素
    14                 buf[start] = buf[i];
    15                 buf[i] = temp;
    16 
    17                 permutation(buf, start + 1, end);// 后续元素递归全排列
    18 
    19                 temp = buf[start];// 将交换后的数组还原
    20                 buf[start] = buf[i];
    21                 buf[i] = temp;
    22             }
    23         }
    24     }
    25 
    26     @Test
    27     public void testPermutation() throws Exception {
    28         char[] buf = new char[] { 'a', 'b', 'c' };
    29         permutation(buf, 0, 2);
    30     }
    31 }
  • 相关阅读:
    setsid
    dup
    信号量
    linux标准输入输出
    linux守护进程范例
    c++字符串操作
    浏览器缓存
    bfc
    苹果手机自制铃声
    vue-cli 源码解读
  • 原文地址:https://www.cnblogs.com/jiangyi-uestc/p/5892820.html
Copyright © 2011-2022 走看看