python打印从1到最大的n位数

569次阅读
没有评论

python打印从1到最大的n位数

实现目标:

输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。

示例 1:
输入: n = 1
输出: [1,2,3,4,5,6,7,8,9]
说明:
用返回一个整数列表来代替打印
n 为正整数

2、C++

class Solution { /* 2020-9-19 DuYong */
public:
    vector<int> printNumbers(int n) {
        vector<int> result;
        if (n){
            for (int i = 1, max = pow(10, n); i < max; i++){
                result.push_back(i);
            }
        }

        return result;
    }
};

3、python

class Solution: # 2020-9-19 DuYong
    def printNumbers(self, n: int) -> List[int]:
        result = []
        if n:
            for i in range(1, pow(10, n)):
                result.append(i)

        return result
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-10发表,共计612字。
新手QQ群:570568346,欢迎进群讨论 Python51学习