zoukankan      html  css  js  c++  java
  • 递归和动态规划——**字符串的全排列

    打印一个字符串的全部排列

    public class CC_Print_All_Permutation {
        public static void printAllPermutation(String str){
            if(str == null || str.length() == 0) return;
            char[] chars = str.toCharArray();
            permutation(chars, 0);
        }
    
        public static void permutation(char[] chars, int index){
            if(index == chars.length){
                System.out.println(String.valueOf(chars));
                return;
            }
            HashSet<Character> hashSet = new HashSet<>();
            for(int i = index; i < chars.length; i++){
                if(!hashSet.contains( chars[i])){
                    hashSet.add(chars[i]);
                    swap(chars, index, i);
                    permutation( chars, index + 1 );
                    swap(chars, index, i);
                }
            }
        }
    
        public static void swap(char[] chars, int i, int j){
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
    
        public static void main(String[] args){
            printAllPermutation( "abc" );
        }
    }
    

      

     

  • 相关阅读:
    pinyin4j使用示例
    迭代器模式
    适配器模式
    策略模式
    装饰模式
    责任链模式
    命令模式
    中介者模式
    原型模式
    代理模式
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8965813.html
Copyright © 2011-2022 走看看