1021. Remove Outermost Parentheses
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.
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.
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.
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
Example 1:
Input: “(()())(())”
Output: “()()()”
Explanation:
The input string is “(()())(())”, with primitive decomposition “(()())” + “(())”.
After removing outer parentheses of each part, this is “()()” + “()” = “()()()”.
Example 2:
Input: “(()())(())(()(()))”
Output: “()()()()(())”
Explanation:
The input string is “(()())(())(()(()))”, with primitive decomposition “(()())” + “(())” + “(()(()))”.
After removing outer parentheses of each part, this is “()()” + “()” + “()(())” = “()()()()(())”.
给定一个由’(’、’)'组成的字符串,将合法括号的最外层一对括号去掉,返回最后留下的括号字符串
事先一个cnt标记,遇到一个左括号+1,右括号-1,若cnt为0,则表示到目前为止的字符串为合法,将最外层括号去掉添加到目标字符串中即可
class Solution {
public:
string removeOuterParentheses(string S) {
string str = "";
int cnt = 0, pos = 0;
for(int i = 0; i < S.length(); ++i)
{
if(S[i] == '(') cnt++;
else cnt--;
if(cnt == 0)
{
str += S.substr(pos + 1, i - pos - 1);
pos = i + 1;
}
}
return str;
}
};
1022. Sum of Root To Leaf Binary Numbers
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
Return the sum of these numbers.
Example 1:
Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
给定一个二叉树,输出所有从根节点到叶子节点的二进制数之和
典型的深度优先,当到达叶子节点时,储存当前的和,注意取模,否则乘2时会超出int存储范围
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
const int MOD = 1e9 + 7;
class Solution {
public:
int sumRootToLeaf(TreeNode* root) {
int sum = 0, cur = 0;
sumRootHelper(root, sum, cur);
return sum;
}
void sumRootHelper(TreeNode* root, int& sum, int cur)
{
if (root == NULL)
{
return;
}
cur = ((cur % MOD) + (cur % MOD) % MOD);
cur += root->val;
if(root->left == NULL && root->right == NULL)
{
sum = (sum % MOD + cur % MOD) % MOD;
return;
}
sumRootHelper(root->left, sum, cur);
sumRootHelper(root->right, sum, cur);
}
};
1023. Camelcase Matching
A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.)
Given a list of queries, and a pattern, return an answer list of booleans, where answer[i] is true if and only if queries[i] matches the pattern.
Example 1:
Input: queries = [“FooBar”,“FooBarTest”,“FootBall”,“FrameBuffer”,“ForceFeedBack”], pattern = “FB”
Output: [true,false,true,true,false]
Explanation:
“FooBar” can be generated like this “F” + “oo” + “B” + “ar”.
“FootBall” can be generated like this “F” + “oot” + “B” + “all”.
“FrameBuffer” can be generated like this “F” + “rame” + “B” + “uffer”.
Example 2:
Input: queries = [“FooBar”,“FooBarTest”,“FootBall”,“FrameBuffer”,“ForceFeedBack”], pattern = “FoBa”
Output: [true,false,true,false,false]
Explanation:
“FooBar” can be generated like this “Fo” + “o” + “Ba” + “r”.
“FootBall” can be generated like this “Fo” + “ot” + “Ba” + “ll”.
给定一个字符串数组和一个模板字符串,判断数组中的每个字符串能否通过给pattern加小写字母得到
有了上述几点考虑,可以写出如下代码:
class Solution {
public:
vector camelMatch(vector& queries, string pattern) {
vector res;
for (int i = 0; i < queries.size(); ++i)
{
string s = queries[i];
// pos 为当前在pattern的位置,每匹配一个字符,pos++
int pos = 0;
bool flag = true;
for (int j = 0; j < s.length(); ++j)
{
// 若pos已到pattern末尾,一旦出现大写字母,则为false
if (pos == pattern.length())
{
if (s[j] < 90)
{
flag = false;
break;
}
else continue;
}
else if (s[j] == pattern[pos])
{
pos++;
}
// 出现模板外的大写字母
else if (s[j] < 90 && s[j] != pattern[pos])
{
flag = false;
break;
}
}
if (pos != pattern.length() || !flag)
{
res.emplace_back(false);
}
else res.emplace_back(true);
}
return res;
}
};
1024. Video Stitching
You are given a series of video clips from a sporting event that lasted T seconds. These video clips can be overlapping with each other and have varied lengths.
Each video clip clips[i] is an interval: it starts at time clips[i][0] and ends at time clips[i][1]. We can cut these clips into segments freely: for example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].
Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]). If the task is impossible, return -1.
Example 1:
Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], T = 10
Output: 3
Explanation:
We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
Then, we can reconstruct the sporting event as follows:
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].
Example 2:
Input: clips = [[0,1],[1,2]], T = 5
Output: -1
Explanation:
We can’t cover [0,5] with only [0,1] and [0,2].
Example 3:
Input: clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], T = 9
Output: 3
Explanation:
We can take clips [0,4], [4,7], and [6,9].
Example 4:
Input: clips = [[0,4],[2,8]], T = 5
Output: 2
Explanation:
Notice you can have extra video after the event ends.
给定一串区间数组和一个整数T,判断数组中的区间能否完全覆盖[0, T], 若能返回最小的个数,否则返回-1
先按照开始时间排序,cnt保存最后的次数, pos 为当前搜索的起始位置的下标, end 为当前可到达的最大终止位置,基本就是循环遍历,直到遍历完整个数组,或终止位置超过目标T,或当前搜索的起始位置比上一个终止位置还大
最后判断可到达的最大终止位置是否超过目标T,若是,返回cnt,否则返回-1
class Solution {
public:
static bool videoStitchingCmp(vector &v1, vector &v2)
{
return v1[0] < v2[0];
}
int videoStitching(vector>& clips, int T) {
sort(clips.begin(), clips.end(), videoStitchingCmp);
int cnt = 0, pos = 0, end = 0, beg = 0;
while (true)
{
if (pos >= clips.size()) break;
if (clips[pos][0] > beg) break;
if (end >= T) break;
cnt++;
for (; pos < clips.size(); ++pos)
{
if (clips[pos][0] <= beg)
{
end = max(end, clips[pos][1]);
}
else break;
}
beg = end;
}
if (end >= T) return cnt;
else return -1;
}
};