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
andB
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:
1 2 3 |
<strong>Input:</strong> n = 2 <strong>Output:</strong> [1,1] <strong>Explanation:</strong> A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation. |
Example 2:
1 2 |
<strong>Input:</strong> n = 11 <strong>Output:</strong> [2,9] |
Example 3:
1 2 |
<strong>Input:</strong> n = 10000 <strong>Output:</strong> [1,9999] |
Example 4:
1 2 |
<strong>Input:</strong> n = 69 <strong>Output:</strong> [1,68] |
Example 5:
1 2 |
<strong>Input:</strong> n = 1010 <strong>Output:</strong> [11,999] |
Constraints:
2 <= n <= 10^4
Solution: Brute Force
Time complexity: O(nlogn)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Author: Huahua class Solution { public: vector<int> getNoZeroIntegers(int n) { auto valid = [](int x) { if (!x) return false; while (x) { if (x % 10 == 0) return false; x /= 10; } return true; }; for (int i = 1; i <= n / 2; ++i) if (valid(i) && valid(n - i)) return {i, n - i}; return {}; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment