结构数组新发现之直接初始化《leetcode-合并区间》
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-合并区间》的更多相关文章
- leetcode合并区间
合并区间 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...
- leetcode 合并区间
使用最简单的排序方法: /** * Definition for an interval. * public class Interval { * int start; * int end; * In ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项
Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- LeetCode(56):合并区间
Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...
- [C语言]贪吃蛇_结构数组实现
一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数 ...
- MATLAB中的结构数组
MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...
- C 语言字符数组的定义与初始化
1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y'} ...
随机推荐
- docker swarm(当前官网示例)
介绍 Docker Swarm 是 Docker 公司推出的官方容器集群平台,基于 Go 语言实现 作为容器集群管理器,Swarm 最大的优势之一就是 100% 支持标准的 Docker API.各种 ...
- saltstack syndic
#syndic 相当于master的代理,master通过syndic代理控制node主机 master <------ syndic+master <---------- node ma ...
- Hadoop生产环境配置文件
前提: ①已经搭建好zk ②已经安装好JDK 正文开始: 首先从官网下载hadoop 2.7.3 (虽然官网3.0都出了.但是目前还没经过完全的测试..待测试后...) 一.hadoop-env.sh ...
- python Bootstarp框架和inconfont、font-awesome使用
http://www.bootcss.com/ http://www.runoob.com/bootstrap/bootstrap-panels.html 查找基本的没问题 https://www. ...
- Eclipse xml中自动提示,添加 dtd或xsd依赖
下载DTD或XSD文件 添加到Eclipse
- hdu 6380
#include<bits/stdc++.h> #define in(a) scanf("%d",&a) using namespace std; struct ...
- javaScript ES5常考面试题总结
js的六种原始值 boolean null undefined number string symbol 坑1: 首先原始类型存储的都是值,是没有函数可以调用的,比如 undefined.toStri ...
- 用Google Brain的机器学习项目:Magenta,教神经网络学抖音小姐姐作曲。
先上我们要学习的小姐姐 的美照.. 一.配置环境 1.自己配置环境:python,tensorflow,bazel(编译),java.然后下载magenta(https://github.com/te ...
- Java - 网络编程完全总结
本文主要是自己在网络编程方面的学习总结,先主要介绍计算机网络方面的相关内容,包括计算机网络基础,OSI参考模型,TCP/IP协议簇,常见的网络协议等等,在此基础上,介绍Java中的网络编程. 一.概述 ...
- javascript文件加载模式与加载方法
加载方式 形象图像化方法,见 http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html 1. script标签, ...