Fizz Buzz 报数
Fizz Buzz
题目详情
问题:Fizz Buzz 报数
考察:数组、字符串
来源:Citadel
链接:https://www.jointaro.com/interviews/questions/fizz-buzz/
Given an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz"ifiis divisible by3and5.answer[i] == "Fizz"ifiis divisible by3.answer[i] == "Buzz"ifiis divisible by5.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
解析
思路:从 1 到 n 枚举数字,先判断是否同时被 3 和 5 整除,再分别判断 3 或 5,最后输出数字本身。
复杂度:时间 O(n),输出空间 O(n)。