zoukankan      html  css  js  c++  java
  • [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:

    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).

    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

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:

    1. 'A' : Absent,缺勤
    2. 'L' : Late,迟到
    3. '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 }
  • 相关阅读:
    Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目
    jquery ajax异步提交表单数据的方法
    python字符串转换成变量的几种方法
    django 线上线下使用不同的数据库 上线:mysql 线下sqlite3 以及debug模式的开和关
    django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段
    nonce和timestamp在Http安全协议中的作用
    Web API接口 安全验证
    .Net环境下的缓存技术介绍
    .Net缓存管理框架CacheManager
    在asp.net web api中利用过滤器设置输出缓存
  • 原文地址:https://www.cnblogs.com/strengthen/p/9841699.html
Copyright © 2011-2022 走看看