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

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 class Solution {
 2 private:
 3     bool containZero(int i) {
 4         while (i > 0) {
 5             if (i % 10 == 0)
 6                 return true;
 7             else 
 8                 i /= 10;
 9         }
10         return false;
11     }
12 public:
13     vector<int> getNoZeroIntegers(int n) {
14         int i = 1;
15         for (; i < n; ++i) {
16             if (!containZero(i) && !containZero(n - i)) {
17                 break;
18             }
19         }
20         return {i, n - i};
21     }
22 };

python3:

1 class Solution:
2     def getNoZeroIntegers(self, n: int) -> List[int]:
3         return next([a, n-a] for a in range(1,n) if '0' not in f'{a}{n-a}')

猜你喜欢

转载自www.cnblogs.com/qinduanyinghua/p/12186307.html