Skip to content

Commit e13081e

Browse files
committed
leetcode 3/14/2014
1 parent 8af834e commit e13081e

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

‎LeetCode/canCompleteCircuit.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
4+
int res = -1;
5+
int sum = 0, total = 0;
6+
for (int i = 0; i < gas.size(); ++i) {
7+
sum += gas[i] - cost[i];
8+
total += gas[i] - cost[i];
9+
if (sum < 0) {
10+
res = i;
11+
sum = 0;
12+
}
13+
}
14+
if (total < 0) return -1;
15+
else return res + 1;
16+
}
17+
};

‎LeetCode/insert.cpp‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
#define REP(i,n) for(int i=0;i<(n);++i)
7+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
8+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
9+
typedef long long LL;
10+
11+
struct Interval {
12+
int start;
13+
int end;
14+
Interval() : start(0), end(0) {}
15+
Interval(int s, int e) : start(s), end(e) {}
16+
};
17+
18+
class Solution {
19+
public:
20+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
21+
vector<Interval> res;
22+
int n = intervals.size();
23+
24+
int lt, rt, mt;
25+
26+
lt = 0, rt = n - 1;
27+
while (lt <= rt) {
28+
mt = (lt + rt) / 2;
29+
if (intervals[mt].end >= newInterval.start) rt = mt - 1;
30+
else lt = mt + 1;
31+
}
32+
33+
int idx1 = rt;
34+
FOR(i,0,idx1) res.push_back(intervals[i]);
35+
36+
lt = rt + 1, rt = n - 1;
37+
while (lt <= rt) {
38+
mt = (lt + rt) / 2;
39+
if (intervals[mt].start <= newInterval.end) lt = mt + 1;
40+
else rt = mt - 1;
41+
}
42+
43+
int idx2 = lt;
44+
if (idx2 - idx1 >= 2) {
45+
newInterval.start = min(newInterval.start, intervals[idx1 + 1].start);
46+
newInterval.end = max(newInterval.end, intervals[idx2 - 1].end);
47+
}
48+
res.push_back(newInterval);
49+
FOR(i,idx2,n-1) res.push_back(intervals[i]);
50+
51+
return res;
52+
}
53+
};
54+
55+
int main() {
56+
Solution s;
57+
58+
vector<Interval> mm;
59+
mm.push_back(Interval(1, 3));
60+
mm.push_back(Interval(6, 9));
61+
62+
vector<Interval> ret = s.insert(mm, Interval(2, 5));
63+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
64+
cout << endl;
65+
66+
mm.clear();
67+
mm.push_back(Interval(1, 2));
68+
mm.push_back(Interval(3, 5));
69+
mm.push_back(Interval(6, 7));
70+
mm.push_back(Interval(8, 10));
71+
mm.push_back(Interval(12, 16));
72+
73+
ret = s.insert(mm, Interval(4, 9));
74+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
75+
cout << endl;
76+
77+
mm.clear();
78+
79+
ret = s.insert(mm, Interval(4, 9));
80+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
81+
cout << endl;
82+
83+
mm.clear();
84+
mm.push_back(Interval(1, 3));
85+
86+
ret = s.insert(mm, Interval(4, 9));
87+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
88+
cout << endl;
89+
90+
mm.clear();
91+
mm.push_back(Interval(1, 2));
92+
mm.push_back(Interval(3, 5));
93+
mm.push_back(Interval(6, 7));
94+
mm.push_back(Interval(8, 10));
95+
mm.push_back(Interval(12, 16));
96+
97+
ret = s.insert(mm, Interval(17, 19));
98+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
99+
cout << endl;
100+
101+
mm.clear();
102+
mm.push_back(Interval(1, 2));
103+
mm.push_back(Interval(3, 5));
104+
mm.push_back(Interval(6, 7));
105+
mm.push_back(Interval(8, 10));
106+
mm.push_back(Interval(12, 16));
107+
108+
ret = s.insert(mm, Interval(-14, -9));
109+
REP(i,ret.size()) cout << "[" << ret[i].start << "," << ret[i].end << "] ";
110+
cout << endl;
111+
112+
return 0;
113+
}

‎LeetCode/numDecodings.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
#define REP(i,n) for(int i=0;i<(n);++i)
7+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
8+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
9+
typedef long long LL;
10+
11+
class Solution {
12+
public:
13+
int numDecodings(string s) {
14+
int n = s.length();
15+
if (n == 0) return 0;
16+
REP(i,n) {
17+
if (!(s[i] >= '0' && s[i] <= '9')) return 0;
18+
}
19+
vector<int> mm(n + 5, 0);
20+
mm[n] = 1;
21+
RFOR(i,n-1,0) {
22+
if (s[i] == '0') {
23+
mm[i] = 0;
24+
continue;
25+
}
26+
mm[i] = mm[i + 1];
27+
if (i + 1 < n) {
28+
int num = (s[i] - '0') * 10 + s[i + 1] - '0';
29+
if (num <= 26) mm[i] += mm[i + 2];
30+
}
31+
}
32+
return mm[0];
33+
}
34+
};
35+
36+
int main() {
37+
Solution s;
38+
cout << s.numDecodings("12") << endl;
39+
cout << s.numDecodings("10") << endl;
40+
return 0;
41+
}

0 commit comments

Comments
 (0)