在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds:
Otherwise, we define that this word doesn't use capitals in a right way. Example 1: Input: "USA" Output: True Example 2: Input: "FlaG" Output: False Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters. 给定一个单词,你需要判断单词的大写使用是否正确。 我们定义,在以下情况时,单词的大写用法是正确的:
否则,我们定义这个单词没有正确使用大写字母。 示例 1: 输入: "USA" 输出: True 示例 2: 输入: "FlaG" 输出: False 注意: 输入是由大写和小写拉丁字母组成的非空单词。 12ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 if word == nil || word == "" {return false} 4 //大写 5 let up:String = word.uppercased() 6 //小写 7 let low:String = word.lowercased() 8 //首字母大写 9 let str = word.capitalized 10 if word == up || word == low || word == str 11 { 12 return true 13 } 14 return false 15 } 16 } 8ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 if (word.count <= 1) { 4 return true 5 } 6 7 var index = word.startIndex 8 let end = word.index(before: word.endIndex) 9 10 if (word[index] > "Z") { 11 while (index < end) { 12 word.formIndex(after: &index) 13 if (word[index] < "a") { 14 return false 15 } 16 } 17 } else { 18 word.formIndex(after: &index) 19 if (word[index] > "Z") { 20 while (index < end) { 21 word.formIndex(after: &index) 22 if (word[index] < "a") { 23 return false 24 } 25 } 26 } else { 27 while (index < end) { 28 word.formIndex(after: &index) 29 if (word[index] > "Z") { 30 return false 31 } 32 } 33 } 34 } 35 36 return true 37 } 38 } 16ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 return word.isProper() 4 } 5 } 6 7 extension Character { 8 func isUppercased() -> Bool { 9 let stringVal = String(self) 10 return stringVal.uppercased() == stringVal 11 } 12 } 13 14 extension String { 15 func isProper() -> Bool { 16 guard let first = self.first else { return false } 17 let remaining = self.dropFirst() 18 let remainingIsLower = remaining.lowercased() == remaining 19 let remainingIsUpper = remaining.uppercased() == remaining 20 return first.isUppercased() ? remainingIsUpper || remainingIsLower : remainingIsLower 21 } 22 } 20ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 var allCaps = true 4 var allNotCaps = true 5 var firstCaps = true 6 7 for i in 0..<word.count { 8 let char = String(word[word.index(word.startIndex, offsetBy: i)]) 9 allCaps = allCaps && char == char.capitalized 10 allNotCaps = allNotCaps && char != char.capitalized 11 firstCaps = firstCaps && (i == 0 && char == char.capitalized) || firstCaps && (i != 0 && char != char.capitalized) 12 } 13 14 return allCaps || allNotCaps || firstCaps 15 } 16 } 20ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 let words = word.split(separator: " ") 4 for word in words { 5 var firstCapital = false 6 var secondCapital = false 7 for (index, scalar) in word.unicodeScalars.enumerated() { 8 let value = scalar.value 9 if index == 0 { 10 if value >= 65 && value <= 90 { 11 firstCapital = true 12 } 13 } else if index == 1 { 14 if value >= 65 && value <= 90 { 15 if !firstCapital { 16 return false 17 } 18 19 secondCapital = true 20 } 21 } else { 22 if value >= 65 && value <= 90 { 23 if !secondCapital { 24 return false 25 } 26 } else { 27 if secondCapital { 28 return false 29 } 30 } 31 } 32 } 33 } 34 35 return true 36 } 37 } 24ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 let lower = word.lowercased() 4 let upper = word.uppercased() 5 let initials = upper[word.startIndex] 6 let camel = lower.replacingCharacters(in: lower.startIndex..<lower.index(after: lower.startIndex), with: String(initials)) 7 8 9 return word == lower || 10 word == upper || 11 word == camel 12 } 13 } 28ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 var i = 0 4 var first = 0 5 var flag = false 6 var temp = true 7 if word.count == 1 { 8 return true 9 } 10 11 for w in word.unicodeScalars { 12 13 var num = 0 14 15 if i == 0 { 16 first = Int(w.value) 17 }else{ 18 num = Int(w.value) 19 } 20 21 if first >= 97 && first <= 122 { 22 if num >= 97 && num <= 122 { 23 flag = true 24 }else if num >= 65 && num <= 90 { 25 return false 26 } 27 }else{ 28 29 if num >= 97 && num <= 122 { 30 flag = true 31 }else if num >= 65 && num <= 90 { 32 temp = false 33 } 34 } 35 36 i += 1 37 } 38 39 if flag == false && temp == false { 40 return true 41 } 42 43 return flag&&temp 44 } 45 } 36ms 1 class Solution { 2 func detectCapitalUse(_ word: String) -> Bool { 3 return word == word.capitalized || word == word.uppercased() || word == word.lowercased() 4 5 } 6 }
|
请发表评论