Skip to content

Commit d323471

Browse files
committed
Update solution 0058、0551、0958
1 parent 71b771d commit d323471

File tree

12 files changed

+326
-136
lines changed

12 files changed

+326
-136
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@
365365
|0224|Basic Calculator|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0224.Basic-Calculator)|39.3%|Hard||
366366
|0225|Implement Stack using Queues|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0225.Implement-Stack-using-Queues)|49.8%|Easy||
367367
|0226|Invert Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0226.Invert-Binary-Tree)|69.0%|Easy||
368-
|0227|Basic Calculator II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0227.Basic-Calculator-II)|39.7%|Medium||
368+
|0227|Basic Calculator II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0227.Basic-Calculator-II)|39.6%|Medium||
369369
|0228|Summary Ranges|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0228.Summary-Ranges)|43.8%|Easy||
370370
|0229|Majority Element II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0229.Majority-Element-II)|40.3%|Medium||
371371
|0230|Kth Smallest Element in a BST|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0230.Kth-Smallest-Element-in-a-BST)|64.6%|Medium||
@@ -644,7 +644,7 @@
644644
|0503|Next Greater Element II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0503.Next-Greater-Element-II)|60.2%|Medium||
645645
|0504|Base 7||46.9%|Easy||
646646
|0505|The Maze II||50.1%|Medium||
647-
|0506|Relative Ranks||53.6%|Easy||
647+
|0506|Relative Ranks||53.7%|Easy||
648648
|0507|Perfect Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0507.Perfect-Number)|37.0%|Easy||
649649
|0508|Most Frequent Subtree Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0508.Most-Frequent-Subtree-Sum)|60.8%|Medium||
650650
|0509|Fibonacci Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0509.Fibonacci-Number)|67.8%|Easy||
@@ -1758,7 +1758,7 @@
17581758
|1617|Count Subtrees With Max Distance Between Cities||64.2%|Hard||
17591759
|1618|Maximum Font to Fit a Sentence in a Screen||57.5%|Medium||
17601760
|1619|Mean of Array After Removing Some Elements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1619.Mean-of-Array-After-Removing-Some-Elements)|64.6%|Easy||
1761-
|1620|Coordinate With Maximum Network Quality||36.8%|Medium||
1761+
|1620|Coordinate With Maximum Network Quality||36.7%|Medium||
17621762
|1621|Number of Sets of K Non-Overlapping Line Segments||42.4%|Medium||
17631763
|1622|Fancy Sequence||15.1%|Hard||
17641764
|1623|All Valid Triplets That Can Represent a Country||88.8%|Easy||
@@ -1831,7 +1831,7 @@
18311831
|1690|Stone Game VII|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1690.Stone-Game-VII)|58.8%|Medium||
18321832
|1691|Maximum Height by Stacking Cuboids|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1691.Maximum-Height-by-Stacking-Cuboids)|51.4%|Hard||
18331833
|1692|Count Ways to Distribute Candies||60.1%|Hard||
1834-
|1693|Daily Leads and Partners||91.2%|Easy||
1834+
|1693|Daily Leads and Partners||91.1%|Easy||
18351835
|1694|Reformat Phone Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1694.Reformat-Phone-Number)|65.0%|Easy||
18361836
|1695|Maximum Erasure Value|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1695.Maximum-Erasure-Value)|52.3%|Medium||
18371837
|1696|Jump Game VI|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1696.Jump-Game-VI)|42.1%|Medium||
@@ -1989,7 +1989,7 @@
19891989
|1848|Minimum Distance to the Target Element||60.2%|Easy||
19901990
|1849|Splitting a String Into Descending Consecutive Values||28.4%|Medium||
19911991
|1850|Minimum Adjacent Swaps to Reach the Kth Smallest Number||73.6%|Medium||
1992-
|1851|Minimum Interval to Include Each Query||44.1%|Hard||
1992+
|1851|Minimum Interval to Include Each Query||44.2%|Hard||
19931993
|1852|Distinct Numbers in Each Subarray||74.5%|Medium||
19941994
|1853|Convert Date Format||88.9%|Easy||
19951995
|1854|Maximum Population Year||57.9%|Easy||
@@ -2012,7 +2012,7 @@
20122012
|1871|Jump Game VII||24.3%|Medium||
20132013
|1872|Stone Game VIII||51.5%|Hard||
20142014
|1873|Calculate Special Bonus||91.5%|Easy||
2015-
|1874|Minimize Product Sum of Two Arrays||90.0%|Medium||
2015+
|1874|Minimize Product Sum of Two Arrays||89.9%|Medium||
20162016
|1875|Group Employees of the Same Salary||75.7%|Medium||
20172017
|1876|Substrings of Size Three with Distinct Characters||68.9%|Easy||
20182018
|1877|Minimize Maximum Pair Sum in Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1877.Minimize-Maximum-Pair-Sum-in-Array)|79.4%|Medium||
@@ -2072,7 +2072,7 @@
20722072
|1931|Painting a Grid With Three Different Colors||55.9%|Hard||
20732073
|1932|Merge BSTs to Create Single BST||33.0%|Hard||
20742074
|1933|Check if String Is Decomposable Into Value-Equal Substrings||56.1%|Easy||
2075-
|1934|Confirmation Rate||79.5%|Medium||
2075+
|1934|Confirmation Rate||79.6%|Medium||
20762076
|1935|Maximum Number of Words You Can Type||72.5%|Easy||
20772077
|1936|Add Minimum Number of Rungs||41.4%|Medium||
20782078
|1937|Maximum Number of Points with Cost||28.6%|Medium||
@@ -2081,7 +2081,7 @@
20812081
|1940|Longest Common Subsequence Between Sorted Arrays||81.1%|Medium||
20822082
|1941|Check if All Characters Have Equal Number of Occurrences||77.0%|Easy||
20832083
|1942|The Number of the Smallest Unoccupied Chair||36.8%|Medium||
2084-
|1943|Describe the Painting||44.9%|Medium||
2084+
|1943|Describe the Painting||45.0%|Medium||
20852085
|1944|Number of Visible People in a Queue||62.6%|Hard||
20862086
|1945|Sum of Digits of String After Convert||62.0%|Easy||
20872087
|1946|Largest Number After Mutating Substring||33.1%|Medium||
@@ -2115,7 +2115,7 @@
21152115
|1974|Minimum Time to Type Word Using Special Typewriter||72.9%|Easy||
21162116
|1975|Maximum Matrix Sum||42.9%|Medium||
21172117
|1976|Number of Ways to Arrive at Destination||30.8%|Medium||
2118-
|1977|Number of Ways to Separate Numbers||23.4%|Hard||
2118+
|1977|Number of Ways to Separate Numbers||23.3%|Hard||
21192119
|1978|Employees Whose Manager Left the Company||52.9%|Easy||
21202120
|1979|Find Greatest Common Divisor of Array||80.8%|Easy||
21212121
|1980|Find Unique Binary String||61.2%|Medium||
@@ -2125,7 +2125,7 @@
21252125
|1984|Minimum Difference Between Highest and Lowest of K Scores||54.1%|Easy||
21262126
|1985|Find the Kth Largest Integer in the Array||43.1%|Medium||
21272127
|1986|Minimum Number of Work Sessions to Finish the Tasks||30.7%|Medium||
2128-
|1987|Number of Unique Good Subsequences||49.4%|Hard||
2128+
|1987|Number of Unique Good Subsequences||49.3%|Hard||
21292129
|1988|Find Cutoff Score for Each School||76.0%|Medium||
21302130
|1989|Maximum Number of People That Can Be Caught in Tag||61.1%|Medium||
21312131
|1990|Count the Number of Experiments||58.5%|Medium||
@@ -2136,7 +2136,7 @@
21362136
|1995|Count Special Quadruplets||55.1%|Easy||
21372137
|1996|The Number of Weak Characters in the Game||27.4%|Medium||
21382138
|1997|First Day Where You Have Been in All the Rooms||33.7%|Medium||
2139-
|1998|GCD Sort of an Array||45.3%|Hard||
2139+
|1998|GCD Sort of an Array||45.2%|Hard||
21402140
|1999|Smallest Greater Multiple Made of Two Digits||57.3%|Medium||
21412141
|2000|Reverse Prefix of Word||80.3%|Easy||
21422142
|2001|Number of Pairs of Interchangeable Rectangles||40.1%|Medium||
Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,70 @@
1-
# [58. Length of Last Word](https://leetcode-cn.com/problems/length-of-last-word/)
1+
# [58. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
2+
23

