551. Student Attendance Record I【easy】

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

解法一:

 class Solution {
public:
bool checkRecord(string s) {
int num_a = ; for (int i = ; i < s.length(); ++i) {
if (s[i] == 'A') {
if (++num_a > ) {
return false;
}
} if (i > && i < s.length() -
&& s[i] == 'L' && s[i - ] == 'L' && s[i + ] == 'L') {
return false;
}
} return true;
}
};

解法二:

 public boolean checkRecord(String s) {
int countA=;
int continuosL = ;
int charA = 'A';
int charL ='L';
for(int i=;i<s.length();i++){
if(s.charAt(i) == charA){
countA++;
continuosL = ;
}
else if(s.charAt(i) == charL){
continuosL++;
}
else{
continuosL = ;
}
if(countA > || continuosL > ){
return false;
}
}
return true; }

参考@rakeshuky 的代码。

解法三:

 public boolean checkRecord(String s) {
int acount=;
int lcount=;
for(char c : s.toCharArray()) {
if(c=='L') {
lcount++;
}
else {
lcount=;
if(c=='A') {
acount++;
}
}
if(acount> || lcount>=) {
return false;
}
}
return true;
}

参考@labs 的代码。

551. Student Attendance Record I【easy】的更多相关文章

  1. 【leetcode_easy】551. Student Attendance Record I

    problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...

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

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

  3. 【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则表达式 统计 日期 题目地址:https://l ...

  4. 551. Student Attendance Record I + Student Attendance Record II

    ▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...

  5. [LeetCode&Python] Problem 551. Student Attendance Record I

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

  6. LeetCode 551. Student Attendance Record I (C++)

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

  7. LeetCode 551. Student Attendance Record I (学生出勤纪录 I)

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

  8. 551. Student Attendance Record I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 551 Student Attendance Record I 学生出勤纪录 I

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

随机推荐

  1. 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...

  2. 【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III

    基本等同这个,只是询问的东西不大一样而已. http://www.cnblogs.com/autsky-jadek/p/4159897.html #include<cstdio> #inc ...

  3. 浅谈Uber与滴滴快的提供差异化服务带来的商业模式思考

    一.引言 滴滴和快的烧钱的时代已经过去,在那个时代我们消费者着实得到了不少实惠.自从他们温柔的在一起之后,这种实惠就木有了.让我不禁感叹坐车的几率有降低了50%.前段时间,Uber悄无声息的进入我的视 ...

  4. C# 6.0语法新特性体验(二)

    之前我在文章通过Roslyn体验C# 6.0的新语法中介绍了一些C# 6.0的语法特性,现在随着Visual Studio 14 CTP3的发布,又陆续可以体验一些新的特性了,这里简单的介绍一下之前没 ...

  5. ClipboardJS复制粘贴插件的使用

    1.简单的纯JS复制粘贴(兼容性差,只能用textarea标签) var btn=document.getElementsByClassName("btn")[0]; //复制按钮 ...

  6. [Java基础] java多线程关于消费者和生产者

    多线程: 生产与消费 1.生产者Producer生产produce产品,并将产品放到库存inventory里:同时消费者Consumer从库存inventory里消费consume产品. 2.库存in ...

  7. [Git] Git 的origin和master分析

    转载: http://lishicongli.blog.163.com/blog/static/1468259020132125247302/ 首先要明确一点,对git的操作是围绕3个大的步骤来展开的 ...

  8. ubuntu16.04 登录密码破解方法

    1:开机按Shift键,出现如下界面.(手速要快,Shift键要按时间久一点) 选择第二项 2:按回车键进入如下界面,然后选中有recovery mode的选项(第三项) 3:按e进入如下界面,并找到 ...

  9. CentOS下vi编辑器

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  10. 用聚合数据API(苏州实时公交API)快速写出小程序

    利用聚合数据API快速写出小程序,过程简单. 1.申请小程序账号 2.进入开发 3.调用API.比如“苏州实时公交”小程序,选择的是苏州实时公交API. 苏州实时公交API文档:https://www ...