美文网首页算法每日一刷
412. Fizz Buzz(Swift)

412. Fizz Buzz(Swift)

作者: entre_los_dos | 来源:发表于2019-07-07 15:57 被阅读0次

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fizz-buzz

题目

写一个程序,输出从 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"
]

3的倍数==Fizz;5的倍数==Buzz;同是3和5的倍数==FizzBuzz

方法-从1到n遍历判断

func fizzBuzz(_ n: Int) -> [String] {
     
        var strArr = [String]()
        
        if n > 0 {
            for i in 1...n {
                if ((i % 3 == 0) && (i % 5 == 0)) {
                    strArr.append("FizzBuzz")
                }else if (i % 3 == 0) {
                    strArr.append("Fizz")
                }else if (i % 5 == 0) {
                    strArr.append("Buzz")
                }else {
                    strArr.append(String(i))
                }
            }
        }
        
        return strArr
    }

相关文章

  • 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(Swift)

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/fizz-b...

  • 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(Swift)

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