34
## 题目
45

5-
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.
6+
Given a string `s` consisting of some words separated by some number of spaces, return *the length of the **last** word in the string.*
67

7-
A word is a maximal substring consisting of non-space characters only.
8+
A **word** is a maximal substring consisting of non-space characters only.
89

9-
Example 1:
10+
**Example 1:**
1011

1112
```
1213
Input: s = "Hello World"
1314
Output: 5
1415
Explanation: The last word is "World" with length 5.
16+
1517
```
1618

17-
Example 2:
19+
**Example 2:**
1820

1921
```
2022
Input: s = " fly me to the moon "
2123
Output: 4
2224
Explanation: The last word is "moon" with length 4.
25+
2326
```
2427

25-
Example 3:
28+
**Example 3:**
2629

2730
```
2831
Input: s = "luffy is still joyboy"
2932
Output: 6
3033
Explanation: The last word is "joyboy" with length 6.
34+
3135
```
3236

33-
## 题目大意
37+
**Constraints:**
3438

35-
给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。
39+
- `1 <= s.length <= 104`
40+
- `s` consists of only English letters and spaces `' '`.
41+
- There will be at least one word in `s`.
3642

37-
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
43+
## 题目大意
44+
45+
给你一个字符串 `s`,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。**单词** 是指仅由字母组成、不包含任何空格字符的最大子字符串。
3846

