Skip to content

Commit b1815b6

Browse files
committed
feat: add solutions to lc problem: No.0551
No.0551.Student Attendance Record I
1 parent 0385312 commit b1815b6

File tree

6 files changed

+78
-58
lines changed

6 files changed

+78
-58
lines changed

solution/0500-0599/0551.Student Attendance Record I/README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:字符串遍历**
58+
59+
我们可以遍历字符串 $s$,记录字符 `'A'` 和字符串 `"LLL"` 的出现次数。如果字符 `'A'` 的出现次数小于 $2$,且字符串 `"LLL"` 没有出现过,则可以将该字符串视作记录合法,返回 `true`,否则返回 `false`
60+
61+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
@@ -63,7 +69,7 @@
6369
```python
6470
class Solution:
6571
def checkRecord(self, s: str) -> bool:
66-
return s.count('A') <= 1 and 'LLL' not in s
72+
return s.count('A') < 2 and 'LLL' not in s
6773
```
6874

6975
### **Java**
@@ -73,20 +79,11 @@ class Solution:
7379
```java
7480
class Solution {
7581
public boolean checkRecord(String s) {
76-
int i = s.indexOf("A");
77-
return (i == -1 || s.lastIndexOf("A") == i) && !s.contains("LLL");
82+
return s.indexOf("A") == s.lastIndexOf("A") && !s.contains("LLL");
7883
}
7984
}
8085
```
8186

82-
### **Go**
83-
84-
```go
85-
func checkRecord(s string) bool {
86-
return strings.Count(s, "A") < 2 && !strings.Contains(s, "LLL")
87-
}
88-
```
89-
9087
### **C++**
9188

9289
```cpp
@@ -98,6 +95,22 @@ public:
9895
};
9996
```
10097
98+
### **Go**
99+
100+
```go
101+
func checkRecord(s string) bool {
102+
return strings.Count(s, "A") < 2 && !strings.Contains(s, "LLL")
103+
}
104+
```
105+
106+
### **TypeScript**
107+
108+
```ts
109+
function checkRecord(s: string): boolean {
110+
return s.indexOf('A') === s.lastIndexOf('A') && s.indexOf('LLL') === -1;
111+
}
112+
```
113+
101114
### **...**
102115

