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的吃透了 足以应付非算法岗 ...
随机推荐
- 三种安装python第三方库的方法
还记得第一天的时候我们说python拥有丰富的库,那这么多的第三方库,我们如何使用呢?今天我们可以看一下python库的安装. 方法一:使用python命令进行离线安装 我以urllib5库 ...
- J.U.C体系进阶(五):juc-collections 集合框架
Java - J.U.C体系进阶 作者:Kerwin 邮箱:806857264@qq.com 说到做到,就是我的忍道! juc-collections 集合框架 ConcurrentHashMap C ...
- Ethical Hacking - Web Penetration Testing(4)
CODE EXECUTION VULNS Allows an attacker to execute OS commands. Windows or Linux commands. Can be us ...
- Vue全家桶之一Vue(基础知识篇)
全家桶:Vue本身.状态管理.路由. 异步组件:
- 一张PDF了解JDK11 GC调优秘籍-附PDF下载
目录 简介 废弃的VM选项 Source-File Mode Code Heap状态分析 AppCDS 总结 简介 JDK11相比JDK10,添加了一个新的Source-File Mode,可以直接通 ...
- 怎么理解Python迭代器与生成器?
怎么理解Python迭代器与生成器?在Python中,使用for ... in ... 可以对list.tuple.set和dict数据类型进行迭代,可以把所有数据都过滤出来.如下: ...
- eclipse GIT本地库分支操作
git分支是一个重要的知识点,平时我们开发主要结合eclipse,idea来操作,今天这贴主要以eclipse来操作git本地库分支,主要内容包括新建分支,切换分支,合并分支,冲突解决,重命名分支,删 ...
- pyhton 3.6 pip 出现 Fatal error in launcher: Unable to create process using 解决方法
ERROR:Fatal error in launcher: Unable to create process using '"' 出现这个 打开 终端 输入 python36 -m ...
- [转]十分钟带你理解Kubernetes核心概念
本文将会简单介绍 Kubernetes的核心概念.因为这些定义可以在Kubernetes的文档中找到,所以文章也会避免用大段的枯燥的文字介绍.相反,我们会使用一些图表(其中一些是动画)和示例来解释这些 ...
- Vue脚手架创建项目出现 (Failed to download repo vuejs-templates/webpack: Response code 404)
搭建好(脚手架2.X版本)环境像往常一样使用vue init webpack xxxx 创建项目可以是没多久就开始报错了 报错结果就是:vue-cli · Failed to download rep ...