Kotlin Kata - Fizz Buzz
Given an integer n
, return a string array answer
(1-indexed) where:
answer[i] == "FizzBuzz"
ifi
is divisible by3
and5
.answer[i] == "Fizz"
ifi
is divisible by3
.answer[i] == "Buzz"
ifi
is divisible by5
.answer[i] == i
if non of the above conditions are true.
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 的 map
和 when
可以寫得更加簡潔
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()
}
}
}