Skip to content

Commit 0c6137a

Browse files
committed
194/203
1 parent 04a9f84 commit 0c6137a

23 files changed

+387
-1
lines changed

Diff for: README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 191/207 problems.
5+
Solved 194/203 problems.
66

77
## Database
88

@@ -12,6 +12,18 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[contains-duplicate-ii.cc](contains-duplicate-ii.cc)|
16+
|218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)|[the-skyline-problem.cc](the-skyline-problem.cc)|
17+
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[contains-duplicate.cc](contains-duplicate.cc)|
18+
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)|[combination-sum-iii.cc](combination-sum-iii.cc)|
19+
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|[kth-largest-element-in-an-array.cc](kth-largest-element-in-an-array.cc)|
20+
|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)|[shortest-palindrome.cc](shortest-palindrome.cc)|
21+
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)|[house-robber-ii.cc](house-robber-ii.cc)|
22+
|212|[Word Search II](https://leetcode.com/problems/word-search-ii/)|[word-search-ii.cc](word-search-ii.cc)|
23+
|211|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)|[add-and-search-word-data-structure-design.cc](add-and-search-word-data-structure-design.cc)|
24+
|210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)|[course-schedule-ii.cc](course-schedule-ii.cc)|
25+
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[minimum-size-subarray-sum.cc](minimum-size-subarray-sum.cc)|
26+
|208|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)|[implement-trie-prefix-tree.cc](implement-trie-prefix-tree.cc)|
1527
|207|[Course Schedule](https://leetcode.com/problems/course-schedule/)|[course-schedule.cc](course-schedule.cc)|
1628
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[reverse-linked-list.cc](reverse-linked-list.cc)|
1729
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[isomorphic-strings.cc](isomorphic-strings.cc)|

Diff for: add-and-search-word-data-structure-design.cc

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Add and Search Word - Data structure design
2+
class WordDictionary {
3+
public:
4+
class TrieNode {
5+
public:
6+
// Initialize your data structure here.
7+
bool has;
8+
TrieNode *c[26];
9+
TrieNode() : has(false) {
10+
fill_n(c, 26, nullptr);
11+
}
12+
} *root;
13+
14+
WordDictionary() : root(new TrieNode) {}
15+
16+
// Adds a word into the data structure.
17+
void addWord(string word) {
18+
auto x = root;
19+
for (auto c: word) {
20+
if (! x->c[c-'a'])
21+
x->c[c-'a'] = new TrieNode;
22+
x = x->c[c-'a'];
23+
}
24+
x->has = true;
25+
}
26+
27+
// Returns if the word is in the data structure. A word could
28+
// contain the dot character '.' to represent any one letter.
29+
bool search(string word) {
30+
return search(word, 0, root);
31+
}
32+
33+
bool search(const string &word, string::size_type i, TrieNode *x) {
34+
for (; i < word.size(); i++) {
35+
if (word[i] == '.') {
36+
for (int j = 0; j < 26; j++)
37+
if (x->c[j] && search(word, i+1, x->c[j]))
38+
return true;
39+
return false;
40+
}
41+
if (! x->c[word[i]-'a'])
42+
return false;
43+
x = x->c[word[i]-'a'];
44+
}
45+
return x->has;
46+
}
47+
};
48+
49+
// Your WordDictionary object will be instantiated and called as such:
50+
// WordDictionary wordDictionary;
51+
// wordDictionary.addWord("word");
52+
// wordDictionary.search("pattern");

Diff for: binary-tree-right-side-view.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Binary Tree Right Side View
12
// pre-order
23

34
class Solution {

Diff for: bitwise-and-of-numbers-range.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Bitwise AND of Numbers Range
12
// __builtin_clz
23

34
class Solution {

Diff for: combination-sum-iii.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Combination Sum III
2+
class Solution {
3+
public:
4+
vector<vector<int>> combinationSum3(int k, int n) {
5+
vector<int> a;
6+
vector<vector<int>> r;
7+
f(k, n, a, r);
8+
return r;
9+
}
10+
void f(int k, int n, vector<int> &a, vector<vector<int>> &r) {
11+
if (n < 0) return;
12+
if (! k)
13+
n || (r.push_back(a), 0);
14+
else
15+
for (int i = a.empty() ? 1 : a.back()+1; i <= 9; i++) {
16+
a.push_back(i);
17+
f(k-1, n-i, a, r);
18+
a.pop_back();
19+
}
20+
}
21+
};

Diff for: contains-duplicate-ii.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Contains Duplicate II
2+
class Solution {
3+
public:
4+
bool containsNearbyDuplicate(vector<int>& a, int k) {
5+
map<int, int> m;
6+
for (size_t i = 0; i < a.size(); i++) {
7+
if (m.count(a[i]) && i-m[a[i]] <= k)
8+
return true;
9+
m[a[i]] = i;
10+
}
11+
return false;
12+
}
13+
};

Diff for: contains-duplicate.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Contains Duplicate
2+
class Solution {
3+
public:
4+
bool containsDuplicate(vector<int>& a) {
5+
auto i = a.end();
6+
sort(a.begin(), a.end());
7+
return unique(a.begin(), a.end()) != i;
8+
}
9+
};

Diff for: count-primes.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Count Primes
12
// c(n,m) = c(n,m-1) - (c(n/m,m-1)-cnt(m-1))
23
// cnt(m) = c(m, sqrt(m))
34

Diff for: course-schedule-ii.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Course Schedule II
2+
class Solution {
3+
public:
4+
vector<int> findOrder(int n, vector<pair<int, int>>& a) {
5+
vector<int> d(n, 0), ret;
6+
for (auto &x: a) {
7+
swap(x.first, x.second);
8+
d[x.second]++;
9+
}
10+
sort(a.begin(), a.end());
11+
int top = -1;
12+
for (int i = 0; i < n; i++)
13+
if (! d[i])
14+
d[i] = top, top = i;
15+
while (top != -1) {
16+
int x = top;
17+
top = d[x];
18+
ret.push_back(x);
19+
auto i = lower_bound(a.begin(), a.end(), make_pair(x, 0)),
20+
ii = lower_bound(a.begin(), a.end(), make_pair(x, n));
21+
for (; i != ii; ++i)
22+
if (! --d[i->second])
23+
d[i->second] = top, top = i->second;
24+
}
25+
if (ret.size() < n)
26+
ret.clear();
27+
return ret;
28+
}
29+
};

Diff for: course-schedule.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Course Schedule
12
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
23
#define REP(i, n) FOR(i, 0, n)
34

Diff for: happy-number.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Happy Number
12
class Solution {
23
public:
34
bool isHappy(int n) {

Diff for: house-robber-ii.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// House Robber II
2+
class Solution {
3+
public:
4+
int rob(vector<int>& a) {
5+
int n = (int)a.size(), t, x = 0, y = 0;
6+
if (n == 1) return a[0];
7+
for (int i = 0; i < n-1; i++)
8+
t = x, x = max(x, y+a[i]), y = t;
9+
int r = x;
10+
x = y = 0;
11+
for (int i = 1; i < n; i++)
12+
t = x, x = max(x, y+a[i]), y = t;
13+
return max(r, x);
14+
}
15+
};

Diff for: house-robber.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// House Robber
12
class Solution {
23
public:
34
int rob(vector<int> &a) {

Diff for: implement-trie-prefix-tree.cc

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Implement Trie (Prefix Tree)
2+
class TrieNode {
3+
public:
4+
// Initialize your data structure here.
5+
bool has;
6+
TrieNode *c[26];
7+
TrieNode() : has(false) {
8+
fill_n(c, 26, nullptr);
9+
}
10+
};
11+
12+
class Trie {
13+
public:
14+
Trie() {
15+
root = new TrieNode();
16+
}
17+
18+
// Inserts a word into the trie.
19+
void insert(string s) {
20+
auto x = root;
21+
for (auto c: s) {
22+
if (! x->c[c-'a'])
23+
x->c[c-'a'] = new TrieNode;
24+
x = x->c[c-'a'];
25+
}
26+
x->has = true;
27+
}
28+
29+
// Returns if the word is in the trie.
30+
bool search(string key) {
31+
auto x = root;
32+
for (auto c: key) {
33+
if (! x->c[c-'a'])
34+
return false;
35+
x = x->c[c-'a'];
36+
}
37+
return x->has;
38+
}
39+
40+
// Returns if there is any word in the trie
41+
// that starts with the given prefix.
42+
bool startsWith(string prefix) {
43+
auto x = root;
44+
for (auto c: prefix) {
45+
if (! x->c[c-'a'])
46+
return false;
47+
x = x->c[c-'a'];
48+
}
49+
return true;
50+
}
51+
52+
private:
53+
TrieNode* root;
54+
};
55+
56+
// Your Trie object will be instantiated and called as such:
57+
// Trie trie;
58+
// trie.insert("somestring");
59+
// trie.search("key");

Diff for: isomorphic-strings.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Isomorphic Strings
12
class Solution {
23
public:
34
bool isIsomorphic(string s, string t) {

Diff for: kth-largest-element-in-an-array.cc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Kth Largest Element in an Array
2+
class Solution {
3+
public:
4+
int findKthLargest(vector<int>& a, int k) {
5+
nth_element(a.begin(), a.end()-k, a.end());
6+
return *(a.end()-k);
7+
}
8+
};

Diff for: minimum-size-subarray-sum.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Minimum Size Subarray Sum
2+
class Solution {
3+
public:
4+
int minSubArrayLen(int s, vector<int>& a) {
5+
int n = (int)a.size(), c = n+1;
6+
for (int i = 0, j = 0; i < n; i++) {
7+
for (; j < n && s > 0; j++)
8+
s -= a[j];
9+
if (s > 0) break;
10+
c = min(c, j-i);
11+
s += a[i];
12+
}
13+
return c > n ? 0 : c;
14+
}
15+
};

Diff for: number-of-islands.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Number of Islands
12
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
23
#define REP(i, n) FOR(i, 0, n)
34

Diff for: remove-linked-list-elements.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Remove Linked List Elements
12
class Solution {
23
public:
34
ListNode* removeElements(ListNode* x, int val) {

Diff for: reverse-linked-list.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Reverse Linked List
12
class Solution {
23
public:
34
ListNode* reverseList(ListNode* x) {

Diff for: shortest-palindrome.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Shortest Palindrome
2+
class Solution {
3+
public:
4+
string shortestPalindrome(string s) {
5+
int n = s.size();
6+
vector<int> border(n, 0);
7+
int j = 0;
8+
for (int i = 1; i < n; i++) {
9+
while (j >= 0 && s[i] != s[j])
10+
j = j ? border[j-1] : -1;
11+
border[i] = ++j;
12+
}
13+
j = 0;
14+
for (int i = 0; i < n; i++) {
15+
while (j >= 0 && s[n-1-i] != s[j])
16+
j = j ? border[j-1] : -1;
17+
j++;
18+
}
19+
string ss = s.substr(j, n-j);
20+
reverse(ss.begin(), ss.end());
21+
return ss+s;
22+
}
23+
};

Diff for: the-skyline-problem.cc

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// The Skyline Problem
2+
class Solution {
3+
public:
4+
vector<pair<int, int>> getSkyline(vector<vector<int>>& a) {
5+
int n = a.size(), xx = -1, x, yy = 0, y = 0;
6+
priority_queue<pair<int, int>> pq; // order by first, second is insignificant
7+
vector<pair<int, int>> r;
8+
for (int i = 0; i < n || pq.size(); ) {
9+
if (pq.empty() || i < n && a[i][0] < pq.top().second) {
10+
x = a[i][0];
11+
pq.emplace(a[i][2], a[i][1]);
12+
i++;
13+
} else {
14+
x = pq.top().second;
15+
while (pq.size() && pq.top().second <= x)
16+
pq.pop();
17+
}
18+
if (x != xx && y != yy) {
19+
r.emplace_back(xx, y);
20+
yy = y;
21+
}
22+
xx = x;
23+
y = pq.empty() ? 0 : pq.top().first;
24+
}
25+
if (y != yy)
26+
r.emplace_back(xx, y);
27+
return r;
28+
}
29+
};
30+
31+
///
32+
33+
class Solution {
34+
public:
35+
vector<pair<int, int>> getSkyline(vector<vector<int>>& a) {
36+
vector<pair<int, int>> ev;
37+
for (auto &e: a) {
38+
ev.emplace_back(e[0], e[2]);
39+
ev.emplace_back(e[1], - e[2]);
40+
}
41+
sort(ev.begin(), ev.end());
42+
int x = -1, y = 0, yy = 0;
43+
multiset<int> h;
44+
vector<pair<int, int>> r;
45+
for (auto e: ev) {
46+
if (e.second > 0)
47+
h.insert(e.second);
48+
else
49+
h.erase(h.find(- e.second));
50+
if (e.first != x && yy != y) {
51+
r.emplace_back(x, y);
52+
yy = y;
53+
}
54+
x = e.first;
55+
y = h.empty() ? 0 : *h.rbegin();
56+
}
57+
if (yy != y)
58+
r.emplace_back(x, y);
59+
return r;
60+
}
61+
};

0 commit comments

Comments
 (0)