Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Kotlin Kata - Fizz Buzz

Given an integer n, return a string array answer (1-indexed) where:

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"]

class Solution {
    fun fizzBuzz(n: Int): List<String> {
        
    }
}

解答

點擊展開解答

這題題目的邏輯很單純

class Solution {
    fun fizzBuzz(n: Int): List<String> {
        val list = mutableListOf<String>()
        for (i in 1..n) {
            if(i % 15 == 0) {
                list.add("FizzBuzz")
                continue
            }
            if(i % 5 == 0) {
                list.add("Buzz")
                continue
            }
            if(i % 3 == 0) {
                list.add("Fizz")
                continue
            }
            list.add(i.toString())
        }
        return list
    }
}

利用 Kotlin 的 mapwhen 可以寫得更加簡潔

class Solution {
    fun fizzBuzz(n: Int) = (1..n).map {
        when {
            it % 15 == 0 -> "FizzBuzz"
            it % 5 == 0 -> "Buzz"
            it % 3 == 0 -> "Fizz"
            else -> it.toString()
        }
    }
}

回到 Kotlin Kata 列表