LeetCode Student Attendance Record I
原题链接在这里: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:
- '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.
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的更多相关文章
- [LeetCode] Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- [LeetCode] Student Attendance Record I 学生出勤记录之一
You are given a string representing an attendance record for a student. The record only contains the ...
- [LeetCode] 552. Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 【leetcode】552. Student Attendance Record II
题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 552. Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 551. Student Attendance Record I 从字符串判断学生考勤
[抄题]: You are given a string representing an attendance record for a student. The record only contai ...
- 551. Student Attendance Record I【easy】
551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...
- 【leetcode_easy】551. Student Attendance Record I
problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...
随机推荐
- 如何成为专业的PHP开发者
如何才能成为一名专业的PHP开发者?资深Web开发者Bruno Skvorc在其博客上分享了一些心得. 当阅读各种和PHP相关的博客.Quora问题.Google+社区.资讯和杂志的时候,Bruno ...
- loadrunder之脚本篇——Run-time Settings之Pacing
As soon as the previous iteration ends 前一个迭代一结束就尽可能快的开始新一轮的迭代 After the previous iteration ends ...
- Mybatis一对多/多对多查询时只查出了一条数据
问题描述: 如果三表(包括了关系表)级联查询,主表和明细表的主键都是id的话,明细表的多条数据只能查询出来第一条/最后一条数据. 三个表,权限表(Permission),权限组表(Permission ...
- mysql服务器3306端口不能远程连接的解决
1.网络检测 1)ping主机可以: 2)telnet 主机3306端口不可以: telnet 主机22端口可以: 说明与本机网络没有关系: 2.端口检测 1)netstat ...
- java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction问题 1.问题描述 执行了几条update语句 ...
- 【转】Android BitmapShader 实战 实现圆形、圆角图片
转载自:http://blog.csdn.net/lmj623565791/article/details/41967509 1.概述 记得初学那会写过一篇博客Android 完美实现图片圆角和圆形( ...
- 关于nginx性能优化及基本概念
参考文章: Nginx面试中最常见的18道题:http://blog.csdn.net/liyanlei5858/article/details/77924420 Nginx性能优化指南:http:/ ...
- iso不支持document事件
ios safari游览器除了a.input.button等不支持document事件委托?<body>加上这个样式即可 <style> .clickable-div { cu ...
- Servlet源码级别进行详解
1.首先我们先看看Servlet的类结构图,然后再分别介绍其中的接口方法 由上图可以看到,Servlet和ServletConfig都是顶层接口类,而GenericServlet实现了这两个顶层类,然 ...
- msdn - Developer Library(包括wpf)重要程度——5星*****
https://msdn.microsoft.com/zh-cn/library/ms754242(v=vs.110).aspx https://msdn.microsoft.com/zh-cn/li ...