Update solution 0058、0551、0958

This commit is contained in:
halfrost
2021-09-15 03:51:40 -07:00
parent 71b771dc5d
commit d32347137a
12 changed files with 324 additions and 134 deletions

View File

@ -1,41 +1,70 @@
# [58. Length of Last Word](https://leetcode-cn.com/problems/length-of-last-word/)
# [58. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
## 题目
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.
Given a string `s` consisting of some words separated by some number of spaces, return *the length of the **last** word in the string.*
A word is a maximal substring consisting of non-space characters only.
A **word** is a maximal substring consisting of non-space characters only.
Example 1:
**Example 1:**
```
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
```
Example 2:
**Example 2:**
```
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
```
Example 3:
**Example 3:**
```
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
```
**Constraints:**
- `1 <= s.length <= 104`
- `s` consists of only English letters and spaces `' '`.
- There will be at least one word in `s`.
## 题目大意
给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
给你一个字符串 `s`,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。**单词** 是指仅由字母组成、不包含任何空格字符的最大子字符串。
## 解题思路
- 先从后过滤掉空格找到单词尾部,再从尾部向前遍历,找到单词头部,最后两者相减,即为单词的长度
- 先从后过滤掉空格找到单词尾部,再从尾部向前遍历,找到单词头部,最后两者相减,即为单词的长度
## 代码
```go
package leetcode
func lengthOfLastWord(s string) int {
last := len(s) - 1
for last >= 0 && s[last] == ' ' {
last--
}
if last < 0 {
return 0
}
first := last
for first >= 0 && s[first] != ' ' {
first--
}
return last - first
}
```

View File

@ -1,37 +1,42 @@
# [551. Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/)
# [551. Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/)
## 题目
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:
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:
- 'A': Absent.
- 'L': Late.
- 'P': Present.
- `'A'`: Absent.
- `'L'`: Late.
- `'P'`: Present.
The student is eligible for an attendance award if they meet both of the following criteria:
The student is eligible for an attendance award if they meet **both** of the following criteria:
The student was absent ('A') for strictly fewer than 2 days total.
- The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
- The student was **never** late (`'L'`) for 3 or more **consecutive** days.
The student was never late ('L') for 3 or more consecutive days.
Return true if the student is eligible for an attendance award, or false otherwise.
Return `true` *if the student is eligible for an attendance award, or* `false` *otherwise*.
**Example 1:**
Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
```
Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
```
**Example 2:**
Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
```
Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
```
**Constraints:**
- 1 <= s.length <= 1000
- s[i] is either 'A', 'L', or 'P'.
- `1 <= s.length <= 1000`
- `s[i]` is either `'A'`, `'L'`, or `'P'`.
## 题目大意
@ -43,13 +48,39 @@ Return true if the student is eligible for an attendance award, or false otherwi
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤('A')严格 少于两天。
学生 不会 存在 连续 3 天或 3 天以上的迟到('L')记录。
- 按 总出勤 计,学生缺勤('A')严格 少于两天。
- 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到('L')记录。
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
## 解题思路
- 遍历字符串s求出'A'的总数量和连续'L'的最大数量
- 比较'A'的数量是否小于2并且'L'的连续最大数量是否小于3
- 遍历字符串 s 求出 'A' 的总数量和连续 'L' 的最大数量
- 比较 'A' 的数量是否小于 2 并且 'L' 的连续最大数量是否小于 3。
## 代码
```go
package leetcode
func checkRecord(s string) bool {
numsA, maxL, numsL := 0, 0, 0
for _, v := range s {
if v == 'L' {
numsL++
} else {
if numsL > maxL {
maxL = numsL
}
numsL = 0
if v == 'A' {
numsA++
}
}
}
if numsL > maxL {
maxL = numsL
}
return numsA < 2 && maxL < 3
}
```

View File

@ -17,9 +17,7 @@ type TreeNode = structures.TreeNode
*/
func isCompleteTree(root *TreeNode) bool {
queue := []*TreeNode{root}
found := false
queue, found := []*TreeNode{root}, false
for len(queue) > 0 {
node := queue[0] //取出每一层的第一个节点
queue = queue[1:]
@ -34,4 +32,4 @@ func isCompleteTree(root *TreeNode) bool {
}
}
return true
}
}

View File

@ -1,53 +1,89 @@
# [958. Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)
## 题目
Given the root of a binary tree, determine if it is a complete binary tree.
Given the `root` of a binary tree, determine if it is a *complete binary tree*.
In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
In a **[complete binary tree](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between `1` and `2h` nodes inclusive at the last level `h`.
Example 1:
```c
**Example 1:**
![https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png)
```
Input: root = [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
1
/ \
2 3
/ \ /
4 5 6
```
Example 2:
```c
**Example 2:**
![https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png)
```
Input: root = [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.
1
/ \
2 3
/ \ \
4 5 7
```
Constraints:
The number of nodes in the tree is in the range [1, 100].
1 <= Node.val <= 1000
**Constraints:**
- The number of nodes in the tree is in the range `[1, 100]`.
- `1 <= Node.val <= 1000`
## 题目大意
给定一个二叉树,确定它是否是一个完全二叉树。
百度百科中对完全二叉树的定义如下:
若设二叉树的深度为 h除第 h 层外,其它各层 (1h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。(注:第 h 层可能包含 1~ 2h 个节点。
说明要判断每个节点的左孩子必须不为空。
## 解题思路
- 这一题是按层序遍历的变种题。
- 判断每个节点的左孩子是否为空。
- 第 102,107,199 都是按层序遍历的。
- 类似的题目,第 102107199 都是按层序遍历的。
## 代码
```go
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isCompleteTree(root *TreeNode) bool {
queue, found := []*TreeNode{root}, false
for len(queue) > 0 {
node := queue[0] //取出每一层的第一个节点
queue = queue[1:]
if node == nil {
found = true
} else {
if found {
return false // 层序遍历中,两个不为空的节点中出现一个 nil
}
//如果左孩子为nil则append进去的node.Left为nil
queue = append(queue, node.Left, node.Right)
}
}
return true
}
```