原题链接在这里:https://leetcode.com/problems/student-attendance-record-i/description/

题目:

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

题解:

计算'A'的个数和连续'L'的长度.

Time Complexity: O(s.length()). Space: O(1).

AC Java:

 class Solution {
public boolean checkRecord(String s) {
int countA = 0;
int lengthL = 0;
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(c == 'A'){
countA++;
lengthL = 0;
}else if(c == 'L'){
lengthL++;
}else{
lengthL = 0;
} if(countA>1 || lengthL>2){
return false;
}
}
return true;
}
}

LeetCode Student Attendance Record I的更多相关文章

  1. [LeetCode] Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  2. [LeetCode] Student Attendance Record I 学生出勤记录之一

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

  3. [LeetCode] 552. Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  4. 【leetcode】552. Student Attendance Record II

    题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...

  5. [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  6. 552. Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

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

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

  8. 551. Student Attendance Record I【easy】

    551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...

  9. 【leetcode_easy】551. Student Attendance Record I

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

随机推荐

  1. docker 命令添加容器数据卷

    实现宿主机和容器的数据共享 只要建立连接,即使容器exit,主机的修改仍能提现到容器

  2. focus + select

    focus使光标定位到目标节点之后 select选中光标所在位置的全部内容

  3. mybatis中collection和association的作用以及用法

    deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...

  4. Linux Shell基础 单引号、双引号、反引号、小括号和大括号

    单引号和双引号 单引号和双引号用于变量值出现空格时将字符用引号括起来. 二者的主要区别在于, 被单引号括起来的字符都是普通字符,就算特殊字符也不再有特殊含义: 被双引号括起来的字符中,"$& ...

  5. [原创]spring及springmvc精简版--IOC

    本篇博客为自己学习spring和springmvc的一个总结.主要以代码为主,至于文字性描述理解性东西,可以自行百度.有认识不妥的地方,还望指出,相互学习. 以前很困惑spring中的一些概念,在学习 ...

  6. 获取CPU利用率

    #define MB (1024 * 1024) MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatu ...

  7. 关于图片上传与下载(Java)

    图片的上传 package com.upload; import java.io.IOException;import java.io.PrintWriter; import javax.servle ...

  8. HMM代码实现

    按照网上的代码,自己敲了一下,改了一点点,理解加深了一下. 还有训练HMM的EM算法没看懂,下次接着看: 参考连接:http://www.cnblogs.com/hanahimi/p/4011765. ...

  9. 域名注册中EAP期间是什么意思

    所谓域名申请期间的EAP指的是,域名优先注册期,行业上也称为“早期接入期”,这个期间的时间是由该域名所在的管理注册局定,而这个EPA期的时间长度也不一样,有的是一个星期,也有的长达两个星期. 域名EA ...

  10. FreeTDS-SQL Server在linux和unix下的免费驱动

    微软为MS SQL Server的连接和使用提供了很好的 驱动和 文档. 不幸的是,那只能在windows操作系统上使用. 所以对于Linux或者Unix,您需要寻找不同的方法来连接MS SQL Se ...