public class Solution
    {
        public bool CheckRecord(string s)
        {
            var Absent = 0;
            var MaxLate = 0;
            var ContiLate = 0;
            var preChar = '\0';
            for (int i = 0; i < s.Length; i++)
            {
                var c = s[i];
                if (c == 'A')
                {
                    Absent++;
                    preChar = c;
                    if (MaxLate < ContiLate)
                    {
                        MaxLate = ContiLate;
                    }
                    ContiLate = 0;
                }
                else if (c == 'L')
                {
                    if (ContiLate == 0)
                    {
                        ContiLate = 1;
                        preChar = c;
                    }
                    else
                    {
                        if (preChar == c)
                        {
                            ContiLate++;
                            preChar = c;
                        }
                        else
                        {
                            ContiLate = 0;
                            preChar = c;
                        }
                    }
                }
                else
                {
                    if (MaxLate < ContiLate)
                    {
                        MaxLate = ContiLate;
                    }
                    ContiLate = 0;
                }
            }

            if (MaxLate < ContiLate)
            {
                MaxLate = ContiLate;
            }

            if (Absent > 1 || MaxLate > 2)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

https://leetcode.com/problems/student-attendance-record-i/#/description

posted on 2017-04-19 11:30  Sempron2800+  阅读(165)  评论(0编辑  收藏  举报