[LeetCode] Find the Key of the Numbers

3270. Find the Key of the Numbers

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func generateKey(num1, num2, num3 int) int {
res := 0
po := 1

for num1 != 0 || num2 != 0 || num3 != 0 {
digit1 := num1 % 10
digit2 := num2 % 10
digit3 := num3 % 10

minDigit := min(digit1, digit2, digit3)

res += po * minDigit
num1 /= 10
num2 /= 10
num3 /= 10
po *= 10
}

return res
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/01/PS/LeetCode/find-the-key-of-the-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.