在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:
Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note:
给定一个只包含三种字符的字符串:
示例 1: 输入: "()" 输出: True 示例 2: 输入: "(*)" 输出: True 示例 3: 输入: "(*))" 输出: True 注意:
4ms 1 class Solution { 2 func checkValidString(_ s: String) -> Bool { 3 let left = Character("(") 4 let right = Character(")") 5 let anyMatch = Character("*") 6 var minLeft = 0 7 var maxLeft = 0 8 for char in s { 9 if char == left { 10 minLeft += 1 11 maxLeft += 1 12 } else if char == right { 13 minLeft -= 1 14 maxLeft -= 1 15 } else if char == anyMatch { 16 minLeft -= 1 17 maxLeft += 1 18 } 19 minLeft = max(0, minLeft) 20 if maxLeft < 0 { 21 return false 22 } 23 } 24 return minLeft == 0 25 } 26 } 8ms 1 class Solution { 2 func checkValidString(_ s: String) -> Bool { 3 let left = Character("(") 4 let right = Character(")") 5 let anyMatch = Character("*") 6 var count = 0 7 for char in s { 8 if char == right { 9 count -= 1 10 } else { 11 count += 1 12 } 13 if count < 0 { 14 return false 15 } 16 } 17 count = 0 18 for char in s.reversed() { 19 if char == left { 20 count -= 1 21 } else { 22 count += 1 23 } 24 if count < 0 { 25 return false 26 } 27 } 28 return true 29 } 30 } Runtime: 12 ms
Memory Usage: 19.7 MB
1 class Solution { 2 func checkValidString(_ s: String) -> Bool { 3 var arr:[Character] = Array(s) 4 var left:[Int] = [Int]() 5 var star:[Int] = [Int]() 6 for i in 0..<arr.count 7 { 8 if arr[i] == "*" 9 { 10 star.append(i) 11 } 12 else if arr[i] == "(" 13 { 14 left.append(i) 15 } 16 else 17 { 18 if left.isEmpty && star.isEmpty 19 { 20 return false 21 } 22 if !left.isEmpty 23 { 24 left.removeLast() 25 } 26 else 27 { 28 star.removeLast() 29 } 30 } 31 } 32 while(!left.isEmpty && !star.isEmpty) 33 { 34 if left.last! > star.last! 35 { 36 return false 37 } 38 left.removeLast() 39 star.removeLast() 40 } 41 return left.isEmpty 42 } 43 } 16ms 1 class Solution { 2 func checkValidString(_ s: String) -> Bool { 3 let left = Character("(") 4 let right = Character(")") 5 let anyMatch = Character("*") 6 var leftStack = [Int]() 7 var anyMatchStack = [Int]() 8 for (i, char) in s.enumerated() { 9 if char == right { 10 if let top = leftStack.last { 11 leftStack.popLast() 12 } else if let top = anyMatchStack.last { 13 anyMatchStack.popLast() 14 } else { 15 return false 16 } 17 } else if char == left { 18 leftStack.append(i) 19 } else if char == anyMatch { 20 anyMatchStack.append(i) 21 } 22 } 23 var leftIndex = leftStack.count - 1 24 var anyMatchIndex = anyMatchStack.count - 1 25 while leftIndex >= 0 && anyMatchIndex >= 0 { 26 if leftStack[leftIndex] > anyMatchStack[anyMatchIndex] { 27 return false 28 } 29 leftIndex -= 1 30 anyMatchIndex -= 1 31 } 32 return leftIndex < 0 33 } 34 } 260ms 1 class Solution { 2 func checkValidString(_ s: String) -> Bool { 3 let str = Array(s) 4 var memo = [[Int]].init(repeating: [Int].init(repeating: -1, count: s.count), count: s.count) 5 6 func isValid(_ l: Int, _ r: Int) -> Int { 7 guard l <= r else { 8 return 1 9 } 10 guard memo[l][r] == -1 else { 11 return memo[l][r] 12 } 13 if l == r && str[l] == "*" { 14 memo[l][r] = 1 15 return 1 16 } 17 18 if (str[l] == "(" || str[l] == "*") && (str[r] == ")" || str[r] == "*") && isValid(l+1, r-1) == 1 { 19 memo[l][r] = 1 20 return 1 21 } 22 23 for k in l ..< r { 24 if isValid(l, k) == 1 && isValid(k+1, r) == 1 { 25 memo[l][r] = 1 26 return 1 27 } 28 } 29 30 memo[l][r] = 0 31 return 0 32 } 33 34 return isValid(0, s.count - 1) == 1 35 } 36 }
|
请发表评论