当前位置:网站首页>16 -- 删除无效的括号
16 -- 删除无效的括号
2022-06-27 15:23:00 【JH_Cao】
题目
- 给你一个由若干括号和字母组成的字符串 s ,删除最小数量的无效括号,使得输入的字符串有效。
返回所有可能的结果。答案可以按 任意顺序 返回。
示例 1:
输入:s = “()())()”
输出:[“(())()”,“()()()”]
示例 2:
输入:s = “(a)())()”
输出:[“(a())()”,“(a)()()”]
示例 3:
输入:s = “)(”
输出:[“”]
- 思路
- 先求出"(“多出的个数left 和”)"多出的个数right
- 再挨个删除,比如多出了两个"(“,一个”(“,所有的情况,只能是挨个删除,先删除第一个”(“,再继续递归,再删除第二个”(“,再递归,删除”)",当left和right等于0,并且字符串符合要求,就加入答案中。
代码如下
//
// facebook04.swift
// ForDreams
//
// Created by cao hua on 2022/6/25.
//
import Foundation
class facebook04 {
//s = "()())()"
//s = "(')'())()"
// "()()')'()"
var res = [String]()
func removeInvalidParentheses(_ s: String) -> [String] {
// 1.遍历左右两边的括号多余的数量
let left = getLRCount(s).first! // 0
let right = getLRCount(s).last! // 1
//
helper(s, 0, left, right)
return res
}
private func getLRCount(_ s: String) -> [Int] {
var left = 0
var right = 0
for c in s {
if String(c) == "(" {
left += 1
} else if String(c) == ")" {
if left == 0 {
right += 1
} else {
left -= 1
}
}
}
return [left, right]
}
private func helper(_ s: String, _ start: Int, _ left: Int, _ right: Int) {
if left == 0 && right == 0 {
if isValid(s) {
res.append(s)
}
return
}
for i in start..<s.count {
if i != start && s.charAt(i) == s.charAt(i - 1) {
continue
}
if left + right > s.count - i { // ")))" -- 要没有添加元素了
return
}
if left > 0 && s.charAt(i) == "(" {
let tempArr = Array(s)
let pre = String(tempArr[0..<i])
let post = String(tempArr[(i+1)..<s.count])
let tempStr = pre + post
helper(tempStr, i, left - 1, right)
}
if right > 0 && s.charAt(i) == ")" {
let tempArr = Array(s)
let pre = String(tempArr[0..<i])
let post = String(tempArr[(i+1)..<s.count])
let tempStr = pre + post
helper(tempStr, i, left, right - 1)
}
}
}
private func isValid(_ s: String) -> Bool {
var cnt = 0
for c in s {
if String(c) == "(" {
cnt += 1
} else if String(c) == ")" {
cnt -= 1
if cnt < 0 {
return false
}
}
}
return cnt == 0
}
}
extension String {
func charAt(_ index: Int) -> Character {
let tempArr = Array(self)
return tempArr[index]
}
}
边栏推荐
- Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and the mobile phone can easily read literature
- American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
- PR second training notes
- A brief analysis of the differences between domestic and foreign e-commerce
- Longest substring without repeated characters (Sword finger offer 48)
- enable_ if
- Openssf security plan: SBOM will drive software supply chain security
- Daily question 3 (2): check binary string field
- Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance
- AI begets the moon, and thousands of miles share the literary heart
猜你喜欢

Julia1.1 installation instructions
![[WUSTCTF2020]girlfriend](/img/a8/33fe5feb7bcbb73ba26a94d226cc4d.png)
[WUSTCTF2020]girlfriend

ThreadLocal之强、弱、软、虚引用

How QT sets some areas to be transparent in the background image

What are the operating modes of the live app? What mode should we choose?

Talk about redis transactions

ThreadLocal之强、弱、軟、虛引用
![[high concurrency] deeply analyze the callable interface](/img/24/33c3011752c8f04937ad68d85d4ece.jpg)
[high concurrency] deeply analyze the callable interface

巧用redis实现点赞功能,它不比mysql香吗?

AQS Abstract queue synchronizer
随机推荐
Jupiter core error
Is there any discount for opening an account now? Is it safe to open an account online?
Top ten Devops best practices worthy of attention in 2022
ReentrantLock、ReentrantReadWriteLock、StampedLock
注解学习总结
LVI: feature extraction and sorting of lidar subsystem
[digital signal processing] discrete time signal (discrete time signal knowledge points | signal definition | signal classification | classification according to certainty | classification according t
Notes learning summary
Handling methods for NVIDIA deepstream running delay, jamming and crash
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
Dynamic Networks and Conditional Computation论文简读和代码合集
Redis persistence
CV领域一代宗师黄煦涛教授86岁冥诞,UIUC专设博士奖学金激励新锐
PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)
Teach you how to package and release the mofish Library
[xman2018 qualifying] pass
Library management system
AQS Abstract queue synchronizer
At a time of oversupply of chips, China, the largest importer, continued to reduce imports, and the United States panicked