228. Summary Ranges

Posted on Dec 2, 2024

Solution

Implementation

class Solution {
    fun getRangeString(start: Int, end: Int) = if (start == end) "$start" else "$start->$end"

    fun summaryRanges(nums: IntArray): List<String> {
        if (nums.isEmpty()) return listOf()

        val result = mutableListOf<String>()
        var start = nums.first()

        for (i in 1 until nums.size) {
            if (nums[i] > nums[i-1] + 1) {
                val end = nums[i-1]
                result.add(getRangeString(start, end))
                start = nums[i]
            }
        }

        result.add(getRangeString(start, nums.last()))

        return result
    }
}