AC java iteration. Is this bad coding style?


  • 0
    R

    I thought that we don't need to check for "FizzBuzz" specifically. So just add Buzz after Fizz if it's multiple of 3 and 5.

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

Log in to reply
 

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