LeetCode之Count Number of Teams(Kotlin)

问题:



方法:
三重循环,穷举所有可能出现的队列,然后判断是否符合排序要求,最终即可得到结果。

class CountNumberOfTeams {
    fun numTeams(rating: IntArray): Int {
        var result = 0
        for (i in 0 until rating.lastIndex - 1) {
            val first = rating[i]
            for (j in i + 1 until rating.lastIndex) {
                val second = rating[j]
                for (k in j + 1..rating.lastIndex) {
                    val third = rating[k]
                    if (second in (first + 1) until third || second in (third + 1) until first) {
                        result++
                    }
                }
            }
        }
        return result
    }
}

fun main() {
    val rating = intArrayOf(2, 5, 3, 4, 1)
    val countNumberOfTeams = CountNumberOfTeams()
    print(countNumberOfTeams.numTeams(rating))
}

有问题随时沟通

具体代码实现可以参考Github

你可能感兴趣的:(LeetCode之Count Number of Teams(Kotlin))