Leetcode 412. Fizz Buzz-蜂鸣器

143 阅读1分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第2天,点击查看活动详情

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"]

Constraints:

  • 1 <= n <= 104

思路:

i从1开始计数:

当i只能被3整除:追加**"Fizz"**

当i只能被5整除:追加"Buzz"

当i既能被5整除也能被3整除:追加"FizzBuzz"

当i不能被5整除也不能被3整除:追加i

如下是代码:

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));
    }
}

end