zoukankan      html  css  js  c++  java
  • js实现的笛卡尔乘积-商品发布

    //笛卡儿积组合
    function descartes(list)
    {
        //parent上一级索引;count指针计数
        var point  = {};
     
        var result = [];
        var pIndex = null;
        var tempCount = 0;
        var temp   = [];
     
        //根据参数列生成指针对象
        for(var index in list)
        {
            if(typeof list[index] == 'object')
            {
                point[index] = {'parent':pIndex,'count':0}
                pIndex = index;
            }
        }
     
        //单维度数据结构直接返回
        if(pIndex == null)
        {
            return list;
        }
     
        //动态生成笛卡尔积
        while(true)
        {
            for(var index in list)
            {
                tempCount = point[index]['count'];
                temp.push(list[index][tempCount]);
            }
     
            //压入结果数组
            result.push(temp);
            temp = [];
     
            //检查指针最大值问题
            while(true)
            {
                if(point[index]['count']+1 >= list[index].length)
                {
                    point[index]['count'] = 0;
                    pIndex = point[index]['parent'];
                    if(pIndex == null)
                    {
                        return result;
                    }
     
                    //赋值parent进行再次检查
                    index = pIndex;
                }
                else
                {
                    point[index]['count']++;
                    break;
                }
            }
        }
    }
    //该代码片段来自于: http://www.sharejs.com/codes/javascript/7013
    
    
    
    
    var aa=[1,2,3],bb=[1,2,3],cc=[1,2,3],dd=[1,2,3];
    var zz=[aa,bb,cc,dd];//此处数组个数任意
    var selectSpec = [ ['16G','64G','128G'] , ['土豪金','银色','黑色','pink'],['联通3G存费送餐','联通4G存费送餐','联通2G存费送餐'], ['76元套餐','106元套餐','206元套餐']];
     
    
    var result=descartes(selectSpec);
    console.log(result);
    document.write(result);

     移宝网的实现 思路

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/yuri2016/p/5294914.html
Copyright © 2011-2022 走看看