Student Attendance Record I

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

题意: 给定一个字符串s,若其中的'A'大于1个,或者出现连续的3个或3个以上'L',返回False,否则返回True

例子: 

Example 1:
Input: "PPALLP"
Output: True

Example 2:
Input: "PPALLL"
Output: False

标签: attendance、student、record、ppallp、ppalll、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • SLPH
    2017-08-12 17:16:42 1楼#1层
    Python:
    class Solution(object):
        def checkRecord(self, s):
            """
            :type s: str
            :rtype: bool
            """
            return s.count('A') <=1 and 'LLL' not in s
  • SLPH
    2017-08-12 17:17:09 2楼#1层
    C++:
    class Solution {  
    public:  
        bool checkRecord(string s) {  
            int abnum=0;  
            int latenum=0;  
            int lastnum=0;  
            for (int i=0; i<s.size(); i++)  
            {  
                if (s[i]=='A')  
                {  
                     abnum++;  
                     lastnum=max(lastnum,latenum);  
                     latenum=0;  
                }  
                else if (s[i]=='L')  
                {  
                     latenum++;  
                }  
                else  
                {  
                    lastnum=max(lastnum,latenum);  
                    latenum=0;  
                }  
                
            }  
              
            lastnum=max(lastnum,latenum);  
            if (abnum>1||lastnum>2)  
            return false;  
            return true;  
              
        }  
    };
  • SLPH
    2017-08-12 17:17:40 3楼#1层
    Java:
    public class Solution {  
        public boolean checkRecord(String s) {  
            int a = 0;  
            int l = 0;  
            for (int i = 0; i < s.length(); ++i) {  
                char c = s.charAt(i);  
                if (c == 'P') {  
                    l = 0;  
                    continue;  
                } else if (c == 'A') {  
                    l = 0;  
                    a++;  
                    if (a > 1)  
                        return false;  
                } else {  
                    l++;  
                    if (l > 2)  
                        return false;  
                }  
            }  
            return true;  
        }  
    }
  • 回复
隐藏