Java最小因式分解_[LeetCode] 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

这道题给了我们一个数字,让我们进行因数分解,让我们找出因数组成的最小的数字。从题目中的例子可以看出,分解出的因数一定是个位数字,即范围是[2, 9]。那我们就可以从大到小开始找因数,首先查找9是否是因数,是要能整除a,就是其因数,如果是的话,就加入到结果res的开头,a自除以9,我们用while循环查找9,直到取出所有的9,然后取8,7,6...以此类推,如果a能成功的被分解的话,最后a的值应该为1,如果a值大于1,说明无法被分解,返回true。最后还要看我们结果res字符转为整型是否越界,越界的话还是返回0,参见代码如下:

解法一:

classSolution {public:int smallestFactorization(inta) {if (a == 1) return 1;string res = "";for (int k = 9; k >= 2; --k) {while (a % k == 0) {

res= to_string(k) +res;

a/=k;

}

}if (a > 1) return 0;long long num =stoll(res);return num > INT_MAX ? 0: num;

}

};

下面这种方法跟上面解法思路很像,只是结果res没有用字符串,而是直接用的长整型,我们每次在更新完res的结果后,判断一次是否越整型的界,越了就直接返回0,其他部分和上面没有什么区别,参见代码如下:

解法二:

classSolution {public:int smallestFactorization(inta) {if (a < 10) returna;long long res = 0, cnt = 1;for (int i = 9; i >= 2; --i) {while (a % i == 0) {

res+= cnt *i;if (res > INT_MAX) return 0;

a/=i;

cnt*= 10;

}

}return (a == 1) ? res : 0;

}

};

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值