问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3953 访问。

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

'A' : Absent,缺勤

'L' : Late,迟到

'P' : Present,到场

如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。

你需要根据这个学生的出勤纪录判断他是否会被奖赏。

输入: "PPALLP"

输出: True

输入: "PPALLL"

输出: False


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.

Input: "PPALLP"

Output: True

Input: "PPALLL"

Output: False


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3953 访问。

public class Program {

    public static void Main(string[] args) {
var s = "PPALLP"; var res = CheckRecord(s);
Console.WriteLine(res); s = "AAAA"; res = CheckRecord2(s);
Console.WriteLine(res); Console.ReadKey();
} private static bool CheckRecord(string s) {
//一些小技巧
var boolA = (s.Length - s.Replace("A", "").Length) <= 1;
var boolL = (s.Length == s.Replace("LLL", "").Length);
return boolA && boolL;
} private static bool CheckRecord2(string s) {
//传统计数法
var countA = 0;
var countL = 0;
foreach(var c in s) {
if(c == 'A') ++countA;
if(c == 'L') ++countL;
else countL = 0;
if(countA > 1 || countL > 2) return false;
}
return true;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3953 访问。

True
False

分析:

因为使用了部分运行库,不能简单的认为 CheckRecord 的时间复杂度为  。以上2种算法的时间复杂度均为  。

C#LeetCode刷题之#551-学生出勤纪录 I​​​​​​​(Student Attendance Record I)的更多相关文章

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

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

  2. [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  3. 551. 学生出勤纪录 I

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

  4. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  5. Java实现 LeetCode 551 学生出勤记录 I(暴力大法好)

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

  6. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  7. 551.学生出勤记录I

    /* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...

  8. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  9. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

随机推荐

  1. git分支管理的策略和冲突问题

    目录 备注: 知识点 关于分支中的冲突 分支管理的策略 分支策略 备注: 本文参考于廖雪峰老师的博客Git教程.依照其博客进行学习和记录,感谢其无私分享,也欢迎各位查看原文. 知识点 git log ...

  2. Python Ethical Hacking - ARP Spoofing

    Typical Network ARP Spoofing Why ARP Spoofing is possible: 1. Clients accept responses even if they ...

  3. Python Ethical Hacking - Persistence(2)

    Polish the Python code by adding the become_persistent function. #!/usr/bin/env python import json i ...

  4. 五大高效的PDF文件搜索引擎

    当你花了半个多小时在线搜索PDF文档,却发现您找到的文档都不是您需要的PDF格式.如前说述,您可以先打开PDF文档查看是不是PDF格式的,然后再到web浏览器中下载该文档.那么,为了确保您获得的文档是 ...

  5. vuex : 用vuex控制侧栏点亮状态

    上代码. xxx.vue <template> <div id="xxx"> <div class="layout"> &l ...

  6. C++语法小记---继承中的构造和析构顺序

    继承中构造和析构的顺序 先父母,后客人,最后自己 静态变量和全局变量在最开始 析构和构造的顺序完全相反 #include <iostream> #include <string> ...

  7. Python基础学习之环境搭建

    Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的.今天我们将带领Python零基础的初学者完成入门的第一步——环境搭建.本文会先来区分几个在P ...

  8. 学python不是一个入门教程就行,学之前你必须知道这些

    第一 学习方向:python应用方向很多,必须明确学习方向想 全栈开发:就是web应用开发,是用来写网站后台的数据分析:就是做大数据分析的,数据量级起码是千万级别的,做的是大数据分析网络爬虫:其实属于 ...

  9. vue -电子时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Java数据结构和算法(2)之稀疏数组

    1.定义 稀疏数组可以看做是普通二位数组的压缩,但是这里说的普通数组是值无效数据量远大于有效数据量的数组,关于稀疏数组的运用有五子棋盘,地图等.. *当一个数组中大部分元素为0,或者为同一个值的数组时 ...