leetcode有时候会要求一些奇怪(陌生)的数据形式,刷题因为数据形式卡住了真的很不好...

合并区间里定义了一个Interval的结构数组

struct Interval {
    int start;
    int end;
    Interval() : start(), end() {}
    Interval(int s, int e) : start(s), end(e) {}
};

又根据结构数组创建了vector<Interval> intervals。我就在想,怎么初始化这个intervals。

vector<Interval> intervals;
Interval int1(,); intervals.push_back(int1);
Interval int2(,); intervals.push_back(int2);

其实就是利用结构数组里的定义来,这样初始化真的很方便呐,也不用去特意申请空间。

解题思路:

先根据start排序,再一一合并

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;

typedef struct Interval Int;
struct Interval {
    int start;
    int end;
    Interval() : start(), end() {}
    Interval(int s, int e) : start(s), end(e) {}
};
class Solution {
public:
    vector<Interval> merge(vector<Interval>& intervals) {
        int n = intervals.size();
        ) return intervals;
        int j;
        Interval temp;
        Interval temp0;
        ;
        ;i<n;i++){
            temp = intervals[i];
             && temp.start<intervals[j-].start;j--){
                intervals[j].start = intervals[j-].start;
                intervals[j].end = intervals[j-].end;
            }
            intervals[j].start = temp.start;
            intervals[j].end = temp.end;
        }
        vector<Interval> outcome;
        outcome.push_back(intervals[]);
        ;i<n;i++){
            if(intervals[i].start <= outcome.back().end){
                outcome.back().end = outcome.back().end > intervals[i].end ? outcome.back().end:intervals[i].end;
            }
            else{
                outcome.push_back(intervals[i]);
            }
        }
        return outcome;
    }
};

int main(){
    vector<Interval> intervals;
    Interval int1(,); intervals.push_back(int1);
    Interval int2(,); intervals.push_back(int2);
//    Interval int3(8,10); intervals.push_back(int3);
//    Interval int4(15,18); intervals.push_back(int4);

    Solution S;
    vector<Interval> outCome = S.merge(intervals);
    ;i<outCome.size();i++){
        int1 = outCome[i];
        printf("%d %d\n", int1.start, int1.end);
    }
    ;

}

结构数组新发现之直接初始化《leetcode-合并区间》的更多相关文章

  1. leetcode合并区间

    合并区间     给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...

  2. leetcode 合并区间

    使用最简单的排序方法: /** * Definition for an interval. * public class Interval { * int start; * int end; * In ...

  3. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  4. [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项

    Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...

  5. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  6. LeetCode(56):合并区间

    Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...

  7. [C语言]贪吃蛇_结构数组实现

    一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数 ...

  8. MATLAB中的结构数组

    MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...

  9. C 语言字符数组的定义与初始化

    1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y'} ...

随机推荐

  1. Linux_查找文件

    1.查找文件 (1)通过文件名查找 find / -name jdbc.properties (2)根据部分文件名查找 find /etc -name *srm* find /etc -name sr ...

  2. docker-compose 案例

    官网示例: 安装wordpress version: " services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql r ...

  3. bash 刷题leetcode

    题目一: 给定一个文本文件 file.txt,请只打印这个文件中的第十行. 示例: 假设 file.txt 有如下内容: Line 1 Line 2 Line 3 Line 4 Line 5 Line ...

  4. 解决IOS移动端固定定位失效问题

    根据浏览器窗口position:fixed; 定位在底部的元素,会随着屏幕的滚动而滚动,在iOS系统上不起作用. <div class="header">头部</ ...

  5. HTTP method constants

    HTTP method constants ngx.HTTP_GET ngx.HTTP_HEAD ngx.HTTP_PUT ngx.HTTP_POST ngx.HTTP_DELETE ngx.HTTP ...

  6. web-hacking

    https://wizardforcel.gitbooks.io/web-hacking-101/content/1.html

  7. IDAPython教程(二)

    继续我们的主题—使用IDAPython 让逆向工程师的生活变得更美好. 这一部分,我们将着手处理一个非常常见的问题:shellcode和恶意软件使用hash算法混淆加载的函数和链接库,这项技术被广泛使 ...

  8. oracle 窗口函数 (keep)

    看到很多人对于keep不理解,这里解释一下! Returns the row ranked first using DENSE_RANK2种取值:DENSE_RANK FIRSTDENSE_RANK  ...

  9. Debian Security Advisory(Debian安全报告) DSA-4412-1 drupal7 security update

    Debian Security Advisory(Debian安全报告) DSA-4412-1 drupal7 security update Package:drupal7 CVE ID:暂无 Dr ...

  10. C++向量 vector动态数组

    需要包含头文件, #include  <vector>    using namespace std; vector 容器与数组相比其优点在于它能够根据需要随时自动调整自身的大小以便容下所 ...