当前位置:网站首页>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("", +)
}
}
边栏推荐
- Opening chapter of online document technology - rich text editor
- 你还只知道测试金字塔?
- Nodejs redlock notes
- Pagoda panel installation php7.2 installation phalcon3.3.2
- Graphmae - - lecture rapide des documents
- More appropriate development mode under epidemic situation
- Chapter 3 curve graph of canvas
- Serialization of unity
- Learning event binding of 3D visualization from scratch
- 487. 最大连续1的个数 II ●●
猜你喜欢

GraphMAE----論文快速閱讀

Review of postgraduate English final exam

1279_VMWare Player安装VMWare Tools时VSock安装失败解决

1279_ Vsock installation failure resolution when VMware player installs VMware Tools

Part 1: building OpenGL environment

Swift extension chainlayout (UI chain layout) (source code)

蓝桥杯_N 皇后问题

Écouter le réseau d'extension SWIFT (source)

Blue Bridge Cup_ Queen n problem

JDBC 在性能测试中的应用
随机推荐
Application of JDBC in performance test
51 single chip microcomputer_ External interrupt and timer / Counter interrupt
pyQt 中 QMenu 响应
UTC、GMT、CST
Swift 基礎 閉包/Block的使用(源碼)
Part 1: building OpenGL environment
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
How to design a highly available and extended image storage function
LINQ query (2)
权限模型 DAC ACL RBAC ABAC
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
Introduction to software engineering - Chapter 3 - Requirements Analysis
李白最经典的20首诗排行榜
一文带你了解Windows操作系统安全,保护自己的电脑不受侵害
VR is destined to reappear in the Jianghu?
自动化测试的未来趋势
Phonics
05-ubuntu安装mysql8
Swift foundation features unique to swift
工控机防破解