描述

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

**Credits:**
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

go语言

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func summaryRanges(nums []int) []string {
    if len(nums)==0{
        return []string{}
    }
    s := 0
    e := 0
    result := []string{}
    for i:=1;i<len(nums);i++{
        if nums[i]==nums[i-1]+1{
            e = i
        }else{
            if s ==e{
                result = append(result,fmt.Sprintf("%v",nums[s]))
            }else{
                result = append(result,fmt.Sprintf("%v->%v",nums[s],nums[e]))
            }
            s = i
            e = i
        }
    }
    if s ==e{
        result = append(result,fmt.Sprintf("%v",nums[s]))
    }else{
        result = append(result,fmt.Sprintf("%v->%v",nums[s],nums[e]))
    }

    return result
}