LeetCode 625. 最小因式分解(贪心)

文章目录

1. 题目

给定一个正整数 a,找出最小的正整数 b 使得 b 的所有数位相乘恰好等于 a。

如果不存在这样的结果或者结果不是 32 位有符号整数,返回 0。

样例 1
输入:
48 
输出:
68

样例 2
输入:
15
输出:
35

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-factorization
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 贪心,从最大的9开始除,能整除就放在末尾
class Solution {
public:
    int smallestFactorization(int a) {
        if(a < 10) return a;
    	long long ans = 0, base = 1;
        for(int i = 9; i >= 2; --i)
        {
        	while(a%i == 0)
        	{
        		ans += i*base;
                a /= i;
        		base *= 10;
        		if(ans > INT_MAX)
        			return 0;
        	}
        }
        return a==1 ? ans : 0;
    }
};

0 ms 5.8 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

猜你喜欢

转载自blog.csdn.net/qq_21201267/article/details/107880373