Skip to content

Commit 7fa1f6d

Browse files
committed
373
1 parent 207fdbe commit 7fa1f6d

6 files changed

+139
-1
lines changed

README.md

+6-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 368/368 problems.
5+
Solved 373/373 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[perfect-rectangle.cc](perfect-rectangle.cc)|
16+
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/)|[find-the-difference.cc](find-the-difference.cc)|
17+
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[longest-absolute-file-path.cc](longest-absolute-file-path.cc)|
18+
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|[first-unique-character-in-a-string.cc](first-unique-character-in-a-string.cc)|
19+
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[lexicographical-numbers.cc](lexicographical-numbers.cc)|
1520
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/)|[mini-parser.cc](mini-parser.cc)|
1621
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|[shuffle-an-array.cc](shuffle-an-array.cc)|
1722
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/)|[ransom-note.cc](ransom-note.cc)|

find-the-difference.cc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Find the Difference
2+
class Solution {
3+
public:
4+
char findTheDifference(string s, string t) {
5+
long x = 0;
6+
for (char i: t) x ^= i;
7+
for (char i: s) x ^= i;
8+
return x;
9+
}
10+
};

first-unique-character-in-a-string.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// First Unique Character in a String
2+
class Solution {
3+
public:
4+
int firstUniqChar(string s) {
5+
int a[26] = {}, i = 0;
6+
for (char c: s)
7+
a[c-'a']++;
8+
for (; i < s.size(); i++)
9+
if (a[s[i]-'a'] == 1)
10+
return i;
11+
return -1;
12+
}
13+
};

lexicographical-numbers.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Lexicographical Numbers
2+
class Solution {
3+
public:
4+
vector<int> lexicalOrder(int n) {
5+
vector<int> r;
6+
r.push_back(1);
7+
for (int x = 1, i = 1; i < n; i++) {
8+
if (x*10 <= n)
9+
x *= 10;
10+
else {
11+
while (x == n || x%10 == 9)
12+
x /= 10;
13+
x++;
14+
}
15+
r.push_back(x);
16+
}
17+
return r;
18+
}
19+
};

longest-absolute-file-path.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Longest Absolute File Path
2+
class Solution {
3+
public:
4+
int lengthLongestPath(string input) {
5+
vector<int> st;
6+
int r = 0;
7+
for (int j, i = 0; i < input.size(); i = j+1) {
8+
char dot = 0;
9+
for (j = i; j < input.size() && input[j] != '\n'; j++)
10+
dot |= input[j] == '.';
11+
int t = 0;
12+
for (; input[i] == '\t'; i++)
13+
t++;
14+
st.resize(t);
15+
st.push_back((st.empty() ? 0 : st.back()+1)+j-i);
16+
if (dot)
17+
r = max(r, st.back());
18+
}
19+
return r;
20+
}
21+
};

perfect-rectangle.cc

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Perfect Rectangle
2+
namespace std {
3+
template<>
4+
struct hash<pair<int, int>> {
5+
size_t operator()(const pair<int, int>& x) const {
6+
return size_t(x.first) << 32 | x.second;
7+
}
8+
};
9+
};
10+
11+
class Solution {
12+
public:
13+
bool isRectangleCover(vector<vector<int>>& rectangles) {
14+
int x0 = INT_MAX, x1 = INT_MIN, y0 = INT_MAX, y1 = INT_MIN;
15+
unordered_map<pair<int, int>, int> m;
16+
for (auto& a: rectangles) {
17+
x0 = min(x0, a[0]);
18+
y0 = min(y0, a[1]);
19+
x1 = max(x1, a[2]);
20+
y1 = max(y1, a[3]);
21+
int& m1 = m[make_pair(a[0], a[1])]; if (m1 & 1) return false; m1 |= 1;
22+
int& m2 = m[make_pair(a[0], a[3])]; if (m2 & 2) return false; m2 |= 2;
23+
int& m4 = m[make_pair(a[2], a[3])]; if (m4 & 4) return false; m4 |= 4;
24+
int& m8 = m[make_pair(a[2], a[1])]; if (m8 & 8) return false; m8 |= 8;
25+
}
26+
for (auto& i: m) {
27+
int x, y, mask = i.second;
28+
tie(x, y) = i.first;
29+
if ((x == x0 || x == x1) && (y == y0 || y == y1)) {
30+
if (__builtin_popcount(mask) != 1)
31+
return false;
32+
} else {
33+
if (mask != 3 && mask != 6 && mask != 12 && mask != 9 && mask != 15)
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
39+
};
40+
41+
/// sweep line
42+
43+
class Solution {
44+
public:
45+
bool isRectangleCover(vector<vector<int>>& rectangles) {
46+
long area = 0, x0 = INT_MAX, x1 = INT_MIN, y0 = INT_MAX, y1 = INT_MIN;
47+
vector<pair<int, pair<int, int>>> b;
48+
set<pair<int, int>> active;
49+
for (auto& a: rectangles) {
50+
x0 = min(x0, long(a[0]));
51+
y0 = min(y0, long(a[1]));
52+
x1 = max(x1, long(a[2]));
53+
y1 = max(y1, long(a[3]));
54+
area += (long(a[2])-a[0])*(long(a[3])-a[1]);
55+
b.emplace_back(a[0]*2+1, make_pair(a[1], a[3]));
56+
b.emplace_back(a[2]*2, make_pair(a[1], a[3]));
57+
}
58+
sort(b.begin(), b.end());
59+
for (auto& a: b)
60+
if (a.first % 2) {
61+
auto it = active.lower_bound(a.second);
62+
if (it != active.begin() && a.second.first < prev(it)->second ||
63+
it != active.end() && it->first < a.second.second)
64+
return false;
65+
active.insert(it, a.second);
66+
} else
67+
active.erase(a.second);
68+
return area == (x1-x0)*(y1-y0);
69+
}
70+
};

0 commit comments

Comments
 (0)