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. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  2. 【2-SAT】URAL - 2089 - Experienced coach

    题意:给出n对点a,b  要求从没对点中选出一个,且最终选出的点n个数不能存在相同的.输入数据满足每种数最多出现3次,最少出现1次 思路:第i对点的编号2*i, 2*i+1,   因为每个数最多出现3 ...

  3. 【计算几何】bzoj2338 [HNOI2011]数矩形

    对于两条线段,若其中点重合,且长度相等,那么它们一定是某个矩形的对角线. N*N地处理出所有线段,排序,对每一部分中点重合.长度相等的线段进行暴力枚举,更新答案. 用 long double 注意EP ...

  4. python3-开发进阶Django-debug-toolbar的配置和Django logging的配置

    阅读目录 django-debug-toolbar的配置 Django logging的配置 一.django-debug-toolbar的配置 1.介绍 django-debug-toolbar 是 ...

  5. 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代

    package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...

  6. iOS 常用的#define合集

    1.定义常量 定义常量的时候最好以小写字母k开头,让人见名知意, (1)导航栏高度:我们都知道iPhone竖屏时候导航栏的高度为44,这时候可以定义一个常量来表示该高度, #define kNaivg ...

  7. CSS3 animation动画

    CSS3 animation动画 1.@keyframes 定义关键帧动画2.animation-name 动画名称3.animation-duration 动画时间4.animation-timin ...

  8. Shell--命令执行的判断依据:;,&&,||

    :在命令与命令中间利用分号来隔开,这样一来,分号前得命令执行完后就会立刻接着执行后面的命令了 &&若第一个命令执行完毕并且正确执行也就是$?=0,则开始执行后一个命令,否则不执行 || ...

  9. Solr6.6.0 用 SimplePostTool与界面dataimport索引方式区别

    通过测试发现用SimplePostTool与solr界面dataimport索引数据的结果有如下区别: 1.SimplePostTool索引数据对结构化数据文件索引比较合适,比如csv/json/xm ...

  10. 查看进程id, 父进程id _How do I get the parent process ID of a given child process?

    How to get parent pid from a given children pid? I know I can mannully check it under /proc, I am wo ...