Input: n =11Output: [2,9]Explanation: Let a =2 and b =9.Both a and b are no-zero integers, and a + b =11= n.Note that there are other valid answers as [8,3] that can be accepted.
A no-zero integer is one that does not contain the digit 0. We can try all possible pairs (a, b) such that a + b = n and check if both a and b are no-zero integers. Since n is small, this brute-force approach is efficient enough.
classSolution {
publicint[]getNoZeroIntegers(int n) {
for (int a = 1; a < n; ++a) {
int b = n - a;
if (!String.valueOf(a).contains("0") &&!String.valueOf(b).contains("0"))
returnnewint[]{a, b};
}
returnnewint[0];
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
fungetNoZeroIntegers(n: Int): IntArray {
for (a in1 until n) {
val b = n - a
if (!a.toString().contains('0') && !b.toString().contains('0'))
return intArrayOf(a, b)
}
return intArrayOf()
}
}
1
2
3
4
5
6
7
classSolution:
defgetNoZeroIntegers(self, n: int) -> list[int]:
for a in range(1, n):
b = n - a
if'0'notin str(a) and'0'notin str(b):
return [a, b]
return []
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnget_no_zero_integers(n: i32) -> Vec<i32> {
for a in1..n {
let b = n - a;
if!a.to_string().contains('0') &&!b.to_string().contains('0') {
returnvec![a, b];
}
}
vec![]
}
}