列表

详情


3270. 求出数字答案

给你三个  整数 num1 ,num2 和 num3 。

数字 num1 ,num2 和 num3 的数字答案 key 是一个四位数,定义如下:

请你返回三个数字 没有 前导 0 的数字答案。

 

示例 1:

输入:num1 = 1, num2 = 10, num3 = 1000

输出:0

解释:

补前导 0 后,num1 变为 "0001" ,num2 变为 "0010" ,num3 保持不变,为 "1000" 。

所以数字答案为 "0000" ,也就是 0 。

示例 2:

输入: num1 = 987, num2 = 879, num3 = 798

输出:777

示例 3:

输入:num1 = 1, num2 = 2, num3 = 3

输出:1

 

提示:

相似题目

最大数

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int generateKey(int num1, int num2, int num3) { } };

rust 解法, 执行用时: 0 ms, 内存消耗: 2.3 MB, 提交时间: 2025-01-11 10:26:19

impl Solution {
    pub fn generate_key(mut x: i32, mut y: i32, mut z: i32) -> i32 {
        let mut ans = 0;
        let mut pow10 = 1;
        while x > 0 && y > 0 && z > 0 {
            ans += (x % 10).min(y % 10).min(z % 10) * pow10;
            x /= 10;
            y /= 10;
            z /= 10;
            pow10 *= 10;
        }
        ans
    }
}

javascript 解法, 执行用时: 2 ms, 内存消耗: 52.1 MB, 提交时间: 2025-01-11 10:26:06

/**
 * @param {number} num1
 * @param {number} num2
 * @param {number} num3
 * @return {number}
 */
var generateKey = function(x, y, z) {
    let ans = 0;
    for (let pow10 = 1; x && y && z; pow10 *= 10) {
        ans += Math.min(x % 10, y % 10, z % 10) * pow10;
        x = Math.floor(x / 10);
        y = Math.floor(y / 10);
        z = Math.floor(z / 10);
    }
    return ans;
};

golang 解法, 执行用时: 0 ms, 内存消耗: 2.4 MB, 提交时间: 2024-09-09 09:10:16

func generateKey(x, y, z int) (ans int) {
	for pow10 := 1; x > 0 && y > 0 && z > 0; pow10 *= 10 {
		ans += min(x%10, y%10, z%10) * pow10
		x /= 10
		y /= 10
		z /= 10
	}
	return
}

java 解法, 执行用时: 0 ms, 内存消耗: 39.9 MB, 提交时间: 2024-09-09 09:09:53

class Solution {
    int generateKey(int x, int y, int z) {
        int ans = 0;
        for (int pow10 = 1; x > 0 && y > 0 && z > 0; pow10 *= 10) {
            ans += Math.min(Math.min(x % 10, y % 10), z % 10) * pow10;
            x /= 10;
            y /= 10;
            z /= 10;
        }
        return ans;
    }
}

cpp 解法, 执行用时: 0 ms, 内存消耗: 8.1 MB, 提交时间: 2024-09-09 09:09:41

class Solution {
public:
    int generateKey(int x, int y, int z) {
        int ans = 0;
        for (int pow10 = 1; x && y && z; pow10 *= 10) {
            ans += min({x % 10, y % 10, z % 10}) * pow10;
            x /= 10;
            y /= 10;
            z /= 10;
        }
        return ans;
    }
};

python3 解法, 执行用时: 30 ms, 内存消耗: 16.5 MB, 提交时间: 2024-09-09 09:09:26

class Solution:
    def generateKey(self, x: int, y: int, z: int) -> int:
        ans = 0
        pow10 = 1
        while x and y and z:
            ans += min(x % 10, y % 10, z % 10) * pow10
            x //= 10
            y //= 10
            z //= 10
            pow10 *= 10
        return ans

上一题