Skip to content

Commit 4484b74

Browse files
LeetCode Medium Design Solutions
1 parent ab4fdee commit 4484b74

3 files changed

+236
-0
lines changed

251. Flatten 2D Vector.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Link: https://leetcode.com/problems/flatten-2d-vector
3+
https://www.lintcode.com/problem/flatten-2d-vector/description
4+
5+
Implement an iterator to flatten a 2d vector.
6+
7+
Example
8+
Example 1:
9+
10+
Input:[[1,2],[3],[4,5,6]]
11+
Output:[1,2,3,4,5,6]
12+
Example 2:
13+
14+
Input:[[7,9],[5]]
15+
Output:[7,9,5]
16+
*/
17+
18+
// Solution
19+
class Vector2D {
20+
public:
21+
int col, row;
22+
vector<vector<int>>vec2d;
23+
Vector2D(vector<vector<int>>& v) {
24+
col = -1;
25+
row = 0;
26+
vec2d = v;
27+
// Initialize your data structure here
28+
}
29+
30+
int next() {
31+
return vec2d[row][col];
32+
// Write your code here
33+
}
34+
35+
bool hasNext() {
36+
col++;
37+
while (row < vec2d.size() && col >= vec2d[row].size()) {
38+
row++;
39+
col = 0;
40+
}
41+
return row < vec2d.size();
42+
// Write your code here
43+
}
44+
};
45+
46+
/**
47+
* Your Vector2D object will be instantiated and called as such:
48+
* Vector2D i(vec2d);
49+
* while (i.hasNext()) cout << i.next();
50+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Link: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
3+
4+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
5+
6+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
7+
8+
Example:
9+
10+
You may serialize the following tree:
11+
12+
1
13+
/ \
14+
2 3
15+
/ \
16+
4 5
17+
18+
as "[1,2,3,null,null,4,5]"
19+
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
20+
21+
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
22+
*/
23+
24+
// Solution
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* struct TreeNode {
29+
* int val;
30+
* TreeNode *left;
31+
* TreeNode *right;
32+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
33+
* };
34+
*/
35+
class Codec {
36+
public:
37+
38+
// Encodes a tree to a single string.
39+
string serialize(TreeNode* root) {
40+
// inorder traversal
41+
queue<TreeNode *> q;
42+
string ans;
43+
q.push(root);
44+
while (!q.empty()) {
45+
int len = q.size();
46+
for (int i = 0; i < len; i++) {
47+
TreeNode *temp = q.front();
48+
q.pop();
49+
if (temp){
50+
ans += to_string(temp -> val) + ",";
51+
q.push (temp -> left);
52+
q.push (temp -> right);
53+
} else {
54+
ans += "null," ;
55+
}
56+
}
57+
}
58+
ans.pop_back();
59+
cout<<ans;
60+
return ans;
61+
}
62+
63+
// Decodes your encoded data to tree.
64+
TreeNode* deserialize(string data) {
65+
// obtaining tree from inorder traversal
66+
queue<TreeNode *> q;
67+
vector<string> v;
68+
69+
stringstream ss(data);
70+
71+
while (ss.good()) {
72+
string substr;
73+
getline(ss, substr, ',');
74+
v.push_back(substr);
75+
}
76+
77+
if (v[0] == "null")
78+
return NULL;
79+
80+
TreeNode *root = new TreeNode(stoi(v[0]));
81+
q.push(root);
82+
int i = 1;
83+
while (!q.empty()) {
84+
TreeNode *temp = q.front();
85+
q.pop();
86+
87+
if(temp) {
88+
temp -> left = v[i] == "null" ? NULL : new TreeNode(stoi(v[i]));
89+
i++;
90+
temp -> right = v[i] == "null" ? NULL : new TreeNode(stoi(v[i]));
91+
i++;
92+
q.push(temp ->left);
93+
q.push(temp ->right);
94+
}
95+
}
96+
97+
98+
return root;
99+
}
100+
};
101+
102+
// Your Codec object will be instantiated and called as such:
103+
// Codec codec;
104+
// codec.deserialize(codec.serialize(root));

380. Insert Delete GetRandom O(1).cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Link: https://leetcode.com/problems/insert-delete-getrandom-o1/
3+
4+
Design a data structure that supports all following operations in average O(1) time.
5+
6+
insert(val): Inserts an item val to the set if not already present.
7+
remove(val): Removes an item val from the set if present.
8+
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
9+
Example:
10+
11+
// Init an empty set.
12+
RandomizedSet randomSet = new RandomizedSet();
13+
14+
// Inserts 1 to the set. Returns true as 1 was inserted successfully.
15+
randomSet.insert(1);
16+
17+
// Returns false as 2 does not exist in the set.
18+
randomSet.remove(2);
19+
20+
// Inserts 2 to the set, returns true. Set now contains [1,2].
21+
randomSet.insert(2);
22+
23+
// getRandom should return either 1 or 2 randomly.
24+
randomSet.getRandom();
25+
26+
// Removes 1 from the set, returns true. Set now contains [2].
27+
randomSet.remove(1);
28+
29+
// 2 was already in the set, so return false.
30+
randomSet.insert(2);
31+
32+
// Since 2 is the only number in the set, getRandom always return 2.
33+
randomSet.getRandom();
34+
*/
35+
36+
// Solution
37+
38+
class RandomizedSet {
39+
public:
40+
/** Initialize your data structure here. */
41+
set<int> st;
42+
RandomizedSet() {
43+
44+
}
45+
46+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
47+
bool insert(int val) {
48+
if (st.find(val) == st.end()) {
49+
st.insert(val);
50+
return true;
51+
}
52+
return false;
53+
}
54+
55+
/** Removes a value from the set. Returns true if the set contained the specified element. */
56+
bool remove(int val) {
57+
if (st.find(val) != st.end()) {
58+
st.erase(st.find(val));
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
/** Get a random element from the set. */
65+
int getRandom() {
66+
int x = rand() % st.size();
67+
auto it = st.begin();
68+
69+
while(x--) {
70+
it ++;
71+
}
72+
return *it;
73+
}
74+
};
75+
76+
/**
77+
* Your RandomizedSet object will be instantiated and called as such:
78+
* RandomizedSet* obj = new RandomizedSet();
79+
* bool param_1 = obj->insert(val);
80+
* bool param_2 = obj->remove(val);
81+
* int param_3 = obj->getRandom();
82+
*/

0 commit comments

Comments
 (0)