Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 354a56f

Browse files
committed
Create 1317. 将整数转换为两个无零整数的和.md
1 parent 1139457 commit 354a56f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [1317. 将整数转换为两个无零整数的和](https://leetcode.cn/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)
2+
3+
- 标签:数学
4+
- 难度:简单
5+
6+
## 题目大意
7+
8+
**描述**:给定一个整数 $n$。
9+
10+
**要求**:返回一个由两个整数组成的列表 $[A, B],ドル满足:
11+
12+
- $A$ 和 $B$ 都是无零整数。
13+
- $A + B = n$。
14+
15+
**说明**:
16+
17+
- **无零整数**:十进制表示中不含任何 0ドル$ 的正整数。
18+
- 题目数据保证至少一个有效的解决方案。
19+
- 如果存在多个有效解决方案,可以返回其中任意一个。
20+
- 2ドル \le n \le 10^4$。
21+
22+
**示例**:
23+
24+
- 示例 1:
25+
26+
```Python
27+
输入:n = 2
28+
输出:[1,1]
29+
解释:A = 1, B = 1. A + B = n 并且 A 和 B 的十进制表示形式都不包含任何 0
30+
```
31+
32+
- 示例 2:
33+
34+
```Python
35+
输入:n = 11
36+
输出:[2,9]
37+
```
38+
39+
## 解题思路
40+
41+
### 思路 1:枚举
42+
43+
1. 由于给定的 $n$ 范围为 $[1, 10000],ドル比较小,我们可以直接在 $[1, n)$ 的范围内枚举 $A,ドル并通过 $n - A$ 得到 $B$。
44+
2. 在判断 $A$ 和 $B$ 中是否都不包含 0ドル$。如果都不包含 0ドル,ドル则返回 $[A, B]$。
45+
46+
### 思路 1:代码
47+
48+
```Python
49+
class Solution:
50+
def getNoZeroIntegers(self, n: int) -> List[int]:
51+
for A in range(1, n):
52+
B = n - A
53+
if '0' not in str(A) and '0' not in str(B):
54+
return [A, B]
55+
```
56+
57+
### 思路 1:复杂度分析
58+
59+
- **时间复杂度**:$O(n \times \log n)$。
60+
- **空间复杂度**:$O(1)$。
61+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /