zoukankan      html  css  js  c++  java
  • php求一维数组的排列

    <?php
    class CombinationsGenerator
    {
        public function generate(array $list)
        {
            if (count($list) > 2) {
                for ($i = 0; $i < count($list); $i++) {
                    $listCopy = $list;
    
                    $entry = array_splice($listCopy, $i, 1);
                    foreach ($this->generate($listCopy) as $combination) {
                        yield array_merge($entry, $combination);
                    }
                }
            } elseif (count($list) > 0) {
                yield $list;
    
                if (count($list) > 1) {
                    yield array_reverse($list);
                }
            }
        }
    }
    
    $generator = new CombinationsGenerator();
    
    $temp = [];
    foreach ($generator->generate([1, 2, 3, 5, 5]) as $combination) {
        if (!in_array($combination, $temp))
            $temp[] = $combination;
    //    var_dump($combination);
    }
    var_dump(count($temp));
    
    $temp1 = [];
    foreach ($generator->generate([1, 2, 3, 3]) as $combination) {
        if (!in_array($combination, $temp1)) $temp1[] = $combination;
    }
    var_dump(count($temp1));
    

      

    上面的!in_array条件是去重

  • 相关阅读:
    Java工具类
    集合 -- 嵌套表
    集合--索引表
    第一章
    记录Record
    序列Sequence
    操纵数据库 DML
    表的集合操作
    视图
    索引
  • 原文地址:https://www.cnblogs.com/eleven24/p/7380131.html
Copyright © 2011-2022 走看看