zoukankan      html  css  js  c++  java
  • 28 列出数组中数的全部组合

    //列出一个数组中数的全部组合。如{1,2}则列出12,21
    import java.util.*;
    public class ListCombinations1{
        public static void main(String args[]){
            String[] array = {"1","2","3"};
            listCombinations(Arrays.asList(array),"");
        }
        public static void listCombinations(List candidate, String prefix){
            if(candidate.isEmpty()){
                System.out.println(prefix);
            }
            for(int i=0; i<candidate.size(); i++){
                List temp = new LinkedList(candidate);
                listCombinations(temp, prefix+temp.remove(i));
            }
        }
    }
    
        
    //列出一个数组中数的全部组合。如{1,2}则列出1,2,12,21
    import java.util.*;
    public class ListCombinations{
        public static void main(String args[]){
            String[] array = {"1","2","3"};//是String 而非int!!!
            listCombinations(Arrays.asList(array),"");
        }
        public static void listCombinations(List candidate, String prefix){
            System.out.println(prefix);//与上段代码只有此处不同
            for(int i=0; i<candidate.size(); i++){
                List temp = new LinkedList(candidate);
                listCombinations(temp, prefix+temp.remove(i));
            }
        }
    }
    
        
  • 相关阅读:
    ETL之数据库
    Git的简单实用
    Linux-easy mock部署
    Linux-docker安装mysql
    Linux-安装docker
    Linux-centos7安装Python3和pip3
    Linux-VMware下安装centos7
    Python之hasattr()、getattr()和setattr()
    jsonpath 信息抽取类库
    Python之内置测试框架unittest
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3777236.html
Copyright © 2011-2022 走看看