103116
```

solution/0500-0599/0551.Student Attendance Record I/README_EN.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,19 @@
5555
```python
5656
class Solution:
5757
def checkRecord(self, s: str) -> bool:
58-
return s.count('A') <= 1 and 'LLL' not in s
58+
return s.count('A') < 2 and 'LLL' not in s
5959
```
6060

6161
### **Java**
6262

6363
```java
6464
class Solution {
6565
public boolean checkRecord(String s) {
66-
int i = s.indexOf("A");
67-
return (i == -1 || s.lastIndexOf("A") == i) && !s.contains("LLL");
66+
return s.indexOf("A") == s.lastIndexOf("A") && !s.contains("LLL");
6867
}
6968
}
7069
```
7170

72-
### **Go**
73-
74-
```go
75-
func checkRecord(s string) bool {
76-
return strings.Count(s, "A") < 2 && !strings.Contains(s, "LLL")
77-
}
78-
```
79-
8071
### **C++**
8172

8273
```cpp
@@ -88,6 +79,22 @@ public:
8879
};
8980
```
9081
82+
### **Go**
83+
84+
```go
85+
func checkRecord(s string) bool {
86+
return strings.Count(s, "A") < 2 && !strings.Contains(s, "LLL")
87+
}
88+
```
89+
90+
### **TypeScript**
91+
92+
```ts
93+
function checkRecord(s: string): boolean {
94+
return s.indexOf('A') === s.lastIndexOf('A') && s.indexOf('LLL') === -1;
95+
}
96+
```
97+
9198
### **...**
9299

93100
```
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class Solution {
22
public boolean checkRecord(String s) {
3-
int i = s.indexOf("A");
4-
return (i == -1 || s.lastIndexOf("A") == i) && !s.contains("LLL");
3+
return s.indexOf("A") == s.lastIndexOf("A") && !s.contains("LLL");
54
}
65
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def checkRecord(self, s: str) -> bool:
3-
return s.count('A') <= 1 and 'LLL' not in s
3+
return s.count('A') < 2 and 'LLL' not in s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function checkRecord(s: string): boolean {
2+
return s.indexOf('A') === s.lastIndexOf('A') && s.indexOf('LLL') === -1;
3+
}

solution/main.py

+31-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import time
32
from datetime import timezone, timedelta, datetime
43

@@ -60,18 +59,18 @@ def get_question_detail(self, question_title_slug: str, retry: int = 3) -> dict:
6059
form1 = {
6160
'operationName': 'globalData',
6261
'query': 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n '
63-
'discuss\n mockInterview\n contest\n store\n book\n chinaProblemDiscuss\n '
64-
'socialProviders\n studentFooter\n cnJobs\n enableLsp\n enableWs\n '
65-
'enableDebugger\n enableDebuggerAdmin\n enableDarkMode\n tasks\n '
66-
'leetbook\n __typename\n }\n userStatus {\n isSignedIn\n isAdmin\n '
67-
'isStaff\n isSuperuser\n isTranslator\n isPremium\n isVerified\n '
68-
'isPhoneVerified\n isWechatVerified\n checkedInToday\n username\n '
69-
'realName\n userSlug\n groups\n avatar\n optedIn\n '
70-
'requestRegion\n region\n activeSessionId\n permissions\n notificationStatus {\n '
71-
'lastModified\n numUnread\n __typename\n }\n completedFeatureGuides\n '
72-
'useTranslation\n accountStatus {\n isFrozen\n inactiveAfter\n __typename\n '
73-
'}\n __typename\n }\n siteRegion\n chinaHost\n websocketUrl\n userBannedInfo {\n '
74-
'bannedData {\n endAt\n bannedType\n __typename\n }\n __typename\n }\n}\n',
62+
'discuss\n mockInterview\n contest\n store\n book\n chinaProblemDiscuss\n '
63+
'socialProviders\n studentFooter\n cnJobs\n enableLsp\n enableWs\n '
64+
'enableDebugger\n enableDebuggerAdmin\n enableDarkMode\n tasks\n '
65+
'leetbook\n __typename\n }\n userStatus {\n isSignedIn\n isAdmin\n '
66+
'isStaff\n isSuperuser\n isTranslator\n isPremium\n isVerified\n '
67+
'isPhoneVerified\n isWechatVerified\n checkedInToday\n username\n '
68+
'realName\n userSlug\n groups\n avatar\n optedIn\n '
69+
'requestRegion\n region\n activeSessionId\n permissions\n notificationStatus {\n '
70+
'lastModified\n numUnread\n __typename\n }\n completedFeatureGuides\n '
71+
'useTranslation\n accountStatus {\n isFrozen\n inactiveAfter\n __typename\n '
72+
'}\n __typename\n }\n siteRegion\n chinaHost\n websocketUrl\n userBannedInfo {\n '
73+
'bannedData {\n endAt\n bannedType\n __typename\n }\n __typename\n }\n}\n',
7574
'variables': {},
7675
}
7776
headers = {
@@ -86,20 +85,20 @@ def get_question_detail(self, question_title_slug: str, retry: int = 3) -> dict:
8685
'operationName': 'questionData',
8786
'variables': {'titleSlug': question_title_slug},
8887
'query': 'query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n '
89-
'questionId\n questionFrontendId\n categoryTitle\n boundTopicId\n title\n '
90-
'titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n '
91-
'difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n '
92-
'contributors {\n username\n profileUrl\n avatarUrl\n __typename\n '
93-
'}\n langToValidPlayground\n topicTags {\n name\n slug\n '
94-
'translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n '
95-
'lang\n langSlug\n code\n __typename\n }\n stats\n hints\n '
96-
'solution {\n id\n canSeeDetail\n __typename\n }\n status\n '
97-
'sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n '
98-
'enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n '
99-
'source\n shortDescription\n fullDescription\n bookImgUrl\n '
100-
'pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n '
101-
'isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n '
102-
'exampleTestcases\n __typename\n }\n}\n',
88+
'questionId\n questionFrontendId\n categoryTitle\n boundTopicId\n title\n '
89+
'titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n '
90+
'difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n '
91+
'contributors {\n username\n profileUrl\n avatarUrl\n __typename\n '
92+
'}\n langToValidPlayground\n topicTags {\n name\n slug\n '
93+
'translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n '
94+
'lang\n langSlug\n code\n __typename\n }\n stats\n hints\n '
95+
'solution {\n id\n canSeeDetail\n __typename\n }\n status\n '
96+
'sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n '
97+
'enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n '
98+
'source\n shortDescription\n fullDescription\n bookImgUrl\n '
99+
'pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n '
100+
'isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n '
101+
'exampleTestcases\n __typename\n }\n}\n',
103102
}
104103

105104
try:
@@ -157,17 +156,17 @@ def format_question_detail(question_detail: dict) -> dict:
157156
'relative_path_cn': path_cn,
158157
'relative_path_en': path_en,
159158
'title_cn': question_detail.get('translatedTitle')
160-
or question_title_en
161-
or '',
159+
or question_title_en
160+
or '',
162161
'title_en': question_title_en or '',
163162
'question_title_slug': question_title_slug,
164163
'content_en': question_detail.get('content'),
165164
'content_cn': question_detail.get('translatedContent')
166-
or question_detail.get('content')
167-
or '',
165+
or question_detail.get('content')
166+
or '',
168167
'tags_en': [e['name'] for e in topic_tags if e['name']] or [],
169168
'tags_cn': [e['translatedName'] for e in topic_tags if e['translatedName']]
170-
or [],
169+
or [],
171170
'difficulty_en': question_detail.get('difficulty'),
172171
'difficulty_cn': difficulty.get(question_detail.get('difficulty')),
173172
'code_snippets': question_detail.get('codeSnippets') or [],
@@ -316,7 +315,6 @@ def get_contests(fetch_new=True) -> List:
316315
time.sleep(0.3)
317316
question_details[slug] = Spider.format_question_detail(detail)
318317

319-
320318
# 周赛场次列表
321319
contest_list = get_contests(refresh_all)
322320
cls = []

0 commit comments

Comments
 (0)