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. java 语法分析器 括号匹配

    package test; import java.util.*;public class Test {    public String text="fewe{f(sdd(f)a[j]sd ...

  2. x64系统安装ODAC问题经验分享

    64bit系统安装ODAC经验分享 背景: 最近项目里面有用到 WCF+Entity Framework+oracle 这个架构用过的朋友应该都知道,Entity Framework要通过ODAC的方 ...

  3. beta冲刺6-咸鱼

    前言:此篇是补昨天凌晨的.后面有更新但是太晚了就没有即使更新.所以现在过来更新一下. 昨天的未完成: 用户测试+测试报告 目前剩下的功能点:输入内容检测 我的社团输出显示格式调整. 今天的完成: 我的 ...

  4. 简单的C语言编译器--语法分析器

      语法分析算是最难的一部分了.总而言之,语法分析就是先设计一系列语法,然后再用设计好的语法去归约词法分析中的结果.最后将归约过程打印出来,或者生成抽象语法树. 1. 设计文法 以下是我的文法(引入的 ...

  5. shell中冒号 : 用途说明

    我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等.其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令.只起到占一 ...

  6. MySQL InnoDB锁机制

    概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...

  7. vue 保留两位小数 不能直接用toFixed(2) ?

    用vue做项目的时候多多少少都会遇到这个问题 刚开始我是用toFixed()这个方法来写的  效果是有的 但是控制台一直是红红的围绕着我 突然想到 vue和jquery混搭 的 问题 于是乎 看了一下 ...

  8. 常用Mysql数据库操作语句

    用户管理: 1.新建用户: 语法msyql>CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username - 你将创建 ...

  9. Hangfire使用ApplicationInsigts监控

    起因 我司目前使用清真的ApplicationInsights(以下简称Ai)来做程序级监控.(Ai相关文档: https://azure.microsoft.com/zh-cn/services/a ...

  10. vue项目中的常见问题

    总结了几个vue项目开发过程中遇到的常见问题,希望大家注意. 注:文末有福利! 一.样式问题 1.vue中使用less 安装less依赖 npm install less less-loader -- ...