[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,参见代码如下:
解法一:
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 最小因数分解的更多相关文章
- 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 ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- LeetCode:长度最小的子数组【209】
LeetCode:长度最小的子数组[209] 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [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 ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [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. ...
- [LeetCode] Minimum Genetic Mutation 最小基因变化
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
随机推荐
- ArrayList和Vector区别
java基础之ArrayList和Vector的主要区别: List接口下一共实现了三个类:ArrayList,Vector,LinkedList. LinkedList主要保持数据的插入顺序的时候使 ...
- 使用 Except 和 Intersect
做了一个如下的小厕所,如果我需要得到返回是 d,f 那我需要用那组语句呢? A: ;WITH CA AS( SELECT * FROM (VALUES('a'),('b'),('c'),('d'))a ...
- Beta敏捷冲刺每日报告——Day5
1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.6 00:00 -- 2017.11.7 00:00 讨论时间地点 2017.11.6 早9:30,电话会议会议 ...
- Linux下C编写基本的多线程socket服务器
不想多说什么,会搜这些东西的都是想看代码的吧. 一开始不熟悉多线程的时候还在想怎么来控制一个线程的结束,后来发现原来有pthread_exit()函数可以直接在线程函数内部调用结束这个线程. 开始还想 ...
- QTableView
QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型 QStandardItemModel *student_model = ne ...
- 视图和URL配置
视图和URL配置 实验简介 上一章里我们介绍了如何创建一个Django项目并启动Django的开发服务器.本章你将学到用Django创建动态网页的基本知识. 同时,也教会大家怎么在本地机器上建立一个独 ...
- maven创建web工程
使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...
- .Net Core SignalR 实时推送信息
以前一直没用成功过SignalR(.net asp),最近几天又参考了对应的文档,最终调成功啦. 开始之前,应该注意: 一定要.Net Core 2.1.0以上的SDK. VS2017 15.6以上的 ...
- Aache的虚拟主机配置虚拟目录
3. 打开 httpd.conf 文件, 添加如下代码: # Virtual hosts Include conf/extra/httpd-vhosts.conf 如果已存在,将Include前面的# ...
- angular2 学习笔记 の 移动端开发 ( 手势 )
更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...