天天看点

php与前端的几种排列组合代码分享

前端jquery:

一.

function permutate(str) {
        //保存每一轮递归的排列结果
        var result = [];
        //初始条件:长度为1
        if (str.length == 1) {
            return [str]
        } else {
            //求剩余子串的全排列,对每个排列进行遍历
            var preResult = permutate(str.slice(1));
            for (var j = 0; j < preResult.length; j++) {
                for (var k = 0; k < preResult[j].length + 1; k++) {
                    //将首字母插入k位置  
                    var temp = preResult[j].slice(0, k) + str[0] + preResult[j].slice(k);
                    if($.inArray(temp,result) > -1){  //判断数组中重复元素
                    }else{
                        result.push(temp);
                    }
                }
            }
            return result;
        }
    }
           

permutate('123');      输出的结果是:123,132,213,231,312,321.

二.

// 全排列组合
            function getGroup(data, index = 0, group = []) {
				var need_apply = new Array();
				need_apply.push(data[index]);
				for(var i = 0; i < group.length; i++) {
					need_apply.push(group[i] + data[index]);
				}
				group.push.apply(group, need_apply);
			 
				if(index + 1 >= data.length) return group;
				else return getGroup(data, index + 1, group);
			}
           

var w = ['千','百','十','个'];

console.log(getGroup(w));        输出的结果是:如下图

php与前端的几种排列组合代码分享

后端php:

1.

function arrangement($a, $m) {
    $r = array();
 
    $n = count($a);
    if ($m <= 0 || $m > $n) {
        return $r;
    }
 
    for ($i=0; $i<$n; $i++) {
        $b = $a;
        $t = array_splice($b, $i, 1);
        if ($m == 1) {
            $r[] = $t;
        } else {
            $c = arrangement($b, $m-1);
            foreach ($c as $v) {
                $r[] = array_merge($t, $v);
            }
        }
    }
 
    return $r;
}

// ====== 测试 ======
$a = array("a", "b", "c", "d","e");
 
$r = arrangement($a, 3);
foreach($r as $k=>$v){
    $r[$k] = implode("",$v);
}
echo "<pre>";
print_r($r);exit;
           

2.

function combination($a, $m) {
    $r = array();
 
    $n = count($a);
    if ($m <= 0 || $m > $n) {
        return $r;
    }
 
    for ($i=0; $i<$n; $i++) {
        $t = array($a[$i]);
        if ($m == 1) {
            $r[] = $t;
        } else {
            $b = array_slice($a, $i+1);
            $c = combination($b, $m-1);
            foreach ($c as $v) {
                $r[] = array_merge($t, $v);
            }
        }
    }
    
    return $r;
}
 
 
// ====== 测试 ======
$a = array("a", "b", "c", "d","e");
 
$r = arrangement($a, 3);
foreach($r as $k=>$v){
    $r[$k] = implode("",$v);
}
echo "<pre>";
print_r($r);exit;
           

3.

function getCombinationToString($arr,$m){
        $result = array();
        if($m == 1){
            return $arr;
        }
        if($m == count($arr)){//当取出的个数等于数组的长度,就是只有一种组合,即本身
            $result[] = implode('',$arr);
            return $result;
        }
        $temp_firstelement = $arr[0];
        unset($arr[0]);
        $arr = array_values($arr);
        $temp_first1 = getCombinationToString($arr,$m - 1);

        foreach($temp_first1 as $s){
            $s = $temp_firstelement.$s;
            $result[] = $s;
        }
        unset($temp_first1);
        $temp_first2 = getCombinationToString($arr,$m);
        foreach($temp_first2 as $s){
            $result[] = $s;
        }
        unset($temp_first2);
        return $result;
    }

    $arr = array("a","b","c","d","e");//1~6的数组
    $result = getCombinationToString($arr,3);//6个数里面,取出2个数有多少种组合(即不考虑顺序)
    $data['count'] = count($result);//组合种数
    $data['data'] = $result;//各种数据组合
    echo "<pre>";
    print_r($data);exit;