Skip to content

Commit dda7c99

Browse files
committed
🚀 09-Oct-2020
1 parent ec42565 commit dda7c99

6 files changed

+439
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
2+
3+
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
4+
5+
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
6+
7+
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
8+
9+
10+
11+
Example 1:
12+
13+
Input: "(()())(())"
14+
Output: "()()()"
15+
Explanation:
16+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
17+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
18+
Example 2:
19+
20+
Input: "(()())(())(()(()))"
21+
Output: "()()()()(())"
22+
Explanation:
23+
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
24+
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
25+
Example 3:
26+
27+
Input: "()()"
28+
Output: ""
29+
Explanation:
30+
The input string is "()()", with primitive decomposition "()" + "()".
31+
After removing outer parentheses of each part, this is "" + "" = "".
32+
33+
34+
Note:
35+
36+
S.length <= 10000
37+
S[i] is "(" or ")"
38+
S is a valid parentheses string
39+
40+
41+
42+
43+
44+
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
45+
46+
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
47+
48+
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
49+
50+
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
51+
52+
53+
54+
Example 1:
55+
56+
Input: "(()())(())"
57+
Output: "()()()"
58+
Explanation:
59+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
60+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
61+
Example 2:
62+
63+
Input: "(()())(())(()(()))"
64+
Output: "()()()()(())"
65+
Explanation:
66+
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
67+
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
68+
Example 3:
69+
70+
Input: "()()"
71+
Output: ""
72+
Explanation:
73+
The input string is "()()", with primitive decomposition "()" + "()".
74+
After removing outer parentheses of each part, this is "" + "" = "".
75+
76+
77+
Note:
78+
79+
S.length <= 10000
80+
S[i] is "(" or ")"
81+
S is a valid parentheses string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
2+
3+
We repeatedly make duplicate removals on S until we no longer can.
4+
5+
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
6+
7+
8+
9+
Example 1:
10+
11+
Input: "abbaca"
12+
Output: "ca"
13+
Explanation:
14+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
15+
16+
17+
Note:
18+
19+
1 <= S.length <= 20000
20+
S consists only of English lowercase letters.
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
class Solution {
34+
public:
35+
string removeDuplicates(string S) {
36+
stack<char> s;
37+
int n=S.length();
38+
s.push(S[0]);
39+
for(int i=1;i<n;i++){
40+
if(!s.empty() && s.top()==S[i])
41+
s.pop();
42+
else s.push(S[i]);
43+
}
44+
string res="";
45+
46+
while(!s.empty()){
47+
res=s.top()+res;
48+
s.pop();
49+
}
50+
return res;
51+
}
52+
};
53+
54+
55+
class Solution {
56+
public:
57+
string removeDuplicates(string S) {
58+
int k=0, n=S.length();
59+
for(int i=0;i<n;i++, k++){
60+
S[k]=S[i];
61+
if(k>0 && S[k-1]==S[k]) k-=2;
62+
}
63+
return S.substr(0, k);
64+
}
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Given a positive integer num consisting only of digits 6 and 9.
2+
3+
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
4+
5+
6+
7+
Example 1:
8+
9+
Input: num = 9669
10+
Output: 9969
11+
Explanation:
12+
Changing the first digit results in 6669.
13+
Changing the second digit results in 9969.
14+
Changing the third digit results in 9699.
15+
Changing the fourth digit results in 9666.
16+
The maximum number is 9969.
17+
Example 2:
18+
19+
Input: num = 9996
20+
Output: 9999
21+
Explanation: Changing the last digit 6 to 9 results in the maximum number.
22+
Example 3:
23+
24+
Input: num = 9999
25+
Output: 9999
26+
Explanation: It is better not to apply any change.
27+
28+
29+
Constraints:
30+
31+
1 <= num <= 10^4
32+
num's digits are 6 or 9.
33+
34+
35+
36+
37+
38+
39+
40+
class Solution {
41+
public:
42+
int maximum69Number (int num) {
43+
string s=to_string(num);
44+
for(int i=0;i<s.length();i++){
45+
if(s[i]=='6'){
46+
s[i]='9';
47+
break;
48+
}
49+
}
50+
return stoi(s);
51+
}
52+
};
53+
54+
55+
56+
57+
class Solution {
58+
public:
59+
int maximum69Number (int num) {
60+
int tmp=num, i=0, six=-1;
61+
while(tmp){
62+
if(tmp%10==6) six=i;
63+
tmp/=10;
64+
i++;
65+
}
66+
67+
return six==-1 ? num : num + 3*pow(10, six);
68+
}
69+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Given an array target and an integer n. In each iteration, you will read a number from list = {1,2,3..., n}.
2+
3+
Build the target array using the following operations:
4+
5+
Push: Read a new element from the beginning list, and push it in the array.
6+
Pop: delete the last element of the array.
7+
If the target array is already built, stop reading more elements.
8+
You are guaranteed that the target array is strictly increasing, only containing numbers between 1 to n inclusive.
9+
10+
Return the operations to build the target array.
11+
12+
You are guaranteed that the answer is unique.
13+
14+
15+
16+
Example 1:
17+
18+
Input: target = [1,3], n = 3
19+
Output: ["Push","Push","Pop","Push"]
20+
Explanation:
21+
Read number 1 and automatically push in the array -> [1]
22+
Read number 2 and automatically push in the array then Pop it -> [1]
23+
Read number 3 and automatically push in the array -> [1,3]
24+
Example 2:
25+
26+
Input: target = [1,2,3], n = 3
27+
Output: ["Push","Push","Push"]
28+
Example 3:
29+
30+
Input: target = [1,2], n = 4
31+
Output: ["Push","Push"]
32+
Explanation: You only need to read the first 2 numbers and stop.
33+
Example 4:
34+
35+
Input: target = [2,3,4], n = 4
36+
Output: ["Push","Pop","Push","Push","Push"]
37+
38+
39+
Constraints:
40+
41+
1 <= target.length <= 100
42+
1 <= target[i] <= 100
43+
1 <= n <= 100
44+
target is strictly increasing.
45+
46+
47+
48+
49+
50+
51+
class Solution {
52+
public:
53+
vector<string> buildArray(vector<int>& target, int n) {
54+
string push="Push", pop="Pop";
55+
vector<string> res;
56+
int i=1;
57+
while(i<=n){
58+
for(auto &x: target){
59+
while(i<x && i!=x){
60+
res.push_back(push);
61+
res.push_back(pop);
62+
i++;
63+
}
64+
if(i==x)res.push_back(push);
65+
i++;
66+
}
67+
}
68+
return res;
69+
}
70+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Serialization is 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.
2+
3+
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.
4+
5+
The encoded string should be as compact as possible.
6+
7+
8+
9+
Example 1:
10+
11+
Input: root = [2,1,3]
12+
Output: [2,1,3]
13+
Example 2:
14+
15+
Input: root = []
16+
Output: []
17+
18+
19+
Constraints:
20+
21+
The number of nodes in the tree is in the range [0, 104].
22+
0 <= Node.val <= 104
23+
The input tree is guaranteed to be a binary search tree.
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
/**
35+
* Definition for a binary tree node.
36+
* struct TreeNode {
37+
* int val;
38+
* TreeNode *left;
39+
* TreeNode *right;
40+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
41+
* };
42+
*/
43+
class Codec {
44+
public:
45+
46+
TreeNode* deserializeUtil(stringstream &s){
47+
string str;
48+
getline(s, str, ',');
49+
if(str=="#") return NULL;
50+
else {
51+
TreeNode* root= new TreeNode(stoi(str));
52+
root->left=deserializeUtil(s);
53+
root->right=deserializeUtil(s);
54+
return root;
55+
}
56+
}
57+
58+
// Encodes a tree to a single string.
59+
string serialize(TreeNode* root) {
60+
if(!root) return "#";
61+
return to_string(root->val) + "," + serialize(root->left) + "," + serialize(root->right);
62+
}
63+
64+
// Decodes your encoded data to tree.
65+
TreeNode* deserialize(string data) {
66+
if(data=="#") return NULL;
67+
stringstream s(data);
68+
return deserializeUtil(s);
69+
}
70+
};
71+
72+
// Your Codec object will be instantiated and called as such:
73+
// Codec* ser = new Codec();
74+
// Codec* deser = new Codec();
75+
// string tree = ser->serialize(root);
76+
// TreeNode* ans = deser->deserialize(tree);
77+
// return ans;

0 commit comments

Comments
 (0)