Skip to content

Commit 0ddd7f3

Browse files
committed
n
1 parent c3f4709 commit 0ddd7f3

File tree

12 files changed

+210
-0
lines changed

12 files changed

+210
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
241241
#### [334. Increasing Triplet Subsequence](https://github.com/hitzzc/go-leetcode/tree/master/increasing_triplet_subsequence)
242242
#### [335. Self Crossing](https://github.com/hitzzc/go-leetcode/tree/master/self_crossing)
243243
#### [336. Palindrome Pairs](https://github.com/hitzzc/go-leetcode/tree/master/palindrome_pairs)
244+
#### [337. House Robber III](https://github.com/hitzzc/go-leetcode/tree/master/house_robber_III)
245+
#### [338. Counting Bits](https://github.com/hitzzc/go-leetcode/tree/master/counting_bits)
246+
#### [341. Flatten Nested List Iterator](https://github.com/hitzzc/go-leetcode/tree/master/flatten_nested_list_iterator)
247+
#### [342. Power of Four](https://github.com/hitzzc/go-leetcode/tree/master/power_of_four)
248+
#### [343. Integer Break](https://github.com/hitzzc/go-leetcode/tree/master/integer_break)
249+
#### [344. Reverse String](https://github.com/hitzzc/go-leetcode/tree/master/reverse_string)
250+
#### [345. Reverse Vowels of a String](https://github.com/hitzzc/go-leetcode/tree/master/reverse_vowels_of_a_string)
251+
#### [347. Top K Frequent Elements](https://github.com/hitzzc/go-leetcode/tree/master/top_k_frequent_elements)
252+
#### [349. Intersection of Two Arrays](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays)
253+
#### [350. Intersection of Two Arrays II](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays_II)
244254

245255

246256

counting_bits/counting_bits.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> countBits(int num) {
4+
vector<int> result(num+1);
5+
result[0] = 0;
6+
for (int i = 1; i <= num; ++i) {
7+
result[i] = result[i&(i-1)] + 1;
8+
}
9+
return result;
10+
}
11+
};

counting_bits/t

9.46 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* class NestedInteger {
5+
* public:
6+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* bool isInteger() const;
8+
*
9+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
10+
* // The result is undefined if this NestedInteger holds a nested list
11+
* int getInteger() const;
12+
*
13+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
14+
* // The result is undefined if this NestedInteger holds a single integer
15+
* const vector<NestedInteger> &getList() const;
16+
* };
17+
*/
18+
class NestedIterator {
19+
vector<int> v;
20+
int index = 0;
21+
void flatten(vector<NestedInteger> &nestedList) {
22+
for (auto& item: nestedList) {
23+
if (item.isInteger()) {
24+
v.push_back(item.getInteger());
25+
}else {
26+
flatten(item.getList());
27+
}
28+
}
29+
return;
30+
}
31+
public:
32+
NestedIterator(vector<NestedInteger> &nestedList) {
33+
flatten(nestedList);
34+
}
35+
36+
int next() {
37+
return v[index++];
38+
}
39+
40+
bool hasNext() {
41+
return index < v.size();
42+
}
43+
};
44+
45+
/**
46+
* Your NestedIterator object will be instantiated and called as such:
47+
* NestedIterator i(nestedList);
48+
* while (i.hasNext()) cout << i.next();
49+
*/

house_robber_III/house_robber_III.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct TreeNode {
2+
int val;
3+
TreeNode *left;
4+
TreeNode *right;
5+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
6+
};
7+
8+
class Solution {
9+
unordered_map<TreeNode*, int> records;
10+
public:
11+
int rob(TreeNode* root) {
12+
if (root == NULL) return 0;
13+
if (records.count(root) == 0) {
14+
int left = rob(root->left);
15+
int right = rob(root->right);
16+
int without_left = 0, without_right = 0;
17+
if (root->left) without_left = rob(root->left->left) + rob(root->left->right);
18+
if (root->right) without_right = rob(root->right->left) + rob(root->right->right);
19+
records[root] = max(root->val+without_left+without_right, left+right);
20+
}
21+
return records[root];
22+
}
23+
};

integer_break/integer_break.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int integerBreak(int n) {
4+
if (n == 2) return 1;
5+
if (n == 3) return 2;
6+
int result = 1;
7+
while (n > 4) {
8+
result *= 3;
9+
n -= 3;
10+
}
11+
result *= n;
12+
return result;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
4+
unordered_set<int> s, visited;
5+
for (auto& num: nums1) {
6+
s.insert(num);
7+
}
8+
vector<int> res;
9+
for (auto& num: nums2) {
10+
if (s.count(num) && visited.count(num) == 0) res.push_back(num);
11+
visited.insert(num);
12+
}
13+
return res;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
4+
unordered_map<int, int> m;
5+
for (auto& num: nums1) {
6+
++m[num];
7+
}
8+
vector<int> res;
9+
for (auto& num: nums2) {
10+
if (m[num] > 0) {
11+
--m[num];
12+
res.push_back(num);
13+
}
14+
}
15+
return res;
16+
}
17+
};

power_of_four/power_of_four.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfFour(int num) {
4+
static int mask = 0b01010101010101010101010101010101;
5+
6+
//edge case
7+
if (num<=0) return false;
8+
9+
// there are multiple bits are 1
10+
if ((num & num-1) != 0) return false;
11+
12+
// check which one bit is zero, if the place is 1 or 3 or 5 or 7 or 9...,
13+
// then it is the power of 4
14+
if ((num & mask) != 0) return true;
15+
return false;
16+
}
17+
};

reverse_string/reverse_string.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string reverseString(string s) {
4+
string ret(s.size(), '\0');
5+
for (int i = 0, j = s.size()-1; i <= j; ++i, --j) {
6+
ret[i] = s[j];
7+
ret[j] = s[i];
8+
}
9+
return ret;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string reverseVowels(string s) {
4+
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
5+
string ret(s.size(), '\0');
6+
for (int i = 0, j = s.size()-1; i <= j; ++i, --j) {
7+
while (i < s.size()-1 && vowels.count(s[i]) == 0) {
8+
ret[i] = s[i];
9+
++i;
10+
}
11+
while (j >= 0 && vowels.count(s[j]) == 0) {
12+
ret[j] = s[j];
13+
--j;
14+
}
15+
if (i > j) break;
16+
ret[i] = s[j];
17+
ret[j] = s[i];
18+
}
19+
return ret;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> topKFrequent(vector<int>& nums, int k) {
4+
unordered_map<int, int> mapping;
5+
for (auto num: nums) {
6+
++mapping[num];
7+
}
8+
vector<vector<int>> freq(nums.size()+1);
9+
for (auto m: mapping) {
10+
freq[m.second].push_back(m.first);
11+
}
12+
vector<int> result;
13+
for (int i = freq.size()-1; i >=0 && k > 0; --i) {
14+
for (auto num: freq[i]) {
15+
result.push_back(num);
16+
--k;
17+
if (k == 0) break;
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)