File tree 2 files changed +11
-1
lines changed
2609.Find the Longest Balanced Substring of a Binary String
2610.Convert an Array Into a 2D Array With Conditions
2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change 66
66
遍历字符串 $s$,对于当前字符 $c$:
67
67
68
68
- 如果当前字符为 ` '0' ` ,我们判断此时 $one$ 是否大于 $0$,是则将 $zero$ 和 $one$ 重置为 $0$,接下来将 $zero$ 加 $1$。
69
- - 如果当前字符为 ` '1' ` ,则将 $one$ 加 $1$,并更新答案为 $2 \times min(one, zero)$。
69
+ - 如果当前字符为 ` '1' ` ,则将 $one$ 加 $1$,并更新答案为 $ans = max(ans, 2 \times min(one, zero) )$。
70
70
71
71
遍历结束后,即可得到最长的平衡子串的长度。
72
72
Original file line number Diff line number Diff line change @@ -51,6 +51,16 @@ nums 中的所有元素都有用到,并且每一行都由不同的整数组成
51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ ** 方法一:数组或哈希表**
55
+
56
+ 我们先用数组或哈希表 $cnt$ 统计数组 $nums$ 中每个元素出现的次数。
57
+
58
+ 然后遍历 $cnt$,对于每个元素 $x$,我们将其添加到答案列表中的第 $0$ 行,第 $1$ 行,第 $2$ 行,...,第 $cnt[ x] -1$ 行。
59
+
60
+ 最后返回答案列表即可。
61
+
62
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
63
+
54
64
<!-- tabs:start -->
55
65
56
66
### ** Python3**
You can’t perform that action at this time.
0 commit comments