leetcode-mid-sorting and searching - 56 Merge Intervals
mycode
出现的问题:比如最后一个元素是【1,10】,1小于前面所有元素的最小值,10大于前面所有元素的最大值,而我最开始的思路只考虑了相邻
参考:
思路:如果我只考虑相邻,必须先将list排序,由于key只有一个,所以还要在循环的时候考虑第二个元素
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
intervals.sort(key = lambda x:x[])
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
size=len(res)
if res[size-][] <= intervals[i][] <=res[size-][-]:
res[size-][-] = max(intervals[i][-], res[size-][-])
else:
res.append(intervals[i])
return res
leetcode-mid-sorting and searching - 56 Merge Intervals的更多相关文章
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【一天一道LeetCode】#56. Merge Intervals
一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
随机推荐
- python之kafka消费
使用python3第三方工具,实现kafka消费 # -*- coding: utf-8 -*- import uuid import json from kafka import KafkaCons ...
- 国产芯片选型手册及厂商名录 版本V2019
- linux各路径(目录)的解释
目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点, 比如用户use ...
- 在centOS7.2上编译gcc4.1.2
1.下载安装gcc4.1.2安装包 wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2 注:其他版本的安装包可以在上级目录寻找到. ...
- Java基本的程序结构设计 字符类型
char类型 char是2个字节,和short一样. char用单引号来表示. char可以通过数字来表示,也可以通过字母来表示,也可以通过unicode编码单元来表示,特殊字符还可以通过\+字符来表 ...
- Spring的DI和AOP
Spring 为了降低Java开发的复杂性,Spring采取了以下4种关键策略:* 基于POJO的轻量级和最小入侵性编程:* 通过依赖注入和面向接口实现松耦合:* 基于切面和惯例进行声明式编程:* 通 ...
- SpringMVC入门示例
1.新建一个Java Web项目 2.导入jar包 3.在WEB-INF下面建一个hello.jsp页面. 1 <%@ page language="java" import ...
- CentOS升级乱七八糟问题解决
----------------------------------------------------------------- Error: Package: libgpod--.el7.x86_ ...
- iOS使用protobuf环境的配置
配置protobuf需要HomeBrew工具或则是MacPort.如没有安装,则需要配置HomeBrew工具或则是MacPort. 步骤1(环境配置前的准备工作): 1:使用HomeBrew brew ...
- 【leetcode】Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...