[Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9841699.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are given a string representing an attendance record for a student. The record only contains the following three characters:
?A student could be rewarded if his attendance record doesn't contain?more than one 'A' (absent)?or?more than two continuous 'L' (late).
You need to return whether the student could be rewarded according to his attendance record.
Example 1:
Input: "PPALLP" Output: True?Example 2:
Input: "PPALLL" Output: False給定一個(gè)字符串來代表一個(gè)學(xué)生的出勤紀(jì)錄,這個(gè)紀(jì)錄僅包含以下三個(gè)字符:
如果一個(gè)學(xué)生的出勤紀(jì)錄中不超過一個(gè)'A'(缺勤)并且不超過兩個(gè)連續(xù)的'L'(遲到),那么這個(gè)學(xué)生會(huì)被獎(jiǎng)賞。
你需要根據(jù)這個(gè)學(xué)生的出勤紀(jì)錄判斷他是否會(huì)被獎(jiǎng)賞。
示例 1:
輸入: "PPALLP" 輸出: True示例 2:
輸入: "PPALLL" 輸出: False8ms 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 //檢查不超過一個(gè)'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 //檢查不超過兩個(gè)連續(xù)的'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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/9841699.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的[Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。