228._summary_ranges.md 1.6 KB
Newer Older
K
KEQI HUANG 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
### 228. Summary Ranges

题目:
<https://leetcode.com/problems/summary-ranges/>


难度:

Medium


Just collect the ranges, then format and return them.

```python
K
KEQI HUANG 已提交
15 16 17 18 19 20
class Solution(object):
    def summaryRanges(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
K
KEQI HUANG 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        ranges = []
        for i in nums:
            if not ranges or i > ranges[-1][-1] + 1:
                ranges += [],
            ranges[-1][1:] = i,
        return ['->'.join(map(str, r)) for r in ranges]                           
```
About the commas :-)

```
ranges += [],
r[1:] = n,
```
Why the trailing commas? Because it turns the right hand side into a tuple and I get the same effects as these more common alternatives:
```
ranges += [[]]
or
ranges.append([])

r[1:] = [n]
```
Without the comma, …

- ranges += [] wouldn’t add [] itself but only its elements, i.e., nothing.
- r[1:] = n wouldn’t work, because my n is not an iterable.

Why do it this way instead of the more common alternatives I showed above? Because it’s shorter and faster (according to tests I did a while back).

K
KEQI HUANG 已提交
49 50 51 52 53 54 55 56 57
写到这里可能又有疑问了🤔️,为什么不可以直接写```ranges[-1][1] = i```呢,当然是会报```IndexError: list assignment index out of range```错误啦,那为什么```ranges[-1][1:] = i,```可以呢?

简单来说

L1=L 与 L1=L[:] 
- L1和L  都是对同一个对象的引用(所谓绑定的意思)。
- L[:]  是生成了一个和L不同的新的对象,L1 变为了L[:] 这个对象的引用。


K
KEQI HUANG 已提交
58
参考[stefan](https://leetcode.com/problems/summary-ranges/discuss/63193)