Skip to content

Latest commit

 

History

History
186 lines (158 loc) · 3.59 KB

File metadata and controls

186 lines (158 loc) · 3.59 KB
title subtitle date lastmod draft author authorLink description license images tags categories featuredImage featuredImagePreview hiddenFromHomePage hiddenFromSearch twemoji lightgallery ruby fraction fontawesome linkToMarkdown rssFullText toc code math mapbox share comment library seo
412. Fizz Buzz
2024-02-14 19:37:00 +0800
2024-02-14 19:37:00 +0800
false
Kimi.Tsai
0412.Fizz-Buzz
LeetCode
Go
Easy
Fizz Buzz
Facebook
Microsoft
Apple
string
math
LeetCode
false
false
false
true
true
true
true
false
false
enable auto
true
true
copy maxShownLines
true
200
enable
enable
true
enable
true
css js
images

題目

Facebook, Microsoft, Apple 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

題目大意

解題思路

Big O

時間複雜 : O(n) 空間複雜 : O(n)

來源

解答

https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0412.Fizz-Buzz/main.go

package fizzbuzz

import "strconv"

// 時間複雜 O(), 空間複雜 O()
func fizzBuzz(n int) []string {
	var result []string
	for i := 1; i <= n; i++ {
		if i%15 == 0 {
			result = append(result, "FizzBuzz")
		} else if i%3 == 0 {
			result = append(result, "Fizz")
		} else if i%5 == 0 {
			result = append(result, "Buzz")
		} else {
			result = append(result, strconv.Itoa(i))
		}
	}
	return result
}

func fizzBuzz2(n int) []string {
	var result []string
	for i := 1; i <= n; i++ {
		var str string
		if i%3 == 0 {
			str += "Fizz"
		}
		if i%5 == 0 {
			str += "Buzz"
		}
		if len(str) <= 0 {
			str = strconv.Itoa(i)
		}
		result = append(result, str)
	}
	return result
}

// 最佳
func fizzBuzz3(n int) []string {
	var result []string
	fizz := 0
	buzz := 0
	for i := 1; i <= n; i++ {
		fizz++
		buzz++
		if fizz == 3 && buzz == 5 {
			result = append(result, "FizzBuzz")
			fizz = 0
			buzz = 0
		} else if fizz == 3 {
			result = append(result, "Fizz")
			fizz = 0
		} else if buzz == 5 {
			result = append(result, "Buzz")
			buzz = 0
		} else {
			result = append(result, strconv.Itoa(i))
		}
	}
	return result
}

Benchmark

goos: darwin
goarch: amd64
pkg: LeetcodeGolang/Leetcode/0412.Fizz-Buzz
cpu: Intel(R) Core(TM) i5-6400 CPU @ 2.70GHz
BenchmarkFizzBuzz-4      5918809               287.1 ns/op           112 B/op          3 allocs/op
BenchmarkFizzBuzz2-4     5024536               223.8 ns/op           112 B/op          3 allocs/op
BenchmarkFizzBuzz3-4     5406643               196.3 ns/op           112 B/op          3 allocs/op
PASS
ok      LeetcodeGolang/Leetcode/0412.Fizz-Buzz  5.507s