Java solution without mod


  • 0
    M
    public List<String> fizzBuzz(int n) {
            List<String> res = new ArrayList<>();
            if(n < 1) {
                return res;
            }
            for(int i = 1; i <= n; i ++) {
                if(i == 15 || (i > 15 && res.get(i - 16).equals("FizzBuzz"))) {
                    res.add("FizzBuzz");
                }else if(i == 3 || (i > 3 && (res.get(i - 4).equals("Fizz") || res.get(i - 4).equals("FizzBuzz")))) {
                    res.add("Fizz");
                }else if(i == 5 || (i > 5 && (res.get(i - 6).equals("Buzz") || res.get(i - 6).equals("FizzBuzz")))) {
                    res.add("Buzz");
                }else {
                    res.add(String.valueOf(i));
                }
            }
            return res;
        }
    

Log in to reply
 

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