leetcode 625. Minimum Factorization
来源:互联网 发布:强制解锁软件 编辑:程序博客网 时间:2024/06/05 15:26
题目描述
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
解题思路
简单描述一下题目,给定一个正整数,找到一个最小的正整数,使得该最小正整数的各位乘积等于上述给定的正整数,如果这个数超过32位int的表示范围,就返回0.
这题其实很简单的,但没做出来确实罪过。首先,各位的乘积,就表示不可能出现超过1位的数,然后考虑最小,如果数最小,那么就一定是小的数的乘积在前面,只需要从9开始不断地对原数做除法,迭代递减至2就可以了,这样就一定保证了最小的乘积在最前面,如果最后的余数大于9,那就证明不存在这样的解,直接return 0.
还有一点,从得到的乘积恢复成数的过程中,判断是不是int溢出其实很简单,在C++里定义一个long,可以存储大于INT_MAX的值,直接判断就OK。这个技巧可以用在很多类似的判断是否溢出的题目中。
class Solution {public: int smallestFactorization(int a) { // absolutely an easy problem if(a<10) return a; vector<int> result; for(int i = 9;i>1;--i) { while(a%i==0){ a = a/i; result.push_back(i); } } if(a>10)return 0; long retVal = 0; for(int i = result.size()-1;i>=0;--i) { retVal = retVal*10+result[i]; if(retVal>INT_MAX) return 0; } return retVal; }};
阅读全文
0 0
- leetcode-625. Minimum Factorization
- leetcode 625. Minimum Factorization
- leetcode 625. Minimum Factorization
- LeetCode Minimum Factorization
- 625. Minimum Factorization
- Minimum Factorization
- 625. Minimum Factorization & 621. Task Scheduler
- 625Minimum Factorization
- Factorization
- LeetCode: Minimum Path Sum
- LeetCode: Minimum Window Substring
- LeetCode Minimum Path Sum
- LeetCode Minimum Window Substring
- LeetCode : Minimum Path Sum
- LeetCode : Minimum Window Substring
- [Leetcode] Minimum Path Sum
- [LeetCode] Minimum Path Sum
- [LeetCode] Minimum Window Substring
- C++避免编译警告unused
- Codeforces#419 Karen and Coffee
- javascript数组的方法
- 安装 Homebrew遇到的问题
- Done with "file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.9-1.el6
- leetcode 625. Minimum Factorization
- iptabes打开指定端口
- GIT 不受版本控制
- 使用本地广播 LocalBroadcastReceiver
- sql
- Leetcode:207. Course Schedule
- POJ
- PAT甲级真题及训练集(10)--1036. Boys vs Girls (25)
- Kotlin Reference (八) 可见性修饰符, data class,object Claz