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,参见代码如下:

解法一:

class Solution {
public:
int smallestFactorization(int a) {
if (a == ) return ;
string res = "";
for (int k = ; k >= ; --k) {
while (a % k == ) {
res = to_string(k) + res;
a /= k;
}
}
if (a > ) return ;
long long num = stoll(res);
return num > INT_MAX ? : num;
}
};

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

解法二:

class Solution {
public:
int smallestFactorization(int a) {
if (a < ) return a;
long long res = , cnt = ;
for (int i = ; i >= ; --i) {
while (a % i == ) {
res += cnt * i;
if (res > INT_MAX) return ;
a /= i;
cnt *= ;
}
}
return (a == ) ? res : ;
}
};

参考资料:

https://discuss.leetcode.com/topic/92920/concise-c-solution-10-lines-3ms

https://discuss.leetcode.com/topic/92998/c-clean-code-7-line-3-solutions/2

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Factorization 最小因数分解的更多相关文章

  1. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  2. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  3. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  4. LeetCode:长度最小的子数组【209】

    LeetCode:长度最小的子数组[209] 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 ...

  5. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  6. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  9. [LeetCode] Minimum Genetic Mutation 最小基因变化

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

随机推荐

  1. [poj2406]Power Strings_hash

    Power Strings poj-2406 题目大意:询问一个字符串最多几个相同且连续的字符串构成(Eg:abababab由4个构成,abcd由1个构成). 注释:字符串长度为n,$1\le n\l ...

  2. 移动端Web资源整合

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...

  3. C语言博客作业—指针

    一.PTA实验作业 题目1: 求出数组中最大数和次最大数 1. 本题PTA提交列表 2. 设计思路 定义max表示范围数组中的最大数(初值设为a[0]),z表示找到的元素在数组中的位置: 定义指针*b ...

  4. 201621123062《java程序设计》第七周作业总结

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 1.布局管理器的具体使用方法 2.事件处理模型及其代码的编写 3.Swing中的常用组件 4. ...

  5. selenium 爬虫

    from selenium import webdriver import time driver = webdriver.PhantomJS(executable_path="D:/pha ...

  6. 要学好JAVA要注意些什么?

    从自学开始到参加系统的学习JAVA已经差不多有1个月了的时间了,在这段时间以前我也和很多人一样在网上盲目的搜罗一些视频来自己啃,随着时间的积累,对JAVA的认识也有了一定的提升,之前可能因为在IT咨询 ...

  7. DML数据操作语言之查询(一)

    1.select语句基础 基本语句格式:  select <列名>,.... from <表名>; select子句中列举出希望从表中查询出的列的名称,from子句则指定了选取 ...

  8. bzoj千题计划244:bzoj3730: 震波

    http://www.lydsy.com/JudgeOnline/problem.php?id=3730 点分树内对每个节点动态维护2颗线段树 线段树以距离为下标,城市的价值为权值 对于节点x的两棵线 ...

  9. 技术文档分享_linux中生成考核用的GPT分区表结构修复

    注:历史版本,后期改用python实现了 实验一: 目的:用于生成大量模拟破坏GPT分区结构案例,并生成唯一方式修复后的评判方法.故障:在一个完整的GPT分区磁盘上,丢失了GPT主分区表,或备份分区表 ...

  10. EXT3文件系统误删除导致文件系统中的邮件丢失恢复方法

    一.故障描述 由8块盘组成的RAID5, 上层是EXT3文件系统,由于误删除导致文件系统中的邮件丢失 二.镜像磁盘为防止数据恢复过程中由于误操作对原始磁盘造成二次破坏, 使用winhex软件为每块磁盘 ...