g0401_0500.s0412_fizz_buzz.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0401_0500.s0412_fizz_buzz
// #Easy #Top_Interview_Questions #String #Math #Simulation #Udemy_Integers
// #2022_12_03_Time_307_ms_(71.81%)_Space_41.9_MB_(71.97%)
class Solution {
fun fizzBuzz(n: Int): Array = Array(n) { index ->
val value = index + 1
when {
(value % 3 == 0 && value % 5 == 0) -> "FizzBuzz"
(value % 3 == 0) -> "Fizz"
(value % 5 == 0) -> "Buzz"
else -> "$value"
}
}
}