# 【每日算法Day 87】今天我脫單了，所以大家不用做題了！

## 題目鏈接

LeetCode 1111. 有效括號的嵌套深度

## 題目描述

• 空字符串
• 連接，可以記作 `AB``A``B` 連接），其中 `A``B` 都是有效括號字符串
• 嵌套，可以記作 `(A)`，其中 A 是有效括號字符串

• `s` 爲空時，`depth("") = 0`
• `s``A``B` 連接時，`depth(A + B) = max(depth(A), depth(B))`，其中 `A``B` 都是有效括號字符串
• `s` 爲嵌套情況，`depth("(" + A + ")") = 1 + depth(A)`，其中 `A` 是有效括號字符串

```        ```輸入：
seq = "(()())"

[0,1,1,1,1,0]

```
```

```        ```輸入：
seq = "()(())()"

[0,0,0,1,1,0,1,1]

```
```

• `1 <= text.size <= 10000`

## 題解

```        ```if c = '('
cnt := cnt + 1
else
cnt := cnt - 1
```
```

### 簡化

```        ```if c = '('
cnt := cnt + 1
else
cnt := cnt + 1
```
```

```        ```if c = '('
else
```
```

```        ```if c = '('
else
```
```

## 代碼

### c++

```        ```class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
int cnt = 0;
vector<int> res;
for (auto c : seq) {
if (c == '(') {
res.push_back((++cnt)&1);
} else {
res.push_back((cnt--)&1);
}
}
return res;
}
};
```
```

### python

```        ```class Solution:
def maxDepthAfterSplit(self, seq: str) -> List[int]:
cnt = 0
res = []
for c in seq:
if c == '(':
cnt += 1
res.append(cnt&1)
else:
res.append(cnt&1)
cnt -= 1
return res
```
```

### 簡化（c++）

```        ```class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
vector<int> res;
for (int i = 0, sz = seq.size(); i < sz; ++i) {
res.push_back((i&1)^(seq[i]=='('));
}
return res;
}
};
```
```

### 簡化（python）

```        ```class Solution:
def maxDepthAfterSplit(self, seq: str) -> List[int]:
res = []
for i, c in enumerate(seq):
res.append((i&1)^(c=='('))
return res
```
```

### 參考資料



LeetCode 1111. 有效括號的嵌套深度: leetcode-cn.com/problem
{"ft_lang_label":"__label__zh","ft_lang_prob":0.7625911,"math_prob":0.9992828,"size":2946,"snap":"2020-24-2020-29","text_gpt3_token_len":2189,"char_repetition_ratio":0.09245411,"word_repetition_ratio":0.13910761,"special_character_ratio":0.35607603,"punctuation_ratio":0.14414415,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9972553,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-26T19:31:49Z\",\"WARC-Record-ID\":\"<urn:uuid:7b7ce1a1-a4f4-40b1-ad3f-dce4da2bbd79>\",\"Content-Length\":\"39973\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5057b8f0-cfe0-4675-97cf-2ef0be5a953e>\",\"WARC-Concurrent-To\":\"<urn:uuid:5b6d877f-b626-4c7e-9ee3-ea21420e0cb1>\",\"WARC-IP-Address\":\"104.18.45.120\",\"WARC-Target-URI\":\"https://www.twblogs.net/a/5e86b757bd9eee02cba61cae\",\"WARC-Payload-Digest\":\"sha1:UU5V7KWYUXNX7NBKWL6XK4M2NSY4GAO6\",\"WARC-Block-Digest\":\"sha1:6UTRBO6UAPI5JNRI5KCK4F2B4R2AUH7B\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347391309.4_warc_CC-MAIN-20200526191453-20200526221453-00102.warc.gz\"}"}