es 嵌套查询innerHit API es should嵌套should
A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:
- It is the empty string, or
- It can be written as AB (A concatenated with B), where A and B are VPS's, or
- It can be written as (A), where A is a VPS.
We can similarly define the nesting depth depth(S) of any VPS S as follows:
- depth("") = 0
- depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's
- depth("(" + A + ")") = 1 + depth(A), where A is a VPS.
For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.
Given a VPS seq, split it into two disjoint subsequences A and B, such that A and B are VPS's (and A.length + B.length = seq.length).
Now choose any such A and B such that max(depth(A), depth(B)) is the minimum possible value.
Return an answer array (of length seq.length) that encodes such a choice of A and B: answer[i] = 0 if seq[i] is part of A, else answer[i] = 1. Note that even though multiple answers may exist, you may return any of them.
Example 1:
Input: seq = "(()())" Output: [0,1,1,1,1,0]Example 2:
Input: seq = "()(())()" Output: [0,0,0,1,1,0,1,1]这道题给了一个合法的括号字符串,定义了一种括号深度,就是最深的括号嵌套层数。现在让将这个括号字符串拆分成为两个合法的括号字符串,且二者之中的较大深度最小,就是说要尽可能让二者的深度相同。返回数组中,用0和1来区分不同的字符串。既然是尽可能的平均拆分,那么拆分后的每个字符串的深度一定不会超过原来的一半,就是说有嵌套括号时就要平均分配给两个字符串。什么时候会有嵌套括号呢,就比如 "(())" 这种,要拆分为 "()" 和 "()",而对于没有嵌套括号的,比如 "()()()" 这种,可以都放到一个字符串中都没问题。所以问题的关键就是对于连续的左括号,要将其平均的分配到不同的字符串中。所以处理的方法就是使用一个 level 变量,初始化为0,然后遍历给定括号字符串,若遇到了左括号,则 level 对2取余,将结果存入 res 中,为了避免连续左括号加入同一个组,将 level 自增1。这样接下来又遇到左括号时,就可以加进不同的组,若接下来遇到右括号了,则应该先给 level 自增1,再对2取余,这样就可以跟前一个左括号划分到同一个组中了,参见代码如下:
解法一:
class Solution { public: vector<int> maxDepthAfterSplit(string seq) { int n = seq.size(), level = 0; vector<int> res(n); for (int i = 0; i < n; ++i) { if (seq[i] == '(') { res[i] = level++ % 2; } else { res[i] = ++level % 2; } } return res; } };下面这种写法更加的简洁,但其实思路都是一样的,只不过用i代替了上面的 level 变量的作用了,参见代码如下:
解法二:
class Solution { public: vector<int> maxDepthAfterSplit(string seq) { vector<int> res(seq.size()); for (int i = 0; i < seq.size(); ++i) { res[i] = seq[i] == '(' ? i & 1 : (1 - i & 1); } return res; } };Github 同步地址:
https:///grandyang/leetcode/issues/1111
network error:software caused connection abort解决办法network error software caused connection abort
解决Wine安装时提示You could try running: rpm -Va –nofiles –nodigest问题
BurstNET的linux vps 安装Kloxo面板后一切正常 今天重启导致错误Could not connect to Mysql server
黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
解决mongooseserverselectionerror: connect econnrefused ::1:27017错误的方法
Docker的三种网络代理配置:dockerd pull镜像代理;容器docker run网络代理;docker build代理--build-argdocker pull 代理
解决Kubernetes中的 couldn't get resource list for metrics.k8s.io/v1beta1 错误的全面指南