Skip to content

Commit 4e9a79a

Browse files
committed
406/421
1 parent 51d3ea3 commit 4e9a79a

21 files changed

+425
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
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 386/386 problems.
5+
Solved 406/421 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|453|[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements)|[minimum-moves-to-equal-array-elements.cc](minimum-moves-to-equal-array-elements.cc)|
16+
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs)|[number-of-boomerangs.cc](number-of-boomerangs.cc)|
17+
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string)|[find-all-anagrams-in-a-string.cc](find-all-anagrams-in-a-string.cc)|
18+
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii)|[path-sum-iii.cc](path-sum-iii.cc)|
19+
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval)|[find-right-interval.cc](find-right-interval.cc)|
20+
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals)|[non-overlapping-intervals.cc](non-overlapping-intervals.cc)|
21+
|421|[Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array)|[maximum-xor-of-two-numbers-in-an-array.cc](maximum-xor-of-two-numbers-in-an-array.cc)|
22+
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker)|[strong-password-checker.cc](strong-password-checker.cc)|
23+
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board)|[battleships-in-a-board.cc](battleships-in-a-board.cc)|
24+
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow)|[pacific-atlantic-water-flow.cc](pacific-atlantic-water-flow.cc)|
25+
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum)|[partition-equal-subset-sum.cc](partition-equal-subset-sum.cc)|
26+
|415|[Add Strings](https://leetcode.com/problems/add-strings)|[add-strings.cc](add-strings.cc)|
27+
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number)|[third-maximum-number.cc](third-maximum-number.cc)|
28+
|413|[Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices)|[arithmetic-slices.cc](arithmetic-slices.cc)|
29+
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz)|[fizz-buzz.cc](fizz-buzz.cc)|
30+
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum)|[split-array-largest-sum.cc](split-array-largest-sum.cc)|
31+
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome)|[longest-palindrome.cc](longest-palindrome.cc)|
32+
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height)|[queue-reconstruction-by-height.cc](queue-reconstruction-by-height.cc)|
33+
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal)|[convert-a-number-to-hexadecimal.cc](convert-a-number-to-hexadecimal.cc)|
34+
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves)|[sum-of-left-leaves.cc](sum-of-left-leaves.cc)|
1535
|403|[Frog Jump](https://leetcode.com/problems/frog-jump)|[frog-jump.cc](frog-jump.cc)|
1636
|402|[Remove K Digits](https://leetcode.com/problems/remove-k-digits)|[remove-k-digits.cc](remove-k-digits.cc)|
1737
|401|[Binary Watch](https://leetcode.com/problems/binary-watch)|[binary-watch.cc](binary-watch.cc)|

add-strings.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Add Strings
2+
class Solution {
3+
public:
4+
string addStrings(string a, string b) {
5+
string r;
6+
int m = a.size(), n = b.size(), c = 0;
7+
for (int i = 0; i < max(m, n); i++) {
8+
c += (i < m ? a[m-1-i]-'0' : 0) + (i < n ? b[n-1-i]-'0' : 0);
9+
r += '0'+c%10;
10+
c /= 10;
11+
}
12+
if (c)
13+
r += '1';
14+
reverse(r.begin(), r.end());
15+
return r;
16+
}
17+
};

arithmetic-slices.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Arithmetic Slices
2+
class Solution {
3+
public:
4+
int numberOfArithmeticSlices(vector<int>& A) {
5+
int r = 0, s = 0;
6+
for (int i = 2; i < A.size(); i++)
7+
if (A[i-1]-A[i-2] == A[i]-A[i-1])
8+
r += ++s;
9+
else
10+
s = 0;
11+
return r;
12+
}
13+
};

battleships-in-a-board.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Battleships in a Board
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int countBattleships(vector<vector<char>>& a) {
8+
int r = 0;
9+
REP(i, a.size())
10+
REP(j, a[0].size())
11+
if (a[i][j] == 'X' && (! i || a[i-1][j] != 'X') && (! j || a[i][j-1] != 'X'))
12+
r++;
13+
return r;
14+
}
15+
};

convert-a-number-to-hexadecimal.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Convert a Number to Hexadecimal
2+
class Solution {
3+
public:
4+
string toHex(int num) {
5+
string r;
6+
unsigned x = num;
7+
while (r += x%16 < 10 ? '0'+x%16 : 'a'+x%16-10, x /= 16);
8+
reverse(r.begin(), r.end());
9+
return r;
10+
}
11+
};

find-all-anagrams-in-a-string.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Find All Anagrams in a String
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
vector<int> findAnagrams(string s, string p) {
8+
vector<int> r;
9+
if (s.size() < p.size()) return r;
10+
int c[26] = {}, d = 0;
11+
for (char i: p)
12+
c[i-'a']--;
13+
REP(i, p.size())
14+
c[s[i]-'a']++;
15+
for (int i: c)
16+
if (i)
17+
d++;
18+
if (! d)
19+
r.push_back(0);
20+
REP(i, s.size()-p.size()) {
21+
if (! c[s[i+p.size()]-'a']++) d++;
22+
if (! c[s[i+p.size()]-'a']) d--;
23+
if (! c[s[i]-'a']--) d++;
24+
if (! c[s[i]-'a']) d--;
25+
if (! d) r.push_back(i+1);
26+
}
27+
return r;
28+
}
29+
};

find-right-interval.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Find Right Interval
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
vector<int> findRightInterval(vector<Interval>& intervals) {
8+
vector<int> r;
9+
vector<pair<int, int>> a;
10+
REP(i, intervals.size())
11+
a.emplace_back(intervals[i].start, i);
12+
sort(a.begin(), a.end());
13+
for (auto& x: intervals) {
14+
auto it = lower_bound(a.begin(), a.end(), make_pair(x.end, 0));
15+
r.push_back(it == a.end() ? -1 : it->second);
16+
}
17+
return r;
18+
}
19+
};

fizz-buzz.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Fizz Buzz
2+
class Solution {
3+
public:
4+
vector<string> fizzBuzz(int n) {
5+
int fizz = 3, buzz = 5, fizzbuzz = 15;
6+
vector<string> r;
7+
for (int i = 1; i <= n; i++)
8+
if (i == fizzbuzz)
9+
r.push_back("FizzBuzz"), fizz += 3, buzz += 5, fizzbuzz += 15;
10+
else if (i == fizz)
11+
r.push_back("Fizz"), fizz += 3;
12+
else if (i == buzz)
13+
r.push_back("Buzz"), buzz += 5;
14+
else
15+
r.push_back(to_string(i));
16+
return r;
17+
}
18+
};

longest-palindrome.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Longest Palindrome
2+
class Solution {
3+
public:
4+
int longestPalindrome(string s) {
5+
int c[52] = {}, d = 0, r = 0;
6+
for (char i: s)
7+
c[i >= 'a' ? i-'a' : i-'A'+26]++;
8+
for (int i: c) {
9+
d |= i%2;
10+
r += i-i%2;
11+
}
12+
return r+d;
13+
}
14+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Maximum XOR of Two Numbers in an Array
2+
class Solution {
3+
public:
4+
int findMaximumXOR(vector<int>& nums) {
5+
int m = 0, r = 0;
6+
unordered_set<int> s;
7+
for (int i = 30; i >= 0; i--) {
8+
m |= 1<<i;
9+
s.clear();
10+
for (int x: nums)
11+
s.insert(x & m);
12+
int t = r|1<<i;
13+
for (int x: s)
14+
if (s.count(x^t)) {
15+
r = t;
16+
break;
17+
}
18+
}
19+
return r;
20+
}
21+
};

0 commit comments

Comments
 (0)