Skip to content

Commit c0191c2

Browse files
committed
Add solution 1268
1 parent ec597e2 commit c0191c2

26 files changed

+925
-645
lines changed

README.md

Lines changed: 461 additions & 459 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func suggestedProducts(products []string, searchWord string) [][]string {
8+
sort.Strings(products)
9+
searchWordBytes, result := []byte(searchWord), make([][]string, 0, len(searchWord))
10+
for i := 1; i <= len(searchWord); i++ {
11+
searchWordBytes[i-1]++
12+
products = products[:sort.SearchStrings(products, string(searchWordBytes[:i]))]
13+
searchWordBytes[i-1]--
14+
products = products[sort.SearchStrings(products, searchWord[:i]):]
15+
if len(products) > 3 {
16+
result = append(result, products[:3])
17+
} else {
18+
result = append(result, products)
19+
}
20+
}
21+
return result
22+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1268 struct {
9+
para1268
10+
ans1268
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1268 struct {
16+
products []string
17+
searchWord string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1268 struct {
23+
one [][]string
24+
}
25+
26+
func Test_Problem1268(t *testing.T) {
27+
28+
qs := []question1268{
29+
30+
{
31+
para1268{[]string{"bags", "baggage", "banner", "box", "cloths"}, "bags"},
32+
ans1268{[][]string{
33+
{"baggage", "bags", "banner"}, {"baggage", "bags", "banner"}, {"baggage", "bags"}, {"bags"},
34+
}},
35+
},
36+
37+
{
38+
para1268{[]string{"mobile", "mouse", "moneypot", "monitor", "mousepad"}, "mouse"},
39+
ans1268{[][]string{
40+
{"mobile", "moneypot", "monitor"},
41+
{"mobile", "moneypot", "monitor"},
42+
{"mouse", "mousepad"},
43+
{"mouse", "mousepad"},
44+
{"mouse", "mousepad"},
45+
}},
46+
},
47+
48+
{
49+
para1268{[]string{"havana"}, "havana"},
50+
ans1268{[][]string{
51+
{"havana"}, {"havana"}, {"havana"}, {"havana"}, {"havana"}, {"havana"},
52+
}},
53+
},
54+
55+
{
56+
para1268{[]string{"havana"}, "tatiana"},
57+
ans1268{[][]string{
58+
{}, {}, {}, {}, {}, {}, {},
59+
}},
60+
},
61+
}
62+
63+
fmt.Printf("------------------------Leetcode Problem 1268------------------------\n")
64+
65+
for _, q := range qs {
66+
_, p := q.ans1268, q.para1268
67+
fmt.Printf("【input】:%v 【output】:%v\n", p, suggestedProducts(p.products, p.searchWord))
68+
}
69+
fmt.Printf("\n\n\n")
70+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [1268. Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)
2+
3+
## 题目
4+
5+
Given an array of strings `products` and a string `searchWord`. We want to design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
6+
7+
Return *list of lists* of the suggested `products` after each character of `searchWord` is typed.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
13+
Output: [
14+
["mobile","moneypot","monitor"],
15+
["mobile","moneypot","monitor"],
16+
["mouse","mousepad"],
17+
["mouse","mousepad"],
18+
["mouse","mousepad"]
19+
]
20+
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
21+
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
22+
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
23+
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: products = ["havana"], searchWord = "havana"
30+
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
37+
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
38+
```
39+
40+
**Example 4:**
41+
42+
```
43+
Input: products = ["havana"], searchWord = "tatiana"
44+
Output: [[],[],[],[],[],[],[]]
45+
```
46+
47+
**Constraints:**
48+
49+
- `1 <= products.length <= 1000`
50+
- There are no repeated elements in `products`.
51+
- `1 <= Σ products[i].length <= 2 * 10^4`
52+
- All characters of `products[i]` are lower-case English letters.
53+
- `1 <= searchWord.length <= 1000`
54+
- All characters of `searchWord` are lower-case English letters.
55+
56+
## 题目大意
57+
58+
给你一个产品数组 products 和一个字符串 searchWord ,products  数组中每个产品都是一个字符串。请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。
59+
60+
## 解题思路
61+
62+
- 由于题目要求返回的答案要按照字典序输出,所以先排序。有序字符串又满足了二分搜索的条件,于是可以用二分搜索。sort.SearchStrings 返回的是满足搜索条件的第一个起始下标。末尾不满足条件的字符串要切掉。所以要搜 2 次,第一次二分搜索先将不满足目标串前缀的字符串筛掉。第二次二分搜索再搜索出最终满足题意的字符串。
63+
64+
## 代码
65+
66+
```go
67+
package leetcode
68+
69+
import (
70+
"sort"
71+
)
72+
73+
func suggestedProducts(products []string, searchWord string) [][]string {
74+
sort.Strings(products)
75+
searchWordBytes, result := []byte(searchWord), make([][]string, 0, len(searchWord))
76+
for i := 1; i <= len(searchWord); i++ {
77+
searchWordBytes[i-1]++
78+
products = products[:sort.SearchStrings(products, string(searchWordBytes[:i]))]
79+
searchWordBytes[i-1]--
80+
products = products[sort.SearchStrings(products, searchWord[:i]):]
81+
if len(products) > 3 {
82+
result = append(result, products[:3])
83+
} else {
84+
result = append(result, products)
85+
}
86+
}
87+
return result
88+
}
89+
```

website/content/ChapterFour/1200~1299/1266.Minimum-Time-Visiting-All-Points.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ func minTimeToVisitAllPoints(points [][]int) int {
7878
----------------------------------------------
7979
<div style="display: flex;justify-content: space-between;align-items: center;">
8080
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1260.Shift-2D-Grid/">⬅️上一页</a></p>
81-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/">下一页➡️</a></p>
81+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1268.Search-Suggestions-System/">下一页➡️</a></p>
8282
</div>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [1268. Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)
2+
3+
## 题目
4+
5+
Given an array of strings `products` and a string `searchWord`. We want to design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
6+
7+
Return *list of lists* of the suggested `products` after each character of `searchWord` is typed.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
13+
Output: [
14+
["mobile","moneypot","monitor"],
15+
["mobile","moneypot","monitor"],
16+
["mouse","mousepad"],
17+
["mouse","mousepad"],
18+
["mouse","mousepad"]
19+
]
20+
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
21+
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
22+
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
23+
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: products = ["havana"], searchWord = "havana"
30+
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
37+
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
38+
```
39+
40+
**Example 4:**
41+
42+
```
43+
Input: products = ["havana"], searchWord = "tatiana"
44+
Output: [[],[],[],[],[],[],[]]
45+
```
46+
47+
**Constraints:**
48+
49+
- `1 <= products.length <= 1000`
50+
- There are no repeated elements in `products`.
51+
- `1 <= Σ products[i].length <= 2 * 10^4`
52+
- All characters of `products[i]` are lower-case English letters.
53+
- `1 <= searchWord.length <= 1000`
54+
- All characters of `searchWord` are lower-case English letters.
55+
56+
## 题目大意
57+
58+
给你一个产品数组 products 和一个字符串 searchWord ,products  数组中每个产品都是一个字符串。请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。
59+
60+
## 解题思路
61+
62+
- 由于题目要求返回的答案要按照字典序输出,所以先排序。有序字符串又满足了二分搜索的条件,于是可以用二分搜索。sort.SearchStrings 返回的是满足搜索条件的第一个起始下标。末尾不满足条件的字符串要切掉。所以要搜 2 次,第一次二分搜索先将不满足目标串前缀的字符串筛掉。第二次二分搜索再搜索出最终满足题意的字符串。
63+
64+
## 代码
65+
66+
```go
67+
package leetcode
68+
69+
import (
70+
"sort"
71+
)
72+
73+
func suggestedProducts(products []string, searchWord string) [][]string {
74+
sort.Strings(products)
75+
searchWordBytes, result := []byte(searchWord), make([][]string, 0, len(searchWord))
76+
for i := 1; i <= len(searchWord); i++ {
77+
searchWordBytes[i-1]++
78+
products = products[:sort.SearchStrings(products, string(searchWordBytes[:i]))]
79+
searchWordBytes[i-1]--
80+
products = products[sort.SearchStrings(products, searchWord[:i]):]
81+
if len(products) > 3 {
82+
result = append(result, products[:3])
83+
} else {
84+
result = append(result, products)
85+
}
86+
}
87+
return result
88+
}
89+
```
90+
91+
92+
----------------------------------------------
93+
<div style="display: flex;justify-content: space-between;align-items: center;">
94+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1266.Minimum-Time-Visiting-All-Points/">⬅️上一页</a></p>
95+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/">下一页➡️</a></p>
96+
</div>

website/content/ChapterFour/1200~1299/1275.Find-Winner-on-a-Tic-Tac-Toe-Game.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,6 @@ func tictactoe(moves [][]int) string {
156156

157157
----------------------------------------------
158158
<div style="display: flex;justify-content: space-between;align-items: center;">
159-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1266.Minimum-Time-Visiting-All-Points/">⬅️上一页</a></p>
159+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1268.Search-Suggestions-System/">⬅️上一页</a></p>
160160
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/">下一页➡️</a></p>
161161
</div>

0 commit comments

Comments
 (0)