★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hicncxjo-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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:git
'('
must have a corresponding right parenthesis ')'
.')'
must have a corresponding left parenthesis '('
.'('
must go before the corresponding right parenthesis ')'
.'*'
could be treated as a single right parenthesis ')'
or a single left parenthesis '('
or an empty string.Example 1:github
Input: "()" Output: True
Example 2:微信
Input: "(*)" Output: True
Example 3:app
Input: "(*))" Output: True
Note:函數
給定一個只包含三種字符的字符串:(
,)
和 *
,寫一個函數來檢驗這個字符串是否爲有效字符串。有效字符串具備以下規則:this
(
必須有相應的右括號 )
。)
必須有相應的左括號 (
。(
必須在對應的右括號以前 )
。*
能夠被視爲單個右括號 )
,或單個左括號 (
,或一個空字符串。示例 1:spa
輸入: "()" 輸出: True
示例 2:code
輸入: "(*)" 輸出: True
示例 3:htm
輸入: "(*))" 輸出: 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 }
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 }