0412. Fizz Buzz

412. Fizz Buzz #

题目 #

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

题目大意 #

3的倍数输出 “Fizz”,5的倍数输出 “Buzz”,15的倍数输出 “FizzBuzz”,其他时候都输出原本的数字。

解题思路 #

按照题意做即可。

代码 #


package leetcode

import "strconv"

func fizzBuzz(n int) []string {
	solution := make([]string, n)
	for i := 1; i <= n; i++ {
		solution[i-1] = ""
		if i%3 == 0 {
			solution[i-1] += "Fizz"
		}
		if i%5 == 0 {
			solution[i-1] += "Buzz"
		}
		if solution[i-1] == "" {
			solution[i-1] = strconv.Itoa(i)
		}
	}
	return solution
}


⬅️上一页

下一页➡️

Calendar Apr 8, 2023
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者