3ms Java solution


  • 0
    A

    public class Solution {

    public List<String> fizzBuzz(int n) {
            String[] str = new String[n];
            for (int i = 0; i < n; i++) { 
                if ((i + 1) % 3 == 0) {
                    if ((i+1) % 5 == 0) {
                        str[i] = "FizzBuzz";
                    } else str[i] = "Fizz";
                } else if ((i+1) % 5 == 0) {
                    str[i] = "Buzz";
                }else str[i] = Integer.toString(i + 1);
            }
            return Arrays.asList(str);
        }

    }


Log in to reply
 

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