LeetCode Minimum Factorization

625. 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

 1 public class Solution {
 2     int[] aa = new int[100];
 3     int j = 0;
 4     boolean flag = true;
 5     public int smallestFactorization(int a) {
 6         smallest(a);
 7         Arrays.sort(aa);
 8         String string = "";
 9         if(j > 31 || !flag) return 0;
10         for(int i = 0; i < 100; i++) {
11             if(aa[i] != 0) {
12                 string = string + aa[i];
13             }
14         }
15         if(string != "") {
16             Long temp = Long.parseLong(string);
17             if(temp > Integer.MAX_VALUE) {
18                 return 0;
19             } else {
20                 return Integer.parseInt(string);
21             }
22         } else
23         return 0;
24     }
25     public void smallest(int a) {
26         if(a <= 9) {
27             aa[j++] = a;
28             return ;
29         }
30         for(int i = 9; i >=2 ; i--) {
31             if(a % i == 0) {
32                 aa[j++] = i;
33                 smallest(a/i);
34                 return;
35             }
36         }
37         flag = false;
38         return ;
39     }
40 }
时间: 2024-08-26 01:20:12

LeetCode Minimum Factorization的相关文章

[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 1Input: 48 Output: 68 Example 2Input: 15

LeetCode: Minimum Window Substring [076]

[题目] Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such

leetcode -625-Minimum Factorization

625. 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 1Input: 48 Out

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or r

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 题意: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 解题思路:分几种情况考虑:1,树为空,

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" M