大纲
题目
地址
https://leetcode.com/problems/find-the-key-of-the-numbers/description/
内容
You are given three positive integers num1, num2, and num3.
The key of num1, num2, and num3 is defined as a four-digit number such that:
- Initially, if any number has less than four digits, it is padded with leading zeros.
- The ith digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the ith digits of num1, num2, and num3.
Return the key of the three numbers without leading zeros (if any).
Example 1:
Input: num1 = 1, num2 = 10, num3 = 1000
Output: 0
Explanation:
On padding, num1 becomes “0001”, num2 becomes “0010”, and num3 remains “1000”.
The 1st digit of the key is min(0, 0, 1).
The 2nd digit of the key is min(0, 0, 0).
The 3rd digit of the key is min(0, 1, 0).
The 4th digit of the key is min(1, 0, 0).
Hence, the key is “0000”, i.e. 0.
Example 2:
Input: num1 = 987, num2 = 879, num3 = 798
Output: 777
Example 3:
Input: num1 = 1, num2 = 2, num3 = 3
Output: 1
Constraints:
- 1 <= num1, num2, num3 <= 9999
解题
这题就是要求出三个10进制数的每位最小数字,然后组成一个新的数。方法就是从后向前,获取最后一位最小的数字。然后再让每个数字除以10,以让更高的位置的数字变成最后一位数字。
#include <algorithm>
#include <cmath>
using namespace std;
class Solution {
public:
int generateKey(int num1, int num2, int num3) {
int result = 0;
for (int i = 0; i < 4; i++) {
result += min(min(num1 % 10, num2 % 10), num3 % 10) * pow(10, i);
num1 /= 10;
num2 /= 10;
num3 /= 10;
}
return result;
}
};
代码地址
https://github.com/f304646673/leetcode/tree/main/3270-Find-the-Key-of-the-Numbers/cplusplus
标签:key,10,digit,num1,num2,num3,Numbers,3270,Find From: https://blog.csdn.net/breaksoftware/article/details/142353326