zoukankan      html  css  js  c++  java
  • 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example,

    8 = 2 x 2 x 2;
      = 2 x 4.
    

    Write a function that takes an integer n and return all possible combinations of its factors.

    Note: 

    1. You may assume that n is always positive.
    2. Factors should be greater than 1 and less than n.

    Examples: 
    input: 1
    output: 

    []
    

    input: 37
    output: 

    []
    

    input: 12
    output:

    [
      [2, 6],
      [2, 2, 3],
      [3, 4]
    ]
    

    input: 32
    output:

    [
      [2, 16],
      [2, 2, 8],
      [2, 2, 2, 4],
      [2, 2, 2, 2, 2],
      [2, 4, 4],
      [4, 8]
    ]

    这道题需要注意的是如何避免重复,需要在backtracking的inputs里加一个sentinel,在backtracking的循环过程中从sentinel开始加。

    public IList<IList<int>> GetFactors(int n) {
            var res = new List<IList<int>>();
            if(n ==0) return res;
            BackTracking(n,new List<int>(),res,2);
            return res;
        }
        
        private void BackTracking(int n, List<int> cur, IList<IList<int>> res,int last)
        {
            if(n==1)
            {
                if(cur.Count()>1)
                res.Add(new List<int>(cur));
            }
            else
            {
                for(int i = last;i<= n;i++)
                {
                    if(n%i ==0)
                    {
                        cur.Add(i);
                        BackTracking(n/i,cur,res,i);
                        cur.RemoveAt(cur.Count()-1);
                    }
                }
            }
        }

    上面算法是可以优化的,因为一个整数分为两个整数乘积的时候,较小的乘数也不会超过原来数字的平方根。则可以在loop的上界限制为sqrt(n)。

    public IList<IList<int>> GetFactors(int n) {
            var res = new List<IList<int>>();
            if(n ==0) return res;
            BackTracking(n,new List<int>(),res,2);
            return res;
        }
        
        private void BackTracking(int n, List<int> cur, IList<IList<int>> res,int last)
        {
            if(cur.Count()>0)
            {
                cur.Add(n);
                res.Add(new List<int>(cur));
                cur.RemoveAt(cur.Count()-1);
            }
            
                for(int i = last;i<= (int)Math.Sqrt(n);i++)
                {
                    if(n%i ==0)
                    {
                        cur.Add(i);
                        BackTracking(n/i,cur,res,i);
                        cur.RemoveAt(cur.Count()-1);
                    }
                }
            
        }
  • 相关阅读:
    .net core2.0 中使用aspectcore实现aop
    [Superset] 设置Superset的登录设置
    [Python]Pandas对于非唯一的label index的数据选择问题
    Data Science Radar测试结果
    [R]R包版本更迭【持续更新】
    [面试] 删除多余的数组内容
    [Python]Python中的包(Package)
    [Linux] 使用Yum在CentOS上安装MySQL
    [pyMongo]insert_many的Bulkwrite实现机制
    [Git]2018-10 解决git cmd中文乱码问题
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5876176.html
Copyright © 2011-2022 走看看