[Swift]LeetCode412. Fizz Buzz

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-aidmcxhm-dp.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write a program that outputs the string representation of numbers from 1 to n.git

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」.github

Example:微信

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

寫一個程序,輸出從 1 到 n 數字的字符串表示。app

1. 若是 是3的倍數,輸出「Fizz」;spa

2. 若是 是5的倍數,輸出「Buzz」;code

3.若是 同時是3和5的倍數,輸出 「FizzBuzz」。htm

示例:blog

n = 15,

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

 1 class Solution {
 2     func fizzBuzz(_ n: Int) -> [String] {
 3         var res:[String] = [String]()
 4         for i in 1...n
 5         {
 6             if i % 3 == 0 && i % 5 == 0
 7             {
 8                 res.append("FizzBuzz")
 9             }
10             else if  i % 5 == 0
11             {
12                 res.append("Buzz")
13             }
14             else if  i % 3 == 0
15             {
16                 res.append("Fizz")
17             }
18             else
19             {
20                 res.append("\(i)")
21             }            
22         }
23         return res
24     }
25 }

16ms
 1 class Solution {
 2     func fizzBuzz(_ n: Int) -> [String] {
 3         var res = [String]()
 4         for i in 1...n {
 5             if i % 3 != 0, i % 5 != 0 { res.append(String(i)) }
 6             else if i % 5 != 0 { res.append("Fizz") }
 7             else if i % 3 != 0 { res.append("Buzz") }
 8             else { res.append("FizzBuzz") }
 9         }
10         return res
11     }
12 }

20msthree

 1 class Solution {
 2     func fizzBuzz(_ n: Int) -> [String] {
 3         
 4         var i = 1
 5         
 6         var fizzbuzz = [String]()
 7         
 8         while i <= n {
 9             if i % 3 == 0 && i % 5 == 0 {
10                 fizzbuzz.append("FizzBuzz")
11             } else if i % 3 == 0 {
12                 fizzbuzz.append("Fizz")
13             } else if i % 5 == 0 {
14                 fizzbuzz.append("Buzz")
15             } else {
16                 fizzbuzz.append(String(i))
17             }
18             i += 1
19         }
20         
21         return fizzbuzz
22         
23     }
24 }

20ms

 1 class Solution {
 2     func fizzBuzz(_ n: Int) -> [String] {
 3         var res = [String]()
 4         
 5         for i in 1...n {
 6             if i % 3 == 0 && i % 5 == 0 {
 7                 res.append("FizzBuzz")
 8             } else if i % 3 == 0 {
 9                 res.append("Fizz")
10             } else if i % 5 == 0 {
11                 res.append("Buzz")
12             } else {
13                res.append(String(i))
14             }
15         }
16         return res
17     }
18 }
相關文章
相關標籤/搜索