/*
 * 演算法 / Algorithm:
 * 單次掃描，start 記住當前連續區段的起點。 / Single pass; `start` marks the run's start.
 * 當下一個數打破 +1 連續（或到末端）時，區段結束，組成一個範圍字串。
 * When the +1 chain breaks (or we reach the end), close the run and build its string.
 */
class Solution {
public:
    // 回傳 vector<string>：可自動增長的字串動態陣列。
    // Returns vector<string>: a self-growing dynamic array of strings.
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;            // 存放答案 / holds the answer
        int n = nums.size();              // 陣列長度 / array length
        if (n == 0) return result;        // 空陣列直接回傳空清單 / empty input → empty list

        int start = 0;                    // 當前區段起點索引 / start index of current run

        for (int i = 0; i < n; i++) {
            // 區段結束條件：i 是最後一個，或下一個數不等於目前數 +1。
            // Run ends if i is last, or next value is not (current + 1).
            // 用 long long 避免 nums[i]+1 在 INT_MAX 時溢位。
            // Use long long so nums[i]+1 won't overflow at INT_MAX.
            if (i == n - 1 || (long long)nums[i] + 1 != nums[i + 1]) {
                if (nums[start] == nums[i]) {
                    // 單一數字 → 直接用 to_string 把整數轉成字串並加入結果。
                    // Single number → to_string converts the int to text; push it.
                    result.push_back(to_string(nums[start]));
                } else {
                    // 範圍 "a->b" → 用 + 串接字串。to_string 處理數字轉文字。
                    // Range "a->b" → concatenate with +. to_string handles int-to-text.
                    result.push_back(to_string(nums[start]) + "->" + to_string(nums[i]));
                }
                start = i + 1;            // 下一段從 i+1 開始 / next run starts at i+1
            }
            // 否則仍在同一段內，繼續。 / Otherwise still in the same run; continue.
        }

        return result;                    // 回傳所有範圍 / return all ranges
    }
};
