在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, contains only lowercase characters, or Example 1: Input: s = "lee(t(c)o)de)" Input: s = "a)b(c)d" Input: s = "))((" Input: s = "(a(b(c)d)" Constraints: 1 <= s.length <= 10^5 给你一个由 '('、')' 和小写字母组成的字符串 s。 你需要从字符串中删除最少数目的 '(' 或者 ')' (可以删除任意位置的括号),使得剩下的「括号字符串」有效。 请返回任意一个合法字符串。 有效「括号字符串」应当符合以下 任意一条 要求: 空字符串或只包含小写字母的字符串 示例 1: 输入:s = "lee(t(c)o)de)" 输入:s = "a)b(c)d" 输入:s = "))((" 输入:s = "(a(b(c)d)" 提示: 1 <= s.length <= 10^5 188ms 1 class Solution { 2 func minRemoveToMakeValid(_ s: String) -> String { 3 var parentheses = Array<Int>() 4 var deleteIndex = [Int]() 5 var chars = [Character](s) 6 for i in 0..<s.count { 7 if chars[i] == "(" { 8 parentheses.append(i) 9 } else if chars[i] == ")" { 10 if parentheses.isEmpty { 11 deleteIndex.append(i) 12 } else { 13 parentheses.removeLast() 14 } 15 } 16 } 17 deleteIndex.append(contentsOf: parentheses) 18 let sortedIndex = deleteIndex.sorted { $0 > $1} 19 for index in sortedIndex { 20 chars.remove(at: index) 21 } 22 return String(chars) 23 } 24 } 192ms 1 class Solution { 2 func minRemoveToMakeValid(_ s: String) -> String { 3 var leftParentheses = Array<Int>() 4 var charsLeft = Array<Bool>(repeating: true, count: s.count) 5 var chars = [Character](s) 6 for i in 0..<s.count { 7 if chars[i] == "(" { 8 leftParentheses.append(i) 9 } else if chars[i] == ")" { 10 if leftParentheses.isEmpty { 11 charsLeft[i] = false 12 } else { 13 leftParentheses.popLast() 14 } 15 } 16 } 17 for i in 0..<leftParentheses.count { 18 charsLeft[leftParentheses[i]] = false 19 } 20 21 var ans = "" 22 for i in 0..<chars.count { 23 if charsLeft[i] { 24 ans.append(chars[i]) 25 } 26 } 27 return ans 28 } 29 } 240ms 1 class Solution { 2 func minRemoveToMakeValid(_ s: String) -> String { 3 var opens = 0 4 var result = [Character]() 5 6 for char in s { 7 if char != "(" && char != ")" { 8 result.append(char) 9 } else if char == ")" { 10 if opens > 0 { 11 result.append(char) 12 opens -= 1 13 } 14 } else { 15 opens += 1 16 result.append(char) 17 } 18 } 19 20 if opens == 0 { 21 return String(result) 22 } 23 24 var realResult = [Character]() 25 26 for char in result.reversed() { 27 if char == "(" && opens > 0 { 28 opens -= 1 29 continue 30 } else { 31 realResult.append(char) 32 } 33 } 34 return String(realResult.reversed()) 35 } 36 } Runtime: 256 ms
Memory Usage: 25.7 MB
1 class Solution { 2 func minRemoveToMakeValid(_ s: String) -> String { 3 var arrS:[Character] = Array(s) 4 var stack:[Int] = [Int]() 5 for i in 0..<arrS.count 6 { 7 if arrS[i] == "(" {stack.append(i)} 8 if arrS[i] == ")" 9 { 10 if !stack.isEmpty {stack.removeLast()} 11 else 12 { 13 arrS[i] = "*" 14 } 15 } 16 } 17 while(!stack.isEmpty) 18 { 19 arrS[stack.removeLast()] = "*" 20 } 21 return String(arrS.filter{$0 != "*"}) 22 } 23 }
|
请发表评论