当前位置:网站首页>13 -- remove invalid parentheses
13 -- remove invalid parentheses
2022-06-24 08:19:00 【JH_ Cao】
Remove invalid brackets
- Ideas
- encounter "(" Push
- encounter ")“, Check whether the stack is empty , The stack is empty. , Delete the current element
Stack is not empty. , Then take out the top element of the stack for comparison
If the elements at the top of the stack can form a pair ”()", Then remove the stack top element - After traversal , Look at the elements in the stack
- When storing stacks , Use a dictionary to store , This will record the index
- When deleting , Delete from back to front , Do not destroy the structure of the index
class facebook_01 {
//s = "lee(t(c)o)de)" // "a)b(c)d" //"(a(b(c)d)" //"())()(((" "()"
//"())()((("
func minRemoveToMakeValid(_ s: String) -> String {
var arr = Array(s).map{
String($0)}
var stack = [[Int: String]]()
var left = 0
while left < arr.count {
if stack.isEmpty {
if arr[left] == ")" {
arr.remove(at: left)
} else {
if arr[left] == "(" {
let dict = [left: "("]
stack.append(dict)
}
left += 1
}
} else {
// The stack is not empty , Take out the top element of the stack to match , To match , Just delete
let topEle = stack[stack.count - 1]
if topEle.first!.value == "(" && arr[left] == ")" {
stack.removeLast()
}
if arr[left] == "(" {
let dict = [left: "("]
stack.append(dict)
}
left += 1
}
}
if !stack.isEmpty {
for i in (0..<stack.count).reversed() {
arr.remove(at: stack[i].first!.key)
}
}
return arr.reduce("", +)
}
}
边栏推荐
- 11--无重复字符的最长子串
- 2022 PMP project management examination agile knowledge points (1)
- Vscode topic recommendation
- Optimization and practice of Tencent cloud EMR for cloud native containerization based on yarn
- 2022年流动式起重机司机特种作业证考试题库及在线模拟考试
- 1279_VMWare Player安装VMWare Tools时VSock安装失败解决
- Atguigu---16-custom instruction
- Chapter 1 overview of canvas
- Application of JDBC in performance test
- Introduction to software engineering - Chapter 2 - feasibility study
猜你喜欢

对于flex:1的详细解释,flex:1

GraphMAE----论文快速阅读

Installation and use of selenium IDE

2022茶艺师(中级)上岗证题库及在线模拟考试

OC Extension 检测手机是否安装某个App(源码)

jwt(json web token)

More than observation | Alibaba cloud observable suite officially released

Understanding of the concept of "quality"
![[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA](/img/a0/f03b8d9c8f5e53078c38cce11f8ad3.png)
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA

Swift Extension NetworkUtil(網絡監聽)(源碼)
随机推荐
Unity culling related technologies
JVM underlying principle analysis
UTC、GMT、CST
疫情下更合适的开发模式
【毕业季】你好陌生人,这是一封粉色信笺
李白最经典的20首诗排行榜
Swift Extension NetworkUtil(网络监听)(源码)
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
Swift extension chainlayout (UI chain layout) (source code)
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
2022年流动式起重机司机特种作业证考试题库及在线模拟考试
Opening chapter of online document technology - rich text editor
Vscode topic recommendation
Atguigu---16-custom instruction
Teach you how to use the reflect package to parse the structure of go - step 1: parameter type check
C language_ Love and hate between string and pointer
基金的募集,交易与登记
Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
Shader common functions
Getting started with ffmpeg