面试题17. 打印从1到最大的n位数。1星

#问题:输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。
#示例:输入: n = 1
输出: [1,2,3,4,5,6,7,8,9]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/compress-string-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处

class Solution {
    public int[] printNumbers(int n) {
        int maxnum = (int)Math.pow(10,n);
        int[] result = new int[maxnum-1];
        for(int i=0;i<maxnum-1;i++){
            result[i] = i+1;
        }
        return result;
    }
}

Math.pow(m,n)返回m的n次方。

发布了18 篇原创文章 · 获赞 0 · 访问量 334

猜你喜欢

转载自blog.csdn.net/qq_44787671/article/details/104431198