Recca Chao 的 gitHub page

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

View on GitHub

Kotlin Leetcode - 3280. Convert Date to Binary

題目連接:https://leetcode.com/problems/convert-date-to-binary/

class Solution {
    fun convertDateToBinary(date: String): String {
        
    }
}

解題思路

這一題考的是字串處理的邏輯

我們可以用 split() 將日期拆解出數字

然後用 toInt().toString(2) 轉換成二進位表示

最後用 joinToString() 合併為一個字串

Kotlin 參考解答

點擊展開解答
class Solution {
    fun convertDateToBinary(date: String) =
        date.split('-')
            .joinToString("-") {
                it.toInt().toString(2)
            }
}