LeetCode OJ 系列之228 Summary Ranges --Python

原创 2015年11月16日 09:11:30

Problem:

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"].

Answer:

class Solution(object):
    def summaryRanges(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
        result = []
        if len(nums) == 0:
            return result
        for index in range(len(nums)):
        	if index == 0:
        		start = nums[index]
        		continue
        	if nums[index]!=nums[index-1]+1:
        		if nums[index-1]==start:
        			result.append(str(start))
        		else:
        			result.append(str(start)+'->'+str(nums[index-1]))
        		start = nums[index]		
        
        if nums[-1] == start:
        	result.append(str(start))
        else:
        	result.append(str(start)+'->'+str(nums[-1]))
        return result
        


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ln152315/article/details/49861493

【Python】第二周Python基础(下)

本周课程主要讲解Python的面向对象编程, 异常处理、模块、MySQL数据库、Python的数据库支持以及图形用户界面等技术,最后我们使用阶段案例开发了一个《飞机大战》的游戏。
  • 2018年03月16日 11:34

leetcode 228: Summary Ranges

leetcode 228: Summary Ranges python, java, c++
  • xudli
  • xudli
  • 2015-06-26 06:22:18
  • 10259

【LEETCODE】228-Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2015-12-16 16:24:15
  • 277

Summary Ranges(数组中连续的数字段)

Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. ...
  • u010032372
  • u010032372
  • 2015-06-26 11:53:18
  • 624

LeetCode - 228. Summary Ranges - 思路详解- C++

假设有一个已排序整数数组,不包含重复的元素,返回其范围摘要
  • SJWL2012
  • SJWL2012
  • 2017-01-13 20:50:21
  • 190

【LeetCode】228. Summary Ranges 解题报告(Python)

【LeetCode】228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-03-31 23:06:21
  • 10

LeetCode OJ 系列之228 Summary Ranges --Python

Problem: Given a sorted integer array without duplicates, return the summary of its ranges. For ex...
  • ln152315
  • ln152315
  • 2015-11-16 09:11:30
  • 777

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.For example, given...
  • kakitgogogo
  • kakitgogogo
  • 2016-04-30 17:20:57
  • 106

228. Summary Ranges

边界条件边界条件边界条件边界条件class Solution { public: string int_to_str(int n) { if(n==-214748364...
  • u013480495
  • u013480495
  • 2016-03-09 20:17:09
  • 76

leetcode 228:Summary Ranges

leetcode 228:Summary Ranges 题目: Given a sorted integer array without duplicates, return the summ...
  • tuobadon
  • tuobadon
  • 2015-07-04 11:29:19
  • 160
收藏助手
不良信息举报
您举报文章:LeetCode OJ 系列之228 Summary Ranges --Python
举报原因:
原因补充:

(最多只允许输入30个字)