java 6 line solution


  • 0
    X

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


  • 0
    K

    I'm think you can shorten it by one more line by omitting the variable declaration and initialization.
    i.e list.add(i%3 == 0 && i%5 == 0 ? "FizzBuzz" : i%3 == 0 ? "Fizz" : i%5 == 0 ? "Buzz" : String.valueOf(i))


Log in to reply
 

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