Skip to content

Commit 5e095e9

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题17. 打印从1到最大的n位数
1 parent 2ab87c3 commit 5e095e9

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

lcci/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
本书是原谷歌资深面试官的经验之作,帮助了许多想要加入脸书、苹果、谷歌等 IT 名企的求职者拿到 Dream offer。本专题的 100+ 编程面试题是在原书基础上精心挑选出来的,帮助你轻松应战 IT 名企技术面试。
33

44
## [题解](../lcci)
5+
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com/problemset/lcci/),题解由 [doocs/leetcode](https://github.com/doocs/leetcode) 贡献者提供。
56

67
```
78
.

lcof/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
本书精选谷歌、微软等知名IT企业的典型面试题,系统地总结了如何在面试时写出高质量代码,如何优化代码效率,以及分析、解决难题的常用方法。
33

44
## [题解](../lcof)
5+
以下所有题目均来源 [LeetCode 中国官网](https://leetcode-cn.com/problemset/lcof/),题解由 [doocs/leetcode](https://github.com/doocs/leetcode) 贡献者提供。
56

67
| 题号 | 题解 | 难度 |
78
| --- | --- | --- |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [面试题17. 打印从1到最大的n位数](https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/)
2+
3+
## 题目描述
4+
输入数字 `n`,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。
5+
6+
**示例 1:**
7+
8+
```
9+
输入: n = 1
10+
输出: [1,2,3,4,5,6,7,8,9]
11+
```
12+
13+
**说明:**
14+
15+
- 用返回一个整数列表来代替打印
16+
- n 为正整数
17+
18+
## 解法
19+
### Python3
20+
```python
21+
class Solution:
22+
def printNumbers(self, n: int) -> List[int]:
23+
return [i for i in range(1, pow(10, n))]
24+
```
25+
26+
### Java
27+
```java
28+
class Solution {
29+
public int[] printNumbers(int n) {
30+
int nums = (int) Math.pow(10, n);
31+
int[] res = new int[nums - 1];
32+
for (int i = 0; i < nums - 1; ++i) {
33+
res[i] = i + 1;
34+
}
35+
return res;
36+
}
37+
}
38+
```
39+
40+
### ...
41+
```
42+
43+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int[] printNumbers(int n) {
3+
int nums = (int) Math.pow(10, n);
4+
int[] res = new int[nums - 1];
5+
for (int i = 0; i < nums - 1; ++i) {
6+
res[i] = i + 1;
7+
}
8+
return res;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def printNumbers(self, n: int) -> List[int]:
3+
return [i for i in range(1, pow(10, n))]

0 commit comments

Comments
 (0)