java recursive StackOverflow


  • -1
    P

    The stackoverflow problem will occur when n = 9999......well, perhaps I shuold not use recursion in this problem(>_<)
    public class Solution {
    LinkedList<String> list = new LinkedList<String>();
    public List<String> fizzBuzz(int n) {
    if(n == 0)
    {
    return list;
    }
    if(n%3 == 0&&n%5 == 0)
    {
    list.addFirst("FizzBuzz");
    }
    else if(n%3 == 0)
    {
    list.addFirst("Fizz");
    }
    else if(n%5 == 0)
    {
    list.addFirst("Buzz");
    }
    else
    {
    list.addFirst(String.valueOf(n));
    }
    return fizzBuzz(n-1);
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.