-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Create Solution 056[CPP] #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
solution/056.Merge Intervals/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 给出一个区间的集合,请合并所有重叠的区间。 | ||
| ``` | ||
| 示例 1: | ||
| 输入: [[1,3],[2,6],[8,10],[15,18]] | ||
| 输出: [[1,6],[8,10],[15,18]] | ||
| 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6]. | ||
|
|
||
| 示例 2: | ||
| 输入: [[1,4],[4,5]] | ||
| 输出: [[1,5]] | ||
| 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 | ||
| ``` | ||
|
|
||
| ------------------- | ||
| 思路: | ||
|
|
||
| 1. 对容器按start值从小到大排序 | ||
| 2. 两两顺序比较,用第一个元素的end值和第二个元素的start值比较 | ||
| 3. 如果后start比前end小,且前end比后end小,则合并! | ||
| 4. 不满足3,则直接插入 | ||
|
|
||
| 时间复杂度O(n) | ||
|
|
||
| ```CPP | ||
| /** | ||
| * Definition for an interval. | ||
| * struct Interval { | ||
| * int start; | ||
| * int end; | ||
| * Interval() : start(0), end(0) {} | ||
| * Interval(int s, int e) : start(s), end(e) {} | ||
| * }; | ||
| */ | ||
| bool cmp(Interval &val1,Interval &val2){ | ||
| return !(val1.start >= val2.start); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里写成 |
||
| } | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<Interval> merge(vector<Interval>& intervals) { | ||
|
|
||
| int len = intervals.size(); | ||
| if(len <= 1)return intervals; | ||
|
|
||
| sort(intervals.begin(),intervals.end(),cmp); | ||
|
|
||
| vector<Interval> ans; | ||
| ans.push_back(intervals[0]); | ||
|
|
||
| for(int i = 1;i<len;i++){ | ||
| if(ans.back().end >= intervals[i].start){ | ||
| ans.back().end = max(ans.back().end,intervals[i].end); | ||
| } | ||
| else{ | ||
| ans.push_back(intervals[i]); | ||
| } | ||
| } | ||
| return ans; | ||
| } | ||
| }; | ||
| ``` | ||
36 changes: 36 additions & 0 deletions
solution/056.Merge Intervals/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Definition for an interval. | ||
| * struct Interval { | ||
| * int start; | ||
| * int end; | ||
| * Interval() : start(0), end(0) {} | ||
| * Interval(int s, int e) : start(s), end(e) {} | ||
| * }; | ||
| */ | ||
| bool cmp(Interval &val1,Interval &val2){ | ||
| return !(val1.start >= val2.start); | ||
| } | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<Interval> merge(vector<Interval>& intervals) { | ||
|
|
||
| int len = intervals.size(); | ||
| if(len <= 1)return intervals; | ||
|
|
||
| sort(intervals.begin(),intervals.end(),cmp); | ||
|
|
||
| vector<Interval> ans; | ||
| ans.push_back(intervals[0]); | ||
|
|
||
| for(int i = 1;i<len;i++){ | ||
| if(ans.back().end >= intervals[i].start){ | ||
| ans.back().end = max(ans.back().end,intervals[i].end); | ||
| } | ||
| else{ | ||
| ans.push_back(intervals[i]); | ||
| } | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.