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

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/summary-ranges/description/

题目描述:

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

Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"] Example 2: Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]

题目大意

把一个有序的数组,合并区间。也就是两个相邻的数字之间的距离如果是1,那么就应该合并。

解题方法

完全是自己想出来的算法哈哈哈。两个while嵌套,第一个嵌套遍历Nums,第二个嵌套要往后走,看看后面的数字是不是比前面的数字大1,是的话就一直后移。根据i和j是否重叠来判断是加上一个数字还是加上一个区间。

代码:

class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
if not nums: return []
res = []
i = 0
while i < len(nums):
j = i
while j < len(nums) - 1 and nums[j] == nums[j + 1] - 1:
j += 1
if j == i:
res.append(str(nums[i]))
else:
res.append('%s->%s' % (str(nums[i]), str(nums[j])))
i = j + 1
return res

日期

2018 年 3 月 31 日 ———— 晚上睡不好,一天没精神啊

【LeetCode】228. Summary Ranges 解题报告(Python)的更多相关文章

  1. [LeetCode] 228. Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  2. Java for LeetCode 228 Summary Ranges

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

  3. Java [Leetcode 228]Summary Ranges

    题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...

  4. C#解leetcode 228. Summary Ranges Easy

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

  5. LeetCode 228. Summary Ranges (总结区间)

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  6. LeetCode(228) Summary Ranges

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

  7. (easy)LeetCode 228.Summary Ranges

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

  8. [leetcode]228. Summary Ranges区间统计

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. php header下载文件 无法查看原因

    php header下载文件 无法查看原因 php header下载文件 下方函数可以下载单个文件 function download($file_url){ if(!isset($file_url) ...

  2. mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)

    一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...

  3. 出现NoClassDefFoundError,始终无法引入jar的解决

    在拉取代码后,项目的部分版本与本地存在的不一定一致,所以IDEA会自动下载并引入,但是在启动时可能存在java.lang.NoClassDefFoundError这个报错 比如引入marshallin ...

  4. linux系统中上传文件与下载文件的方式

    方式一:FileZilla 使用FileZilla第三方工具 绿色版直接打开exe文件即可 主机:连接的linux服务器的IP地址 用户名:登录的用户名 密码:登录密码 端口:默认使用22 左边是自己 ...

  5. python-3.x- 序列操作

    1. list操作 A.添加元素 1 list = ["C++","C", "Java", "Python"] 2 &q ...

  6. 【AWS】【Basis】基础概念

    1.基础服务类型: 1.1. 链接: 官方文档,很详细:https://www.amazonaws.cn/products/#compute_networking/?nc1=f_dr 这个是一个whi ...

  7. 【Java多线程】ExecutorService和ThreadPoolExecutor

    ExecutorService Java.util.concurrent.ExecutorService接口代表一种异步执行机制,它能够在后台执行任务.因此ExecutorService与thread ...

  8. java中的迭代器的含义

    可迭代是Java集合框架下的所有集合类的一种共性,也就是把集合中的所有元素遍历一遍.迭代的过程需要依赖一个迭代器对象,那么什么是迭代器呢? 迭代器(Iterator)模式,又叫做游标模式,它的含义是, ...

  9. 云服务器ECS部署和卸载RabbitMQ

    云服务器ECS部署RabbitMQ RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件,用于在分布式系统中存储转发消息,有良好的易用性.扩展性和高可用性.本文介绍如何通过ECS实例 ...

  10. linux 编程随笔

    Linux 命令: 在linux 系统中,所有的命令都是人为编写的程序,如 who 和 ls ,而且绝大多数都是C写的.在Linux 中增加新的命令是很简单的事,把程序的可执行文件放到以下目录就可以了 ...