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的吃透了 足以应付非算法岗 ...
随机推荐
- 太实用了!自己动手写软件——我们的密码PJ器终于完成了
之前我们完成了密码破解工具的界面,今天我们来看看功能实现吧. 目录 编码 提交——功能实现 开始破解——功能实现 读取密码字典 选择协议并执行破解动作 POP3协议的破解函数 IMAP协议的破解函数 ...
- 题解 CF1359B 【New Theatre Square】
题意 有一个 n×m 的广场,其中一部分要铺地砖,地砖有两种, 1 × 1 和 1×2 的,后者只能横着铺,其中, 1 × 1的单价为 x , 1 × 2 的单价为 y , 输入这个广场," ...
- P3756 [CQOI2017]老C的方块
题目链接 看到网格图+最优化问题,当然要想黑白染色搞网络流.不过这道题显然无法用黑白染色搞定. 仔细观察那四种图形,发现都是蓝线两边一定有两个格子,两个格子旁边一定还有且仅有一个格子.因此我们可以这么 ...
- vue+springboot文件上传
//vue element-ui组件 <el-upload style="position: relative;top: -40px;left: 240px;" ...
- centos7.5安装gdal编译环境
安装准备的环境: 名称 类型与版本 软件连接 服务器 linux-centos7.5 jdk 1.8.0_25 ant 1.9.14 http://mirror.bit.edu.cn/apac ...
- 一个文本框的各项说明,包括类似html的table
https://www.cnblogs.com/zhqiux/archive/2013/09/03/3298654.html
- ✨Shell脚本实现Base64 加密解密
加密算法 # !/bin/bash # 全局变量 str="" base64_encode_string(){ # 源数据 source_string=$1 echo " ...
- Elasticsearch必知必会的干货知识二:ES索引操作技巧
该系列上一篇文章<Elasticsearch必知必会的干货知识一:ES索引文档的CRUD> 讲了如何进行index的增删改查,本篇则侧重讲解说明如何对index进行创建.更改.迁移.查询配 ...
- python学习笔记1 -- 函数式编程之高阶函数 sorted排序
python提供了很强大的内置排序函数,妈妈再也不担心我不会写冒泡排序了呀,sorted函数就是这个排序函数,该函数参数准确的说有四个,sorted(参数1,参数2,参数3,参数4). 参数1 是需要 ...
- jquery判断radio是否选中
微交易-实体系统 微交易-虚拟系统 <div class="system"> <div class="systemt"> <l ...