Recca Chao 的 gitHub page

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

View on GitHub

Kotlin Leetcode - 2798. Number of Employees Who Met the Target

題目連接:https://leetcode.com/problems/number-of-employees-who-met-the-target/

class Solution {
    fun numberOfEmployeesWhoMetTarget(hours: IntArray, target: Int) {

    }
}

解題思路

這題考的是陣列內元素的處理

我們要找到陣列內元素大於 target 的元素個數

利用 Kotlin Collection 的 count

我們可以很簡單的算出問題的解答

Kotlin 參考解答

利用 Kotlin Collection 的 count

我們可以這樣寫

class Solution {
    fun numberOfEmployeesWhoMetTarget(hours: IntArray, target: Int) = hours.count { it >= target }
}