Skip to content

Commit ec265e1

Browse files
author
applewjg
committed
update
1 parent e237818 commit ec265e1

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

InsertInterval.h

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: Jun 7, 2013
4+
Update: Dec 14, 2014
45
Problem: Insert Interval
56
Difficulty: Medium
6-
Source: http://leetcode.com/onlinejudge#question_57
7+
Source: https://oj.leetcode.com/problems/insert-interval/
78
Notes:
89
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
910
You may assume that the intervals were initially sorted according to their start times.
@@ -19,6 +20,8 @@
1920
3. merge [6,7] with [3,9], get newInterval = [3,9];
2021
4. merge [8,10] with [3,9], get newInterval = [3,10];
2122
5. compare [12,16] with [3,10], insert newInterval [3,10], then all the remaining intervals...
23+
Solution 1 : Time O(N).
24+
Solution 2 : Time O(Log(N)).
2225
*/
2326

2427
/**
@@ -32,13 +35,13 @@
3235
*/
3336
class Solution {
3437
public:
35-
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
38+
vector<Interval> insert_1(vector<Interval> &intervals, Interval newInterval) {
3639
vector<Interval> res;
3740
vector<Interval>::iterator it = intervals.begin();
3841
bool inserted = false;
3942
for (; it != intervals.end(); ++it)
4043
{
41-
if (inserted || it->end < newInterval.start) // non-overlaping
44+
if (inserted == true || it->end < newInterval.start) // non-overlaping
4245
{
4346
res.push_back(*it);
4447
}
@@ -54,8 +57,42 @@ class Solution {
5457
newInterval.end = max(it->end, newInterval.end);
5558
}
5659
}
57-
if (!inserted)
60+
if (inserted == false)
5861
res.push_back(newInterval);
5962
return res;
6063
}
64+
vector<Interval> insert_2(vector<Interval> &intervals, Interval newInterval) {
65+
vector<Interval> res;
66+
int n = intervals.size();
67+
int left = 0, right = n-1;
68+
while(left<=right){
69+
int mid = (left+right)/2;
70+
if(intervals[mid].start>newInterval.start) right=mid-1;
71+
else left = mid+1;
72+
}
73+
int idxStart = right;
74+
left = 0; right = n-1;
75+
while(left<=right){
76+
int mid = (left+right)/2;
77+
if(intervals[mid].end>=newInterval.end) right = mid -1;
78+
else left = mid+1;
79+
}
80+
int idxEnd = left;
81+
82+
if (idxStart>=0 && newInterval.start<=intervals[idxStart].end)
83+
{
84+
newInterval.start=intervals[idxStart--].start;
85+
}
86+
87+
if (idxEnd<intervals.size() && newInterval.end>=intervals[idxEnd].start)
88+
{
89+
newInterval.end=intervals[idxEnd++].end;
90+
}
91+
for(int i=0;i<=idxStart;i++)
92+
res.push_back(intervals[i]);
93+
res.push_back(newInterval);
94+
for(int i=idxEnd;i<intervals.size();i++)
95+
res.push_back(intervals[i]);
96+
return res;
97+
}
6198
};

ZigZagConversion.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
Author: Annie Kim, [email protected]
33
Date: Apr 7, 2013
4-
Update: Sep 26, 2013
4+
Update: Dec 14, 2014
55
Problem: ZigZag Conversion
66
Difficulty: Easy
7-
Source: http://leetcode.com/onlinejudge#question_6
7+
Source: https://oj.leetcode.com/problems/zigzag-conversion/
88
Notes:
99
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
1010
@@ -23,23 +23,17 @@
2323
class Solution {
2424
public:
2525
string convert(string s, int nRows) {
26-
if (nRows == 1) return s;
26+
if(nRows <= 1) return s;
27+
int n = s.size();
2728
string res;
28-
int inc = (nRows - 1) * 2, N = s.size();
29-
for (int i = 0; i < nRows; ++i)
30-
{
31-
int j = 0;
32-
while (true)
33-
{
34-
if (i > 0 && i < nRows-1 && j-i >= 0 && j-i < N)
35-
res.push_back(s[j-i]);
36-
if (j+i < N)
37-
res.push_back(s[j+i]);
38-
if (j+i >= N)
39-
break;
40-
j += inc;
29+
for(int i = 0;i < nRows; ++i){
30+
for (int j = 0; j + i < n; j += 2*nRows - 2) {
31+
res.push_back(s[j+i]);
32+
if (i == 0 || i == nRows - 1) continue;
33+
if (j + 2*nRows - 2 - i < n)
34+
res.push_back(s[j + 2*nRows - 2 - i]);
4135
}
4236
}
4337
return res;
4438
}
45-
};
39+
};

0 commit comments

Comments
 (0)