[Swift]LeetCode520. 檢測大寫字母 | Detect Capital

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-diiklktn-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a word, you need to judge whether the usage of capitals in it is right or not.git

We define the usage of capitals in a word to be right when one of the following cases holds:github

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".

Otherwise, we define that this word doesn't use capitals in a right way.api

 Example 1:微信

Input: "USA"
Output: True

 Example 2:this

Input: "FlaG"
Output: False

 Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.spa


給定一個單詞,你須要判斷單詞的大寫使用是否正確。scala

咱們定義,在如下狀況時,單詞的大寫用法是正確的:code

  1. 所有字母都是大寫,好比"USA"。
  2. 單詞中全部字母都不是大寫,好比"leetcode"。
  3. 若是單詞不僅含有一個字母,只有首字母大寫, 好比 "Google"。

不然,咱們定義這個單詞沒有正確使用大寫字母。orm

示例 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 }
相關文章
相關標籤/搜索