日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9841699.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

  • 'A'?: Absent.
  • 'L'?: Late.
  • '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).

    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

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

  • 'A'?: Absent,缺勤
  • 'L'?: Late,遲到
  • 'P'?: Present,到場
  • 如果一個學生的出勤紀錄中不超過一個'A'(缺勤)并且不超過兩個連續的'L'(遲到),那么這個學生會被獎賞。

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

    示例 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 }

    ?

    轉載于:https://www.cnblogs.com/strengthen/p/9841699.html

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的[Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。