LeetCode-551. Student Attendance Record I (Java)

来源:互联网 发布:js中如何定义数组 编辑:程序博客网 时间:2024/05/16 10:36

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
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

代码

public class Solution {    public boolean checkRecord(String s) {       char[] array = s.toCharArray();        int countA = 0;        int countL =0;        for(int i  =0;i<array.length;i++){        if('A' == array[i]){        countA++;        if(countA >1){        return false;        }        }        if('L'== array[i]){        countL++;          if(countL >2){        return false;        }              }        else{        countL =0;        }        }        return true;    }}