首页 > 代码库 > 字符串的全排列
字符串的全排列
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 @Test27 public void testPermutation() throws Exception {28 char[] buf = new char[] { ‘a‘, ‘b‘, ‘c‘ };29 permutation(buf, 0, 2);30 }31 }
字符串的全排列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。