剑指offer 28. 字符串的全排列

xiaoxiao2021-02-28  143

//题目:输入一个字符串,输出字符串的全排列 //解法:确定字符串的第一个字符,然后递归求之后字符的全排列 public class Main { public static void main(String[] args) throws Exception { printAllSeq("abc"); } public static void printAllSeq(String str){ if(str == null){ return; } char[] input = str.toCharArray(); printAllSeqHelper(input,0); } public static void printAllSeqHelper(char[] input, int begin){ if(begin == input.length-1){ System.out.println(input); }else{ for(int i = begin;i<input.length;i++){ char temp = input[begin]; input[begin] = input[i]; input[i] = temp; printAllSeqHelper(input,begin+1); temp = input[begin]; input[begin] = input[i]; input[i] = temp; } } } }
转载请注明原文地址: https://www.6miu.com/read-35529.html

最新回复(0)