lintcode-medium-Print Numbers by Recursion

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

Notice

It‘s pretty easy to do recursion like:

recursion(i) {
    if i > largest number:
        return
    results.add(i)
    recursion(i + 1)
}

however this cost a lot of recursion memory as the recursion depth maybe very large. Can you do it in another way to recursive with at most N depth?

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].

Challenge

Do it in recursion, not for-loop.

public class Solution {
    /**
     * @param n: An integer.
     * return : An array storing 1 to the largest number with n digits.
     */
    public List<Integer> numbersByRecursion(int n) {
        // write your code here
        List<Integer> result = new ArrayList<Integer>();
        if(n < 1)
            return result;

        if(n == 1){
            for(int i = 1; i <= 9; i++)
                result.add(i);
            return result;
        }

        List<Integer> start = numbersByRecursion(n - 1);
        result.addAll(start);
        result.add(result.get(result.size() - 1) + 1);

        int factor = 1;
        for(int i = 1; i < n; i++)
            factor *= 10;

        for(int i = 1; i <= 9; i++){
            List<Integer> next = new ArrayList<Integer>();
            for(int j = 0; j < start.size(); j++)
                next.add(i * factor + start.get(j));

            result.addAll(next);
            if(i < 9)
                result.add(result.get(result.size() - 1) + 1);
        }

        return result;
    }
}
时间: 2024-08-14 03:00:50

lintcode-medium-Print Numbers by Recursion的相关文章

Lintcode371 Print Numbers by Recursion solution 题解

[题目描述] Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do recursion like: recursion(i) { if i > largest number: return results.add(i) recursion(i + 1) } however this cost a lot of recursion memory as

Print Numbers by Recursion

Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do recursion like: recursion(i) { if i > largest number: return results.add(i) recursion(i + 1) } however this cost a lot of recursion memory as the rec

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[Lintcode] Add Two Numbers I &amp;&amp; II

Add Two Numbers You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and ret

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,