[Swift]LeetCode551. 學生出勤紀錄 I | Student Attendance Record I

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

You are given a string representing an attendance record for a student. The record only contains the following three characters:git

  1. 'A' : Absent.
  2. 'L' : Late.
  3. 'P' : Present.

 A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).github

You need to return whether the student could be rewarded according to his attendance record.微信

Example 1:spa

Input: "PPALLP"
Output: True

 Example 2:code

Input: "PPALLL"
Output: False

給定一個字符串來表明一個學生的出勤紀錄,這個紀錄僅包含如下三個字符:htm

  1. 'A' : Absent,缺勤
  2. 'L' : Late,遲到
  3. 'P' : Present,到場

若是一個學生的出勤紀錄中不超過一個'A'(缺勤)而且不超過兩個連續的'L'(遲到),那麼這個學生會被獎賞。blog

你須要根據這個學生的出勤紀錄判斷他是否會被獎賞。three

示例 1:字符串

輸入: "PPALLP"
輸出: True

示例 2:

輸入: "PPALLL"
輸出: False

8ms
 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3          if checkOnlyA(s) && checkContinuousL(s)
 4         {
 5             return true
 6         }
 7         return false
 8     }
 9     
10     //檢查不超過一個'A'(缺勤)
11     func checkOnlyA(_ s: String) -> Bool
12     {
13         var count:Int = 0
14         for char in s.characters
15         {
16             if char == "A"
17             {
18                 count += 1
19             }
20             if count > 1
21             {
22                 return false
23             }
24         }
25         return true
26     }
27     
28     //檢查不超過兩個連續的'L'(遲到)
29     func checkContinuousL(_ s: String) -> Bool
30     {
31         if s.count < 3 {return true}
32         for i in 0...s.count-3
33         {
34             var char1:Character = s[s.index(s.startIndex,offsetBy: i)]
35             var char2:Character = s[s.index(s.startIndex,offsetBy: i + 1)]
36             var char3:Character = s[s.index(s.startIndex,offsetBy: i + 2)]
37             if char1 == "L" && char1 == char2 && char2 == char3
38             {
39                 return false
40             }
41         }
42         return true
43     }
44 }

8ma

 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3         var lateCounter = 0
 4         var absentCounter = 0
 5         for character in s {
 6             if character == "L" {
 7                 lateCounter += 1
 8                 if lateCounter > 2 {
 9                     return false
10                 }
11                 continue
12             }
13         
14             lateCounter = 0
15             
16             if character == "A" {
17                 absentCounter += 1
18                 if absentCounter > 1 {
19                     return false
20                 }
21             }            
22         }
23         return true
24     }
25 }

12ms

 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3         let sArray = Array(s)
 4         var absentCount = 0
 5         var latenessCount = 0
 6         for i in 0..<sArray.count {
 7             if sArray[i] == Character("L") {
 8                 latenessCount += 1
 9                 
10                 if latenessCount > 2 {
11                     return false
12                 }
13             } else {
14                 latenessCount = 0
15 
16                 if sArray[i] == Character("A") {
17                     absentCount += 1
18                 }
19             }
20         }
21         
22         return absentCount < 2
23     }
24 }

16ms

 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3       var numberOfAbsenses = 0
 4       var contigousTardies = 0
 5       
 6       for char in s {
 7         switch String(char) {
 8           case "A":
 9             numberOfAbsenses = numberOfAbsenses + 1
10             contigousTardies = 0
11             if numberOfAbsenses == 2 {
12               return false
13             }
14           case "L":
15             contigousTardies = contigousTardies + 1
16           if contigousTardies == 3 {
17               return false
18             }
19           default: 
20             contigousTardies = 0
21             continue
22         }
23       }
24       return true
25     }
26 }

20ms

 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3         var aCount = 0
 4         var lCount = 0
 5         for char in s {
 6             if char == "A" {
 7                 aCount += 1
 8                 if aCount == 2 {
 9                     return false
10                 }
11                 
12                 lCount = 0
13             } else if char == "L" {
14                 lCount += 1
15                 if lCount == 3 {
16                     return false
17                 }
18             } else if char == "P" {
19                 lCount = 0
20             }
21         }
22 
23         return true
24     }
25 }

24ms

 1 class Solution {
 2     func checkRecord(_ s: String) -> Bool {
 3         var absentCount = 0
 4         var temp: Character
 5         
 6         for i in 0..<s.count {
 7             temp = s[s.index(s.startIndex, offsetBy: i)]
 8             if temp == "A" {
 9                 absentCount += 1
10                 if absentCount >= 2 { return false }
11             }
12             else if  (s.count > (i + 2) && temp == "L" 
13                     && s[s.index(s.startIndex, offsetBy: i + 1)] == "L"
14                     && s[s.index(s.startIndex, offsetBy: i + 2)] == "L") {
15                     return false
16                 }
17         }
18         
19         return true
20     }
21 }
相關文章
相關標籤/搜索