Given an integer n. No-Zero integer is a positive integer which doesn't contain any 0 in its decimal representation.
Return a list of two integers [A, B] where:
A and B are No-Zero integers.
A + B = n
It's guarateed 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 don't contain any 0 in their decimal representation.
Example 2:
Input: n = 11
Output: [2,9]
Example 3:
Input: n = 10000
Output: [1,9999]
Example 4:
Input: n = 69
Output: [1,68]
Example 5:
Input: n = 1010
Output: [11,999]
Constraints:
2 <= n <= 10^4
这题卡住了我一段时间。后来直接遍历每个数字了。我们只要求出一个减数就行了,保证减数和留下来的那位数字不为0。
数字小于等于1的,那么减数的位数我们置为数字+1,然后我们减去相应的值,然后再去遍历下一位的数字,循环往复。
假设到了最后一位,那么假设减数已经有值了,那么直接跳过生成即可。当然我觉得我这种方法算是笨办法,应当还有更加直接的方法,毕竟这题的难度是Easy。
class ConvertIntegerToTheSumOfTwoNoZeroIntegers : public Solution {
public:
void Exec() {
}
vector<int> getNoZeroIntegers(int n) {
vector<int> borrows;
int num = n;
while (num != 0) {
int sub = num % 10;
if (sub <= 1) {
if (num / 10 == 0 && !borrows.empty()) {
break;
}
num -= sub + 1;
borrows.push_back(sub + 1);
} else {
num -= 1;
borrows.push_back(1);
}
num /= 10;
}
int fv = 0;
for (int i = borrows.size() - 1; i >= 0; i--) {
fv *= 10;
fv += borrows[i];
}
return vector<int>{n - fv, fv};
}
};