C#LeetCode刷题之#551-学生出勤纪录 I(Student Attendance Record I)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the ...
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 551. 学生出勤纪录 I
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- Java实现 LeetCode 551 学生出勤记录 I(暴力大法好)
551. 学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- 551.学生出勤记录I
/* * @lc app=leetcode.cn id=551 lang=java * * [551] 学生出勤记录 I * * https://leetcode-cn.com/problems/st ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
随机推荐
- 据说比Spring快44倍的web开发框架,不妨试试
该框架称为:**light-4j **.官方网站简介:A fast, lightweight and more productive microservices framework.很简单,翻译过来就 ...
- 12C oracle 12.1.0.2版本打补丁
从support 1454618.1文档 中可以下载到各版本的补丁和PSU. 此次采用的是28349311 版本号的psu 和28440711 版本号的jvm.opatch版本号6880880_12 ...
- 【mysql】- 索引简介篇
简介 我们都知道mysql使用存储引擎的是InnoDB,InnoDB使用的索引的对应的数据结构是B+树 结构图: 如上图所示,我们实际用户记录是存放在B+树的最底层的节点上,这些节点也被称为叶子节点或 ...
- ISE第三方编辑器的使用
刚开始使用ISE时候感觉ISE自带的编辑器并没有什么难用的,但是在看到了小梅哥的视频教学中那样行云流水般的操作让我心动不已,由此找到了相关的编辑器.为了以后看着方便直接摘取了前人的经验在我自己的博客中 ...
- React Navigation / React Native Navigation 多种类型的导航结合使用,构造合理回退栈
React Navigation 更新到版本5已经是非常完善的一套导航管理组件, 提供了Stack , Tab , Drawer 导航方式 , 那么我们应该怎样设计和组合应用他们来构建一个完美的回退栈 ...
- Java基础(二)流程语句与数组
Java流程语句详解:https://www.cnblogs.com/jiajia-16/p/6008200.html Java数组详解:https://www.cnblogs.com/jiajia- ...
- Spring JPA实现增删改查
1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...
- Django开发之ORM批量操作
版本 1 Python 3.8.2 2 Django 3.0.6 批量入库 场景: 前端页面通过 textarea 文本框提交一列多行数据到Django后台,后台通过ORM做入库操作 表名: Tabl ...
- PHP date_default_timezone_set() 函数
------------恢复内容开始------------ 实例 设置默认时区: <?php date_default_timezone_set("Asia/Shanghai&quo ...
- PHP fclose() 函数
定义和用法 fclose() 函数关闭打开的文件. 该函数如果成功则返回 TRUE,如果失败则返回 FALSE. 语法 fclose(file) 参数 描述 file 必需.规定要关闭的文件. 实例 ...