Skip to content

Commit d876599

Browse files
committed
添加 3 题
1 parent d0cda0c commit d876599

12 files changed

+667
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
func isPrefixOfWord(sentence string, searchWord string) int {
6+
for i, v := range strings.Split(sentence, " ") {
7+
if strings.HasPrefix(v, searchWord) {
8+
return i + 1
9+
}
10+
}
11+
return -1
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1455 struct {
9+
para1455
10+
ans1455
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1455 struct {
16+
sentence string
17+
searchWord string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1455 struct {
23+
one int
24+
}
25+
26+
func Test_Problem1455(t *testing.T) {
27+
28+
qs := []question1455{
29+
30+
question1455{
31+
para1455{"i love eating burger", "burg"},
32+
ans1455{4},
33+
},
34+
35+
question1455{
36+
para1455{"this problem is an easy problem", "pro"},
37+
ans1455{2},
38+
},
39+
40+
question1455{
41+
para1455{"i am tired", "you"},
42+
ans1455{-1},
43+
},
44+
45+
question1455{
46+
para1455{"i use triple pillow", "pill"},
47+
ans1455{4},
48+
},
49+
50+
question1455{
51+
para1455{"hello from the other side", "they"},
52+
ans1455{-1},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 1455------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans1455, q.para1455
60+
fmt.Printf("【input】:%v ", p)
61+
fmt.Printf("【output】:%v \n", isPrefixOfWord(p.sentence, p.searchWord))
62+
}
63+
fmt.Printf("\n\n\n")
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)
2+
3+
4+
## 题目
5+
6+
Given a `sentence` that consists of some words separated by a **single space**, and a `searchWord`.
7+
8+
You have to check if `searchWord` is a prefix of any word in `sentence`.
9+
10+
Return *the index of the word* in `sentence` where `searchWord` is a prefix of this word (**1-indexed**).
11+
12+
If `searchWord` is a prefix of more than one word, return the index of the first word **(minimum index)**. If there is no such word return **-1**.
13+
14+
**prefix** of a string `S` is any leading contiguous substring of `S`.
15+
16+
**Example 1**:
17+
18+
```
19+
Input: sentence = "i love eating burger", searchWord = "burg"
20+
Output: 4
21+
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
22+
23+
```
24+
25+
**Example 2**:
26+
27+
```
28+
Input: sentence = "this problem is an easy problem", searchWord = "pro"
29+
Output: 2
30+
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
31+
32+
```
33+
34+
**Example 3**:
35+
36+
```
37+
Input: sentence = "i am tired", searchWord = "you"
38+
Output: -1
39+
Explanation: "you" is not a prefix of any word in the sentence.
40+
41+
```
42+
43+
**Example 4**:
44+
45+
```
46+
Input: sentence = "i use triple pillow", searchWord = "pill"
47+
Output: 4
48+
49+
```
50+
51+
**Example 5**:
52+
53+
```
54+
Input: sentence = "hello from the other side", searchWord = "they"
55+
Output: -1
56+
57+
```
58+
59+
**Constraints**:
60+
61+
- `1 <= sentence.length <= 100`
62+
- `1 <= searchWord.length <= 10`
63+
- `sentence` consists of lowercase English letters and spaces.
64+
- `searchWord` consists of lowercase English letters.
65+
66+
## 题目大意
67+
68+
给你一个字符串 sentence 作为句子并指定检索词为 searchWord ,其中句子由若干用 单个空格 分隔的单词组成。请你检查检索词 searchWord 是否为句子 sentence 中任意单词的前缀。
69+
70+
- 如果 searchWord 是某一个单词的前缀,则返回句子 sentence 中该单词所对应的下标(下标从 1 开始)。
71+
- 如果 searchWord 是多个单词的前缀,则返回匹配的第一个单词的下标(最小下标)。
72+
- 如果 searchWord 不是任何单词的前缀,则返回 -1 。
73+
74+
字符串 S 的 「前缀」是 S 的任何前导连续子字符串。
75+
76+
## 解题思路
77+
78+
- 给出 2 个字符串,一个是匹配串,另外一个是句子。在句子里面查找带匹配串前缀的单词,并返回第一个匹配单词的下标。
79+
- 简单题。按照题意,扫描一遍句子,一次匹配即可。
80+
81+
## 代码
82+
83+
```go
84+
85+
package leetcode
86+
87+
import "strings"
88+
89+
func isPrefixOfWord(sentence string, searchWord string) int {
90+
for i, v := range strings.Split(sentence, " ") {
91+
if strings.HasPrefix(v, searchWord) {
92+
return i + 1
93+
}
94+
}
95+
return -1
96+
}
97+
98+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
func maxProduct(nums []int) int {
4+
max1, max2 := 0, 0
5+
for _, num := range nums {
6+
if num >= max1 {
7+
max2 = max1
8+
max1 = num
9+
} else if num <= max1 && num >= max2 {
10+
max2 = num
11+
}
12+
}
13+
return (max1 - 1) * (max2 - 1)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1464 struct {
9+
para1464
10+
ans1464
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1464 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1464 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1464(t *testing.T) {
26+
27+
qs := []question1464{
28+
29+
question1464{
30+
para1464{[]int{3, 4, 5, 2}},
31+
ans1464{12},
32+
},
33+
34+
question1464{
35+
para1464{[]int{1, 5, 4, 5}},
36+
ans1464{16},
37+
},
38+
39+
question1464{
40+
para1464{[]int{3, 7}},
41+
ans1464{12},
42+
},
43+
44+
question1464{
45+
para1464{[]int{1}},
46+
ans1464{0},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 1464------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans1464, q.para1464
54+
fmt.Printf("【input】:%v ", p)
55+
fmt.Printf("【output】:%v \n", maxProduct(p.nums))
56+
}
57+
fmt.Printf("\n\n\n")
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [1464. Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)
2+
3+
4+
## 题目
5+
6+
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. Return the maximum value of `(nums[i]-1)*(nums[j]-1)`.
7+
8+
**Example 1**:
9+
10+
```
11+
Input: nums = [3,4,5,2]
12+
Output: 12
13+
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.
14+
15+
```
16+
17+
**Example 2**:
18+
19+
```
20+
Input: nums = [1,5,4,5]
21+
Output: 16
22+
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
23+
24+
```
25+
26+
**Example 3**:
27+
28+
```
29+
Input: nums = [3,7]
30+
Output: 12
31+
32+
```
33+
34+
**Constraints**:
35+
36+
- `2 <= nums.length <= 500`
37+
- `1 <= nums[i] <= 10^3`
38+
39+
## 题目大意
40+
41+
给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值。请你计算并返回该式的最大值。
42+
43+
## 解题思路
44+
45+
- 简单题。循环一次,按照题意动态维护 2 个最大值,从而也使得 `(nums[i]-1)*(nums[j]-1)` 能取到最大值。
46+
47+
## 代码
48+
49+
```go
50+
51+
package leetcode
52+
53+
func maxProduct(nums []int) int {
54+
max1, max2 := 0, 0
55+
for _, num := range nums {
56+
if num >= max1 {
57+
max2 = max1
58+
max1 = num
59+
} else if num <= max1 && num >= max2 {
60+
max2 = num
61+
}
62+
}
63+
return (max1 - 1) * (max2 - 1)
64+
}
65+
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package leetcode
2+
3+
func shuffle(nums []int, n int) []int {
4+
result := make([]int, 0)
5+
for i := 0; i < n; i++ {
6+
result = append(result, nums[i])
7+
result = append(result, nums[n+i])
8+
}
9+
return result
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1470 struct {
9+
para1470
10+
ans1470
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1470 struct {
16+
nums []int
17+
n int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1470 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem1470(t *testing.T) {
27+
28+
qs := []question1470{
29+
30+
question1470{
31+
para1470{[]int{2, 5, 1, 3, 4, 7}, 3},
32+
ans1470{[]int{2, 3, 5, 4, 1, 7}},
33+
},
34+
35+
question1470{
36+
para1470{[]int{1, 2, 3, 4, 4, 3, 2, 1}, 4},
37+
ans1470{[]int{1, 4, 2, 3, 3, 2, 4, 1}},
38+
},
39+
40+
question1470{
41+
para1470{[]int{1, 1, 2, 2}, 2},
42+
ans1470{[]int{1, 2, 1, 2}},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1470------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1470, q.para1470
50+
fmt.Printf("【input】:%v 【output】:%v \n", p, shuffle(p.nums, p.n))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}

0 commit comments

Comments
 (0)