栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP数组组合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP数组组合

如果链接断开,这里是代码。

class Combinations implements Iterator{    protected $c = null;    protected $s = null;    protected $n = 0;    protected $k = 0;    protected $pos = 0;    function __construct($s, $k) {        if(is_array($s)) { $this->s = array_values($s); $this->n = count($this->s);        } else { $this->s = (string) $s; $this->n = strlen($this->s);        }        $this->k = $k;        $this->rewind();    }    function key() {        return $this->pos;    }    function current() {        $r = array();        for($i = 0; $i < $this->k; $i++) $r[] = $this->s[$this->c[$i]];        return is_array($this->s) ? $r : implode('', $r);    }    function next() {        if($this->_next()) $this->pos++;        else $this->pos = -1;    }    function rewind() {        $this->c = range(0, $this->k);        $this->pos = 0;    }    function valid() {        return $this->pos >= 0;    }    protected function _next() {        $i = $this->k - 1;        while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i) $i--;        if($i < 0) return false;        $this->c[$i]++;        while($i++ < $this->k - 1) $this->c[$i] = $this->c[$i - 1] + 1;        return true;    }}foreach(new Combinations("1234567", 5) as $substring)    echo $substring, ' ';

12345 12346 12347 12356 12357 12367 12456 12457 12467 12567 13456 13457 13467
13567 14567 23456 23457 23467 23567 24567 34567



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433126.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号