3947
## 解题思路
4048

41-
- 先从后过滤掉空格找到单词尾部,再从尾部向前遍历,找到单词头部,最后两者相减,即为单词的长度
49+
- 先从后过滤掉空格找到单词尾部,再从尾部向前遍历,找到单词头部,最后两者相减,即为单词的长度。
50+
51+
## 代码
52+
53+
```go
54+
package leetcode
55+
56+
func lengthOfLastWord(s string) int {
57+
last := len(s) - 1
58+
for last >= 0 && s[last] == ' ' {
59+
last--
60+
}
61+
if last < 0 {
62+
return 0
63+
}
64+
first := last
65+
for first >= 0 && s[first] != ' ' {
66+
first--
67+
}
68+
return last - first
69+
}
70+
```
Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
# [551. Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/)
1+
# [551. Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/)
22

33
## 题目
44

5-
You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
5+
You are given a string `s` representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
66

7-
- 'A': Absent.
8-
- 'L': Late.
9-
- 'P': Present.
7+
- `'A'`: Absent.
8+
- `'L'`: Late.
9+
- `'P'`: Present.
1010

11-
The student is eligible for an attendance award if they meet both of the following criteria:
11+
The student is eligible for an attendance award if they meet **both** of the following criteria:
1212

13-
The student was absent ('A') for strictly fewer than 2 days total.
13+
- The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
14+
- The student was **never** late (`'L'`) for 3 or more **consecutive** days.
1415

15-
The student was never late ('L') for 3 or more consecutive days.
16-
17-
Return true if the student is eligible for an attendance award, or false otherwise.
16+
Return `true` *if the student is eligible for an attendance award, or* `false` *otherwise*.
1817

1918
**Example 1:**
2019

21-
Input: s = "PPALLP"
22-
Output: true
23-
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
20+
```
21+
Input: s = "PPALLP"
22+
Output: true
23+
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
24+
25+
```
2426

2527
**Example 2:**
2628

27-
Input: s = "PPALLL"
28-
Output: false
29-
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
29+
```
30+
Input: s = "PPALLL"
31+
Output: false
32+
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
33+
34+
```
3035

3136
**Constraints:**
3237

33-
- 1 <= s.length <= 1000
34-
- s[i] is either 'A', 'L', or 'P'.
38+
- `1 <= s.length <= 1000`
39+
- `s[i]` is either `'A'``'L'`, or `'P'`.
3540

3641
## 题目大意
3742

@@ -43,13 +48,39 @@ Return true if the student is eligible for an attendance award, or false otherwi
4348

4449
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
4550

46-
按 总出勤 计,学生缺勤('A')严格 少于两天。
47-
48-
学生 不会 存在 连续 3 天或 3 天以上的迟到('L')记录。
51+
- 按 总出勤 计,学生缺勤('A')严格 少于两天。
52+
- 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到('L')记录。
4953

5054
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
5155

5256
## 解题思路
5357

54-
- 遍历字符串s求出'A'的总数量和连续'L'的最大数量
55-
- 比较'A'的数量是否小于2并且'L'的连续最大数量是否小于3
58+
- 遍历字符串 s 求出 'A' 的总数量和连续 'L' 的最大数量。
59+
- 比较 'A' 的数量是否小于 2 并且 'L' 的连续最大数量是否小于 3。
60+
61+
## 代码
62+
63+
```go
64+
package leetcode
65+
66+
func checkRecord(s string) bool {
67+
numsA, maxL, numsL := 0, 0, 0
68+
for _, v := range s {
69+
if v == 'L' {
70+
numsL++
71+
} else {
72+
if numsL > maxL {
73+
maxL = numsL
74+
}
75+
numsL = 0
76+
if v == 'A' {
77+
numsA++
78+
}
79+
}
80+
}
81+
if numsL > maxL {
82+
maxL = numsL
83+
}
84+
return numsA < 2 && maxL < 3
85+
}
86+
```

leetcode/0958.Check-Completeness-of-a-Binary-Tree/0958.Check Completeness of a Binary Tree.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ type TreeNode = structures.TreeNode
1717
*/
1818

1919
func isCompleteTree(root *TreeNode) bool {
20-
queue := []*TreeNode{root}
21-
found := false
22-
20+
queue, found := []*TreeNode{root}, false
2321
for len(queue) > 0 {
2422
node := queue[0] //取出每一层的第一个节点
2523
queue = queue[1:]
@@ -34,4 +32,4 @@ func isCompleteTree(root *TreeNode) bool {
3432
}
3533
}
3634
return true
37-
}
35+
}

0 commit comments

Comments
 (0)