FizzBuzz Solution (Java)


  • 2
    Y

    class Solution {
    public List<String> fizzBuzz(int n) {
    List<String> list = new ArrayList<String>();
    for(int i=1;i<=n;i++)
    {
    String s = i%15==0?"FizzBuzz":(i%3==0||i%5==0?(i%3==0?"Fizz":"Buzz"):String.valueOf(i));
    list.add(s);
    }
    return list;
    }
    }


  • 0
    U

    It is not a best practice to use more than one ternary operator in one line of code because it reduced code readability.


Log in to reply
 

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