Description#
No-Zero integer is a positive integer that does not contain any 0
in its decimal representation.
Given an integer n
, return a list of two integers [a, b]
where:
a
and b
are No-Zero integers.a + b = n
The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
Example 1:
Input: n = 2
Output: [1,1]
Explanation: Let a = 1 and b = 1.
Both a and b are no-zero integers, and a + b = 2 = n.
Example 2:
Input: n = 11
Output: [2,9]
Explanation: Let a = 2 and b = 9.
Both a and b are no-zero integers, and a + b = 9 = n.
Note that there are other valid answers as [8, 3] that can be accepted.
Constraints:
Solutions#
Solution 1#
1
2
3
4
5
6
| class Solution:
def getNoZeroIntegers(self, n: int) -> List[int]:
for a in range(1, n):
b = n - a
if "0" not in str(a) + str(b):
return [a, b]
|
1
2
3
4
5
6
7
8
9
10
| class Solution {
public int[] getNoZeroIntegers(int n) {
for (int a = 1;; ++a) {
int b = n - a;
if (!(a + "" + b).contains("0")) {
return new int[] {a, b};
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
| class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
for (int a = 1;; ++a) {
int b = n - a;
if ((to_string(a) + to_string(b)).find('0') == -1) {
return {a, b};
}
}
}
};
|
1
2
3
4
5
6
7
8
| func getNoZeroIntegers(n int) []int {
for a := 1; ; a++ {
b := n - a
if !strings.Contains(strconv.Itoa(a)+strconv.Itoa(b), "0") {
return []int{a, b}
}
}
}
|
Solution 2#
1
2
3
4
5
6
7
8
9
10
11
12
13
| class Solution:
def getNoZeroIntegers(self, n: int) -> List[int]:
def f(x):
while x:
if x % 10 == 0:
return False
x //= 10
return True
for a in range(1, n):
b = n - a
if f(a) and f(b):
return [a, b]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| class Solution {
public int[] getNoZeroIntegers(int n) {
for (int a = 1;; ++a) {
int b = n - a;
if (f(a) && f(b)) {
return new int[] {a, b};
}
}
}
private boolean f(int x) {
for (; x > 0; x /= 10) {
if (x % 10 == 0) {
return false;
}
}
return true;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
auto f = [](int x) {
for (; x; x /= 10) {
if (x % 10 == 0) {
return false;
}
}
return true;
};
for (int a = 1;; ++a) {
int b = n - a;
if (f(a) && f(b)) {
return {a, b};
}
}
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| func getNoZeroIntegers(n int) []int {
f := func(x int) bool {
for ; x > 0; x /= 10 {
if x%10 == 0 {
return false
}
}
return true
}
for a := 1; ; a++ {
b := n - a
if f(a) && f(b) {
return []int{a, b}
}
}
}
|