Java Simple and Naïve solution


  • 0
    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                String item = "";
                if (i % 3 == 0) {
                    item += "Fizz";
                }
                if (i % 5 == 0) {
                    item += "Buzz";
                }
                if (item.isEmpty()) {
                    item = "" + i;
                }
                list.add(item);
             }
            return list;
        }
    }
    

Log in to reply
 

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