Leetcode 412. Fizz Buzz-蜂鸣器

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 412. Fizz Buzz-蜂鸣器相关的知识,希望对你有一定的参考价值。

Given an integer n, return a string array answer (1-indexed) where:

  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • answer[i] == "Fizz" if i is divisible by 3.
  • answer[i] == "Buzz" if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.

Example 1:

Input: n = 3
Output: ["1","2","Fizz"]

Example 2:

Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]

Example 3:

Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
package com.string;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author you guess
 * @Date 2022/4/9 11:59
 * @Version 1.0
 * @Desc answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
 * answer[i] == "Fizz" if i is divisible by 3.
 * answer[i] == "Buzz" if i is divisible by 5.
 * answer[i] == i (as a string) if none of the above conditions are true.
 */
public class Leetcode_412_FizzBuzz 

    /**
     * answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
     * answer[i] == "Fizz" if i is divisible by 3.
     * answer[i] == "Buzz" if i is divisible by 5.
     * answer[i] == i (as a string) if none of the above conditions are true.
     * <p>
     * Runtime: 2 ms, faster than 64.54% of Java online submissions for Fizz Buzz.
     * Memory Usage: 48.4 MB, less than 52.81% of Java online submissions for Fizz Buzz.
     *
     * @param n
     * @return
     */
    public List<String> fizzBuzz2(int n) 
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) 
            if (i % 3 == 0 && i % 5 != 0) 
                list.add("Fizz");
             else if (i % 3 != 0 && i % 5 == 0) 
                list.add("Buzz");
             else if (i % 3 == 0 && i % 5 == 0) 
                list.add("FizzBuzz");
             else 
                list.add(i + "");
            
        
        return list;
    


    /**
     * Runtime: 3 ms, faster than 28.17% of Java online submissions for Fizz Buzz.
     * Memory Usage: 48.4 MB, less than 52.81% of Java online submissions for Fizz Buzz.
     *
     * @param n
     * @return
     */
    public List<String> fizzBuzz3(int n) 
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) 
            if (i % 3 == 0 && i % 5 != 0) 
                list.add("Fizz");
             else if (i % 3 != 0 && i % 5 == 0) 
                list.add("Buzz");
             else if (i % 3 == 0 && i % 5 == 0) 
                list.add("FizzBuzz");
             else 
                list.add(Integer.toString(i));
            
        
        return list;
    


    /**
     * Runtime: 2 ms, faster than 64.54% of Java online submissions for Fizz Buzz.
     * Memory Usage: 48.2 MB, less than 66.20% of Java online submissions for Fizz Buzz.
     *
     * @param n
     * @return
     */
    public List<String> fizzBuzz(int n) 
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) 
            if (i % 3 == 0 && i % 5 != 0) 
                list.add("Fizz");
             else if (i % 3 != 0 && i % 5 == 0) 
                list.add("Buzz");
             else if (i % 3 == 0 && i % 5 == 0) 
                list.add("FizzBuzz");
             else 
                list.add(String.valueOf(i));
            
        
        return list;
    

    public static void main(String[] args) 
        Leetcode_412_FizzBuzz main = new Leetcode_412_FizzBuzz();
        //System.out.println(main.fizzBuzz(5));//[1, 2, Fizz, 4, Buzz]
        System.out.println(main.fizzBuzz(15));
    

以上是关于Leetcode 412. Fizz Buzz-蜂鸣器的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 412. Fizz Buzz

LeetCode 412 Fizz Buzz

LeetCode 412. Fizz Buzz

LeetCode|412. Fizz Buzz

LeetCode 412. Fizz Buzz

LeetCode 412. Fizz Buzz