551. Student Attendance Record I【easy】

551. Student Attendance Record I【easy】

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

解法一:

 1 class Solution {
 2 public:
 3     bool checkRecord(string s) {
 4         int num_a = 0;
 5
 6         for (int i = 0; i < s.length(); ++i) {
 7             if (s[i] == ‘A‘) {
 8                 if (++num_a > 1) {
 9                     return false;
10                 }
11             }
12
13             if (i > 0 && i < s.length() - 1
14                 && s[i] == ‘L‘ && s[i - 1] == ‘L‘ && s[i + 1] == ‘L‘) {
15                 return false;
16             }
17         }
18
19         return true;
20     }
21 };

解法二:

 1 public boolean checkRecord(String s) {
 2         int countA=0;
 3         int continuosL = 0;
 4         int charA = ‘A‘;
 5         int charL =‘L‘;
 6         for(int i=0;i<s.length();i++){
 7             if(s.charAt(i) == charA){
 8                 countA++;
 9                 continuosL = 0;
10             }
11             else if(s.charAt(i) == charL){
12                 continuosL++;
13             }
14             else{
15                 continuosL = 0;
16             }
17             if(countA >1 || continuosL > 2 ){
18                 return false;
19             }
20         }
21         return true;
22
23     }

参考@rakeshuky 的代码。

解法三:

 1 public boolean checkRecord(String s) {
 2     int acount=0;
 3     int lcount=0;
 4     for(char c : s.toCharArray()) {
 5         if(c==‘L‘) {
 6             lcount++;
 7         }
 8         else {
 9             lcount=0;
10             if(c==‘A‘) {
11                 acount++;
12             }
13         }
14         if(acount>1 || lcount>=3) {
15             return false;
16         }
17     }
18     return true;
19    }

参考@labs 的代码。

时间: 2024-11-05 15:56:13

551. Student Attendance Record I【easy】的相关文章

【leetcode_easy】551. Student Attendance Record I

problem 551. Student Attendance Record I 参考 1. Leetcode_easy_551. Student Attendance Record I; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10947954.html

551. Student Attendance Record I(LeetCode)

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 o

551 Student Attendance Record I

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 o

LeetCode 551. Student Attendance Record I (C++)

题目: 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 th

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

661. Image Smoother【easy】

661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.

605. Can Place Flowers【easy】

605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represe

1. Two Sum【easy】

1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums