美文网首页
412. Fizz Buzz

412. Fizz Buzz

作者: matrxyz | 来源:发表于2018-01-16 12:40 被阅读0次

    Write a program that outputs the string representation of numbers from 1 to n.
    But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

    Example:
    
    n = 15,
    
    Return:
    [
        "1",
        "2",
        "Fizz",
        "4",
        "Buzz",
        "Fizz",
        "7",
        "8",
        "Fizz",
        "Buzz",
        "11",
        "Fizz",
        "13",
        "14",
        "FizzBuzz"
    ]
    

    Solution:

    思路:

    Time Complexity: O(N) Space Complexity: O(1)

    Solution Code:

    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> ret = new ArrayList<String>(n);
            for(int i=1,fizz=0,buzz=0;i<=n ;i++){
                fizz++;
                buzz++;
                if(fizz==3 && buzz==5){
                    ret.add("FizzBuzz");
                    fizz=0;
                    buzz=0;
                }else if(fizz==3){
                    ret.add("Fizz");
                    fizz=0;
                }else if(buzz==5){
                    ret.add("Buzz");
                    buzz=0;
                }else{
                    ret.add(String.valueOf(i));
                }
            } 
            return ret;
        }
    }
    

    相关文章

      网友评论

          本文标题:412. Fizz Buzz

          本文链接:https://www.haomeiwen.com/subject/ildsoxtx.html