zoukankan      html  css  js  c++  java
  • [Swift]LeetCode412. Fizz Buzz

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9782998.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

    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”.

    Example:

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

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

    1. 如果 是3的倍数,输出“Fizz”;

    2. 如果 是5的倍数,输出“Buzz”;

    3.如果 同时是3和5的倍数,输出 “FizzBuzz”。

    示例:

    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 }

    20ms

     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 }
  • 相关阅读:
    JS中 try...catch...finally (转)
    Npm基本指令(转)
    常用Dos(转)
    理解Node.js异步非阻塞I/O与传统线性阻塞IO的区别(转)
    Java 连接 Access数据库方式
    感知器-从零开始学深度学习
    Oracle的SQL语句中如何处理‘&’符号
    (转)远程桌面超出最大连接数怎么办
    url中含有%
    (转)silverlight应用程序中未处理的错误代码:2104 类别:InitializeError
  • 原文地址:https://www.cnblogs.com/strengthen/p/9782998.html
Copyright © 2011-2022 走看看