zoukankan      html  css  js  c++  java
  • 位运算全组合算法

    通过位运算,打印出n的全部组合

    public static void getCombinations( int n)
    {
        String str = Integer.toString(n);
        int len = str.length();
            
        ArrayList<ArrayList> data = new ArrayList<ArrayList>();
            
        int nCnt = len;
            
        int nBit = 1<<nCnt;
            
        for (int i = 1; i <= nBit; i++) 
        {
            ArrayList<Integer> tempdata = new ArrayList<Integer>();
                
            for (int j = 0; j < nCnt; j++) 
            {
                if ((1<<j & i ) != 0) 
                {
                    tempdata.add(str.charAt(j) - 48);
                }
            }
                
            if(!tempdata.isEmpty())
                data.add(tempdata);
        }
            
        for(int z = 0 ; z < data.size(); z++)
            System.out.println( data.get(z).toString());
    }
  • 相关阅读:
    [LeetCode] 240
    [LeetCode] 169
    [LeetCode] 28
    [LeetCode] 27
    [LeetCode] 14
    [LeetCode] 9
    [LeetCode] 7
    [LeetCode] 2
    数据库开发规范
    Mysql优化
  • 原文地址:https://www.cnblogs.com/puloieswind/p/5688670.html
Copyright © 2011-2022 走看看