当前位置:网站首页>移除无效的括号[用数组模拟栈]
移除无效的括号[用数组模拟栈]
2022-06-30 11:43:00 【REN_林森】
前言
需要栈的地方,不一定需要真正的栈,可以用数组等来模拟栈,做到真正的见招拆招。
一、移除无效的括号

二、用数组模拟栈
package everyday.medium;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
// 移除无效括号
public class MinRemoveToMakeValid {
/* target:删除其中多余的括号,或不匹配的括号。 */
public String minRemoveToMakeValid(String s) {
List<Integer> left = new ArrayList<>();// 代表左括号,存入它们的位置。
List<Integer> invalid = new ArrayList<>();// 无效的右括号,存入它们的位置。
int idx = 0;
for (char c : s.toCharArray()) {
if (c == '(') left.add(idx);
if (c == ')') {
if (left.size() > 0) left.remove(left.size() - 1);
else invalid.add(idx);
}
// for cycle
++idx;
}
// 筛选字符
Set<Integer> invalidPos = new HashSet<>(left);
invalidPos.addAll(invalid);
StringBuilder sb = new StringBuilder();
idx = 0;
for (char c : s.toCharArray()) if (!invalidPos.contains(idx++)) sb.append(c);
// 返回有效字符。
return sb.toString();
}
}
总结
1)用数组模拟栈,在数组尾加入元素是入栈,移除数组尾的元素表示出栈。
参考文献
[1] LeetCode 移除无效的括号
边栏推荐
- Uncover the whole link communication process of customer service im
- Speech recognition - Fundamentals (I): introduction [speech to text]
- redis在项目中的使用
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
- Evaluation of IP location query interface Ⅲ
- 谁还记得「张同学」?
- Lucene全文检索工具包学习笔记总结
- 使用深度学习进行生物网络分析
- Object mapping - mapping Mapster
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
猜你喜欢
![Speech recognition - Fundamentals (I): introduction [speech to text]](/img/28/eb45bf27fffaa29108de84aa72a335.png)
Speech recognition - Fundamentals (I): introduction [speech to text]

限时预约|6 月 Apache Pulsar 中文开发者与用户组会议

led背光板的作用是什麼呢?

A quietly rising domestic software, low-key and powerful!

他是上海两大产业的第一功臣,却在遗憾中默默离世

爱可可AI前沿推介(6.30)

zabbix监控TCP连接个数

Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
Redis - ziplist compressed list

“\“id\“ contains an invalid value“
随机推荐
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
ZABBIX monitors the number of TCP connections
AUTOCAD——LEN命令
60 divine vs Code plug-ins!!
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
R language ggplot2 visual Facet: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF) and uses the labs function to add a dynamic time title to t
如何使用插件化机制优雅的封装你的请求hook
Embedded SIG | 多 OS 混合部署框架
再不上市,旷视科技就熬不住了
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
Set up your own website (13)
STM32 移植 RT-Thread 标准版的 FinSH 组件
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF), and uses the labs function to add a dynamic time title to
Lucene全文检索工具包学习笔记总结
[cf] 803 div2 B. Rising Sand
HMS core audio editing service 3D audio technology helps create an immersive auditory feast