BiruLyu
8/5/2017 - 5:56 AM

412. Fizz Buzz(#).java

import java.util.*;
public class Solution {
    static List<String> answer = new AbstractList<String>() {
            public String get(int i) {
                i++;
                return i % 15 == 0 ? "FizzBuzz" :
                i % 5 == 0 ? "Buzz" :
                i % 3 == 0 ? "Fizz" :
                Integer.toString(i);
            }
            
            public int size() {
                return Integer.MAX_VALUE;
            }
        };
    
    public List<String> fizzBuzz(int n) {
        return answer.subList(0, n);
    }
}
public class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> res = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            String temp = "";
            if (i % 3 == 0) {
                temp += "Fizz";
            }
            if (i % 5 == 0) {
                temp += "Buzz";
            } 
            if (temp.length() < 1) {
                temp += i;
            }
            res.add(temp);
        }
        return res;
    }
}