leetcode[Student Attendance Record I]//待整理多种解法

来源:互联网 发布:centos 6.2 编辑:程序博客网 时间:2024/04/29 12:13

解法一:

public class Solution {//A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent)//or more than two [continuous] 'L' (late). 对L的要求是[连续]超过2次    public boolean checkRecord(String s) {        int countA = 0;        int countL = 0;//这个代表最大的countL        int curCountL = 0;//这个代表当前连续的curCountL        for(int i = 0; i < s.length(); i++){        if(s.charAt(i) == 'A'){        countA++;        }        if(s.charAt(i) == 'L'){        curCountL++;        countL = Math.max(countL, curCountL);        } else{        curCountL = 0;        }                //System.out.println("countA:" + countA + "  countL:" + countL + "  curCountL:" + curCountL);        }                if(countA > 1 || countL > 2){        return false;        } else{        return true;        }    }}


原创粉丝点击