412. Fizz Buzz

作者: 花果山松鼠 | 来源:发表于2018-09-21 15:36 被阅读2次

一、题目原型:

写一个程序,输出从 1 到 n 数字的字符串表示。

  1. 如果 n 是3的倍数,输出“Fizz”;
  2. 如果 n 是5的倍数,输出“Buzz”;
  3. 如果 n 同时是3和5的倍数,输出 “FizzBuzz”。

二、示例剖析:

n = 15,
返回:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz",
"11","Fizz","13","14","FizzBuzz"]

三、解题思路:

这题非常简单,就是从0遍历到n-1,把对应的数字换成字符串。

// 公倍数
func fizzBuzz(_ n: Int) -> [String] {
    
    var strings: [String] = []
    for i in 0..<n {
        
        if (i + 1) % 15 == 0 {
            strings.append("FizzBuzz")
        }else {
            if (i + 1) % 3 == 0 {
                strings.append("Fizz")
            }
            else if (i + 1) % 5 == 0 {
                strings.append("Buzz")
            }
            else {
                strings.append("\(i+1)")
            }
        }
    }
    return strings
}

四、小结

耗时20毫秒,超过100%的提交记录,总提交数8

相关文章

  • LeetCode 411-430

    412. Fizz Buzz[https://leetcode-cn.com/problems/fizz-buzz...

  • Leetcode PHP题解--D40 412. Fizz Bu

    412. Fizz Buzz 题目链接 412. Fizz Buzz 题目分析 这个题目也很简单。 从1逐个输出到...

  • Leetcode 412 Fizz Buzz

    题目链接:412. Fizz Buzz 题目描述 Write a program that outputs the...

  • 412. Fizz Buzz

    一、题目原型: 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”;如果 ...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    题目分析 题目链接,登录 LeetCode 后可用思路比较简单,直接遍历1 到 n 的每个数,依次判断每种情况即可...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

  • 412. Fizz Buzz

网友评论

    本文标题:412. Fizz Buzz

    本文链接:https://www.haomeiwen.com/subject/yhzanftx.html