1317. Convert Integer to the Sum of Two No-Zero Integers

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: A = 1, B = 1. A + B = n and both A and B do not contain any 0 in their decimal representation.
``````

Example 2:

``````Input: n = 11
Output: [2,9]
``````

Constraints:

• `2 <= n <= 10^4`

``````class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
for (int i = 1; i < n; ++i) {
if (!hasZero(i) && !hasZero(n - i)) {
return {i, n - i};
}
}
return {-1, -1};
}
bool hasZero(int n) {
while (n > 0) {
if (n % 10 == 0) return true;
n /= 10;
}
return false;
}
};
``````

``````class Solution {
public:
vector<int> getNoZeroIntegers(int n) {
int a = 0, b = 0, step = 1;
while (n > 0) {
int d = n % 10;
n /= 10;
if ((d == 0 || d == 1) && n > 0) {
a += step * (1 + d);
b += step * 9;
--n;
} else {
a += step * 1;
b += step * (d - 1);
}
step *= 10;
}
return {a, b};
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1317

https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/

https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/solutions/478216/java-intuitive-non-brute-force/

https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/solutions/478189/java-simple-solution-beats-100/

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation