★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

转载于:https://www.cnblogs.com/strengthen/p/9841699.html

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

  1. LeetCode——552. 学生出勤记录 II(Student Attendance Record II)[困难]——分析及代码(Java)

    LeetCode--552. 学生出勤记录 II[Student Attendance Record II][困难]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 (1)思路 ( ...

  2. Leetcode每日一题-学生出勤记录 II(Student Attendance Record II)

    可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤.迟到.到场).记录中只含下面三种字符: 'A':Absent,缺勤 'L':Late,迟到 'P':Present,到场 ...

  3. 552. Student Attendance Record II(学生出勤记录 II)

    552. Student Attendance Record II 博主看到这题时候第一感觉就是DP方法,但是想了一会方程式写不出来,然后开始怀疑是不是动态方程,想了一下过程感觉极其复杂.看了讨论区, ...

  4. 551. Student Attendance Record I 从字符串判断学生考勤

    [抄题]: You are given a string representing an attendance record for a student. The record only contai ...

  5. 551. Student Attendance Record I -- Python

    551. Student Attendance Record I You are given a string representing an attendance record for a stud ...

  6. C#LeetCode刷题之#551-学生出勤纪录 I​​​​​​​(Student Attendance Record I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3953 访问. 给定一个字符串来代表一个学生的出勤纪录,这个纪录仅 ...

  7. 【LeetCode】Student Attendance Record I记录学生出勤率

    问题描述 You are given a string representing an attendance record for a student. The record only contain ...

  8. LeetCode 551. Student Attendance Record I

    题目: You are given a string representing an attendance record for a student. The record only contains ...

  9. leetcode之Student Attendance Record I(551)

    题目: 给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超 ...

最新文章

  1. 史上最全 Java 多线程面试题及答案
  2. 1、oracle后台进程(数据字典V$BGPROCESS)
  3. Unity,WebGL, 页面JS调用Unity方法
  4. mysql show slave_Mysql复制 show slave status
  5. python微信机器人部署服务器_玩法收藏/云服务器/零基础微信机器人实践( Python )...
  6. mysql 5.764_RHEL5.764位源码编译安装MySQL-5.5.42遇到的问题
  7. 白嫖船长几节课(2)
  8. 在普通类中获取spring容器中的bean
  9. 能破解百度网盘提取码,云盘万能钥匙宣布关闭!
  10. IDEA使用maven helper设置自定义mvn命令
  11. mysql_front安装_MySql5.5安装步骤及MySql_Front视图配置
  12. 如何删除Slader 大数智图PDF阅读器
  13. 简单实现Rectrofit+RXJAVA+Fresco
  14. python图标中文_解决python中matplotlib中文乱码 for Mac
  15. 蚂蚁集团副总裁,任复旦大学人工智能学院院长!
  16. 激光雷达 vs. 雷达【选型比较】
  17. exchange邮箱一直提示密码错误,密码是正确的,求大佬解答
  18. SpringAop篇 (1) AOP 基础之动态代理的实现
  19. 弘辽科技:打造爆款的三大核心。
  20. 徐小平-机会就在每个同学手上-象牙塔内外精英的对话

热门文章

  1. Android应用框架浅析
  2. 网络规划设计师学习攻略(2)
  3. Bad level value for property: .level
  4. hbase数据导入到mysql(转载+自己验证整理,目前失败)
  5. redis连接与redis的python连接
  6. R语言版本查询以及line 1 of `undefined.cases': bad value of `47.25' for attribute `A2'的解决
  7. class与case class的区别(转载)
  8. 大话数据结构 : 二叉排序树
  9. Matlab-OpenCV-VC-混合编程配置
  10. TensorFlow(二)