成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LintCode] Print Numbers by Recursion

kumfo / 2991人閱讀

摘要:只有當(dāng)位數(shù)時(shí),才打印數(shù)字。首先分析邊界,應(yīng)該,然后用存最高位。用函數(shù)對(duì)進(jìn)行遞歸運(yùn)算,同時(shí)更新結(jié)果數(shù)組。更新的過程歸納一下,首先,計(jì)算最高位存入,然后,用到倍的和之前里已經(jīng)存入的所有的數(shù)個(gè)循環(huán)相加,再存入,更新,計(jì)算更高位直到等于

Problem

Print numbers from 1 to the largest number with N digits by recursion.

Example

Given N = 1, return [1,2,3,4,5,6,7,8,9].

Given N = 2, return [1,2,3,4,5,6,7,8,9,10,11,12,...,99].

Note

只有當(dāng)位數(shù)n >= 0時(shí),才打印數(shù)字。首先分析邊界n = 0,應(yīng)該return 1,然后用base存最高位。用helper()函數(shù)對(duì)base進(jìn)行遞歸運(yùn)算,同時(shí)更新結(jié)果數(shù)組res。
更新res的過程:

res = {1, 2, 3, 4, 5, 6, 7, 8, 9};
base = helper(n-1, res); //10
//i = 1 ~ 9;
for i:
curbase = i * base; //10, 20, 30, 40, ... , 80, 90
res.add(curbase); // 10; 20; ... ; 80; 90
//j = index of res;
for j:
res.add(curbase + res.get(j)); //11, 12, 13, ... , 18, 19;
                               //21, 22, 23, ... , 28, 29;
                               //...

歸納一下,首先,計(jì)算最高位存入base,然后,用1到9倍的base(curbase)和之前res里已經(jīng)存入的所有的數(shù)(res.size()個(gè))循環(huán)相加,再存入res,更新res.size,計(jì)算更高位直到base等于10^n...

Solution

Recursion

public class Solution {
    public List numbersByRecursion(int n) {
        List res = new ArrayList();
        if (n >= 0) {
            helper(n, res);
        }
        return res;
    }
    public int helper(int n, List res) {
        if (n == 0) return 1;
        int base = helper(n-1, res);
        int size = res.size();
        for (int i = 1; i <= 9; i++) {
            int curbase = i * base;
            res.add(curbase);
            for (int j = 0; j < size; j++) {
                res.add(curbase + res.get(j));
            }
        }
        return base * 10;
    }
}

Non-recursion

public class Solution {
    public List numbersByRecursion(int n) {
        List res = new ArrayList();
        int max = 1;
        while (n != 0) {
            max *= 10;
            n--;
        }
        for (int i = 1; i < max; i++) {
            res.add(i);
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/65580.html

相關(guān)文章

  • [LintCode/LeetCode] Combination Sum I & II

    摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時(shí)將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...

    ThreeWords 評(píng)論0 收藏0
  • [LintCode] Ugly Number

    Problem Write a program to check whether a given number is an ugly number`. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly ...

    raise_yang 評(píng)論0 收藏0
  • [LintCode] Max Tree

    Problem Given an integer array with no duplicates. A max tree building on this array is defined as follow: The root is the maximum number in the arrayThe left subtree and right subtree are the max tre...

    afishhhhh 評(píng)論0 收藏0
  • [LintCode/LeetCode] Binary Tree Serialization

    摘要:這里要注意的是的用法。所以記住,用可以從自動(dòng)分離出數(shù)組。跳過第一個(gè)元素并放入數(shù)組最快捷語句建立的用意記錄處理過的結(jié)點(diǎn)并按處理所有結(jié)點(diǎn)和自己的連接下面先通過判斷,再修改的符號(hào)的順序,十分巧妙更輕便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...

    keithyau 評(píng)論0 收藏0
  • Understanding Recursion

    Recursion, simply put, is calling a function on itself. It can used to break down complex problems into smaller manageable similar units that can be handled by the same function. Recursion vs Iteratio...

    HtmlCssJs 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<