4ms Java solution O(n)


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

Log in to reply
 

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