Java 3ms Solution using Ternary Operator '?'


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

Log in to reply
 

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