zoukankan      html  css  js  c++  java
  • leetcode -625-Minimum Factorization

    leetcode -625-Minimum Factorization

    625. Minimum Factorization 


    Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.

    If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.

    Example 1
    Input:

    48 
    Output:
    68

    Example 2
    Input:

    15
    Output:
    35

    题解: 

        题目的关键在于a的range, 注意当 a < 10 的情况。   

        同时,需要 vt 的size小于10,32-bit的integer的最高位数是10位,但是最高位的范围是 2,1......, 一定不存在1且最小只能是2,所以只能保留9位。

    class Solution {
    public:
        int smallestFactorization(int a) {
            if(a < 10){
                return a; 
            }
            vector<int> vt; 
            for(int i=9; i>=2; --i){
                if(a == 1){
                    break; 
                }
                while(a%i == 0){
                    vt.push_back(i); 
                    a = a/i; 
                }
            }
            if(a == 1 && vt.size() <= 9){
                int ans = 0; 
                for(int i=vt.size()-1; i>=0; --i){
                    ans = 10*ans + vt[i]; 
                }
                return ans; 
            }else{
                return 0; 
            }
        }
    };
    

      

  • 相关阅读:
    12.静态目录
    11.Git线上操作
    10.分离的前后台交互
    09.后台主页应用
    08.前端主页
    爬虫介绍
    python2与python3爬虫中get与post对比
    HTML+CSS+JavaScript
    数据库 Mysql-mongodb-redis
    时频工具箱介绍与使用
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7067100.html
Copyright © 2011-2022 走看看