【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. 日常Java 2021/10/6

    声明自定义异常 class zidingyiException extends Exception{}//定义自己的异常类 单继承 public class A  {} public class B ...

  2. A Child's History of England.46

    As, one hundred years before, the servile [卑躬屈膝的~serve] followers of the Court had abandoned the Con ...

  3. A Child's History of England.42

    The names of these knights were Reginald Fitzurse, William Tracy, Hugh de Morville, and Richard Brit ...

  4. ceph对象存储场景

    安装ceph-radosgw [root@ceph-node1 ~]# cd /etc/ceph # 这里要注意ceph的源,要和之前安装的ceph集群同一个版本 [root@ceph-node1 c ...

  5. gitlab之数据备份恢复

    备份#备份的时候,先通知相关人员服务要听 ,停止两个服务,并影响访问 root@ubuntu:/opt/web1# gitlab-ctl stop unicorn ok: down: unicorn: ...

  6. Dubbo服务限流

    为了防止某个消费者的QPS或是所有消费者的QPS总和突然飙升而导致的重要服务的失效,系统可以对访问流量进行控制,这种对集群的保护措施称为服务限流. Dubbo中能够实现服务限流的方式较多,可以划分为两 ...

  7. my38_MySQL事务知识点零记

    从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...

  8. Oracle SQL中join方式总结

    在ORACLE数据库中,表与表之间的SQL JOIN方式有多种(不仅表与表,还可以表与视图.物化视图等联结).SQL JOIN其实是一个逻辑概念,像NEST LOOP JOIN. HASH JOIN等 ...

  9. 最基础前端路由实现,事件popstate使用

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. java输入/输出流的基本知识

    通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...