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 ...
随机推荐
- iOS 关于 Missing iOS Distribution signing identity for.... 等 打包 校验 出现的事故 处理经验
着实郁闷了一阵子,不知道为什么 证书和配置文件都没有问题 在Archieve后 validate 提示:"Missing iOS Distribution signing identity ...
- 大话设计模式之PHP篇 - 简单工厂模式
假设有一道编程题:输入两个数字和运算符,然后得到运算结果.非常简单的一道题目,通常的实现代码如下: <?php Function Operation($val1, $val2, $operate ...
- 爬虫实例之使用requests和Beautifusoup爬取糗百热门用户信息
这次主要用requests库和Beautifusoup库来实现对糗百的热门帖子的用户信息的收集,由于糗百的反爬虫不是很严格,也不需要先登录才能获取数据,所以较简单. 思路,先请求首页的热门帖子获得用户 ...
- numpy模块之创建矩阵、矩阵运算
本文参考给妹子讲python https://zhuanlan.zhihu.com/p/34673397 NumPy是Numerical Python的简写,是高性能科学计算和数据分析的基础包,他是 ...
- Go goroutine (协程)
在Go语言中goroutine是一个协程,但是跟Python里面的协程有很大的不同: 在任何函数前只需要加上go关键字就可以定义为协程; 不需要在定义时区分是否是异步函数 VS async def ...
- RSA签名 python PHP demo 例子
python RSA+MD5签名demo: #!/usr/bin/env python2.7 #coding:utf-8 import base64 from Crypto.PublicKey imp ...
- seajs 入门
最近想搞搞JS模块化, 读到了园子里的一篇好文: http://www.cnblogs.com/lvdabao/p/js-modules-develop.html 看里面讲seajs不错, 于是想学 ...
- Maven webapp index.jsp报错
javax.servlet javax.servlet-api 3.1.0
- juniper ssg 常用命令
netscreen juniper ssg操作命令 2013年4月10日 命令行下取得配置信息 get config 命令行下取得相应时间设置 get clock set vrout ...
- HTML5 画布canvas
SVG的<defs> <symbols> 元素用于预定义一个元素使其能够在SVG图像中重复使用 <svg xmlns="http://www.w3.org/20 ...